我正在使用 SQLiteStudio,我正在嘗試運行以下查詢。但是,它沒有回傳值。有人可以幫忙嗎?
SELECT
(SELECT COUNT(t_record.LocationID)
FROM t_record,
t_location
WHERE t_record.LocationID = t_location.LocationID AND
Y_AXIS >= 0 AND
Goal_for = 1)
/
(SELECT COUNT(GoalID)
FROM t_record
WHERE t_record.Goal_for)
* 100
我被要求提供一些資料。下面我介紹了我是如何創建 3 個表的,然后是每個部分的一些示例資料。希望這已經足夠了,但請隨時提出更多要求。這些是我創建的 3 個表
CREATE TABLE t_location (
LocationID INT PRIMARY KEY,
X_Axis INT NOT NULL,
Y_AXIS INT NOT NULL
);
CREATE TABLE t_method (
MethodID INT PRIMARY KEY,
Body_Part VARCHAR(45) NOT NULL
);
CREATE TABLE t_record (
GoalID INT PRIMARY KEY,
LocationID [INT FORIEIGN KEY] REFERENCES t_location (LocationID),
MethodID [INT FORIEIGN KEY] REFERENCES t_method (MethodID),
Time INT NOT NULL,
Goal_for BOOLEAN NOT NULL
方法表的資料
INSERT INTO t_method
VALUES (1,'Left Foot');
INSERT INTO t_method
VALUES (2,'Right Foot');
INSERT INTO t_method
VALUES (3,'Head');
位置表的資料 - 僅包括 3 個位置。
INSERT INTO t_location
VALUES (1, 0, -1);
INSERT INTO t_location
VALUES (2, 0, 0);
INSERT INTO t_location
VALUES (3, 0, 1);
記錄表的資料,很計劃,但我改變了位置。
INSERT INTO t_record
VALUES (1, 1, 1, 28, 1);
INSERT INTO t_record
VALUES (2, 1, 1, 6, 1);
INSERT INTO t_record
VALUES (3, 2, 1, 28, 1);
INSERT INTO t_record
VALUES (4, 2, 1, 28, 1);
INSERT INTO t_record
VALUES (5, 2, 1, 28, 1);
INSERT INTO t_record
VALUES (6, 2, 1, 28, 1);
INSERT INTO t_record
VALUES (7, 3, 1, 28, 1);
INSERT INTO t_record
VALUES (8, 3, 1, 28, 1);
INSERT INTO t_record
VALUES (9, 3, 1, 28, 1);
INSERT INTO t_record
VALUES (10, 3, 1, 28, 1);
INSERT INTO t_record
VALUES (11, 3, 1, 28, 1);
INSERT INTO t_record
VALUES (12, 3, 1, 28, 1);
INSERT INTO t_record
VALUES (1, 3, 1, 28, 1);
謝謝
uj5u.com熱心網友回復:
實際上你只有一些語法錯誤并更正它們加上像 * 1.0 這樣的東西到 count() 會做你想要的(否則 count() 結果是整數,在 SQL Server 整數/整數中會產生一個整數,在你的案件)。
這是您的代碼,稍作修改即可作業:
SELECT
(SELECT COUNT(t_record.LocationID)
FROM t_record,
t_location
WHERE t_record.LocationID = t_location.LocationID AND
Y_AXIS >= 0 AND
Goal_for = 1) * 1.0
/
(SELECT COUNT(GoalID)
FROM t_record
WHERE t_record.Goal_for=1)
* 100;
(我同意防止舊式連接等,但這里不是問題)。你可以讓它變得更好一點:
SELECT
(SELECT COUNT(t_record.LocationID)
FROM t_record,
t_location
WHERE t_record.LocationID = t_location.LocationID AND
Y_AXIS >= 0 AND
Goal_for = 1) * 100.0
/
(SELECT COUNT(GoalID)
FROM t_record
WHERE t_record.Goal_for=1) as Pct;
雖然上面的代碼有效,但我認為有一種更好、更清潔的方法:
SELECT SUM(case when Y_AXIS >= 0 then 1 end) * 100.0 / COUNT(*)
FROM t_record
inner join t_location ON t_record.LocationID = t_location.LocationID
where Goal_for = 1;
您還可以通過添加這些欄位來檢查那里的實際情況:
SELECT
SUM(case when Y_AXIS >= 0 then 1 end) as YAXIS_GTE_ZERO,
COUNT(*) as Total,
SUM(case when Y_AXIS >= 0 then 1 end) * 100.0 / COUNT(*) as Pct
FROM t_record
inner join t_location ON t_record.LocationID = t_location.LocationID
where Goal_for = 1;
這是使用您的示例資料的 DBFiddle 演示
uj5u.com熱心網友回復:
我試過這個;在運算式中使用1.0而不是,以避免整數除以整數,這將導致截斷整數作為結果,即 0:1CASE WHEN
SELECT
SUM(CASE WHEN y_axis >= 0 THEN 1.0 END)
/ COUNT(*)
* 100
AS result
FROM t_record
JOIN t_location USING(locationid)
;
-- out result
-- out ------------------------
-- out 84.6153846153846153800
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/418725.html
標籤:
