为问题准备 sql 查询。
CREATE TABLE right_data(RID NUMBER(9,0), STATUS NUMBER(2,0));
CREATE TABLE left_data(LID NUMBER(9,0), URL VARCHAR(70 BYTE));
CREATE TABLE left_to_right(LID NUMBER(9,0), RID NUMBER(9,0));
INSERT INTO right_data (RID, STATUS) VALUES (1,0);
INSERT INTO right_data (RID, STATUS) VALUES (2,1);
INSERT INTO right_data (RID, STATUS) VALUES (3,0);
INSERT INTO right_data (RID, STATUS) VALUES (4,0);
INSERT INTO right_data (RID, STATUS) VALUES (5,1);
INSERT INTO left_data (LID, URL) VALUES (1,'data_1');
INSERT INTO left_data (LID, URL) VALUES (2,'data_2');
INSERT INTO left_data (LID, URL) VALUES (3,'data_3');
INSERT INTO left_data (LID, URL) VALUES (4,'data_4');
INSERT INTO left_data (LID, URL) VALUES (5,'data_5');
INSERT INTO left_to_right (LID, RID) VALUES (1,1);
INSERT INTO left_to_right (LID, RID) VALUES (1,2);
INSERT INTO left_to_right (LID, RID) VALUES (2,1);
INSERT INTO left_to_right (LID, RID) VALUES (1,4);
INSERT INTO left_to_right (LID, RID) VALUES (4,3);
INSERT INTO left_to_right (LID, RID) VALUES (5,1);
INSERT INTO left_to_right (LID, RID) VALUES (3,5);
INSERT INTO left_to_right (LID, RID) VALUES (4,2);
一个错误的查询。
SELECT
DISTINCT left_to_right.LID
, COUNT(left_to_right.RID) OVER (PARTITION BY left_to_right.LID) AS NUM_RID
, COUNT(left_to_right.RID) OVER (PARTITION BY
left_to_right.LID
AND
right_data.STATUS = 1
) AS NUM_RID_S1
FROM
left_to_right
JOIN
right_data
ON
right_data.RID = left_to_right.RID
ORDER BY left_to_right.LID;
我如何计算同一查询 LID/(所有 right_data.status 的数量)和 LID/(right_data.status 的数量为 1)?