SQL Select 比较主列表和两个子列表的独占 ID
SQL Select exclusive ids comparing master list with two sub lists
我需要有关 SQL 查询的帮助。我有一个"Master list table (X and Y categories)"。我必须将上面的列表与两个子列表表进行比较 - "List X" 和 "List Y"
表:
*MasterList*
ID Cat Status
101 X Ready
102 X Ready
103 Y Dispatched Y
104 X Dispatched Y
105 Y Dispatched
106 X Ready
107 X Dispatched Y
108 Y Ready Y
109 X Dispatched
110 Y Dispatched
111 X Ready Y
112 X Dispatched
113 X Dispatched Y
*ListX*
ID
101
102
106
109
112
*ListY*
ID
105
110
我正在尝试创建一个输出的查询:
- Cat-X 在 List-X
中不可用的 ID
- Cat-Y 在 List-Y
中不可用的 ID
输出
ID Cat Status
103 Y Dispatched
104 X Dispatched
107 X Dispatched
108 Y Ready
111 X Ready
113 X Dispatched
谢谢,
拉维
您可以通过多种方式完成此操作,例如此处:
select *
from master_list
where (cat = 'X' and id not in (select id from listx))
or (cat = 'Y' and id not in (select id from listy))
也可以尝试 not exists
和简单连接。
在Oracle中测试数据并输出:
create table master_list (id int, cat varchar2(1), status varchar2(15));
insert into master_list values (101, 'X', 'Ready');
insert into master_list values (102, 'X', 'Ready');
insert into master_list values (103, 'Y', 'Dispatched');
insert into master_list values (104, 'X', 'Dispatched');
insert into master_list values (105, 'Y', 'Dispatched');
insert into master_list values (106, 'X', 'Ready');
insert into master_list values (107, 'X', 'Dispatched');
insert into master_list values (108, 'Y', 'Ready');
insert into master_list values (109, 'X', 'Dispatched');
insert into master_list values (110, 'Y', 'Dispatched');
insert into master_list values (111, 'X', 'Ready');
insert into master_list values (112, 'X', 'Dispatched');
insert into master_list values (113, 'X', 'Dispatched');
create table listx as (select column_value id
from table(sys.odcinumberlist(101, 102, 106, 109, 112)));
create table listy as (select column_value id
from table(sys.odcinumberlist(105, 110)));
ID CAT STATUS
---- --- ---------------
103 Y Dispatched
104 X Dispatched
107 X Dispatched
108 Y Ready
111 X Ready
113 X Dispatched
您可以在 WHERE 子句中使用 OR:
SELECT ID, CAT, STATUS
FROM MASTERLIST M
WHERE (M.ID NOT IN (SELECT ID FROM LISTX) AND CAT = 'X')
OR (M.ID NOT IN (SELECT ID FROM LISTY) AND CAT = 'Y');
其他替代方法是将两个查询中的规则分开,然后与 UNION ALL 组合:
SELECT ID, CAT, STATUS
FROM MASTERLIST M
WHERE M.ID NOT IN (SELECT ID FROM LISTX) AND CAT = 'X'
UNION ALL
SELECT ID, CAT, STATUS
FROM MASTERLIST M
WHERE M.ID NOT IN (SELECT ID FROM LISTY) AND CAT = 'Y';
两种技术都会产生相同的结果。实际上,一些优化器(如 Oracle 的)会尝试将第一个解决方案实时转换为第二个解决方案。这就是所谓的 OR 扩展。
我需要有关 SQL 查询的帮助。我有一个"Master list table (X and Y categories)"。我必须将上面的列表与两个子列表表进行比较 - "List X" 和 "List Y"
表:
*MasterList*
ID Cat Status
101 X Ready
102 X Ready
103 Y Dispatched Y
104 X Dispatched Y
105 Y Dispatched
106 X Ready
107 X Dispatched Y
108 Y Ready Y
109 X Dispatched
110 Y Dispatched
111 X Ready Y
112 X Dispatched
113 X Dispatched Y
*ListX*
ID
101
102
106
109
112
*ListY*
ID
105
110
我正在尝试创建一个输出的查询:
- Cat-X 在 List-X 中不可用的 ID
- Cat-Y 在 List-Y 中不可用的 ID
输出
ID Cat Status
103 Y Dispatched
104 X Dispatched
107 X Dispatched
108 Y Ready
111 X Ready
113 X Dispatched
谢谢,
拉维
您可以通过多种方式完成此操作,例如此处:
select *
from master_list
where (cat = 'X' and id not in (select id from listx))
or (cat = 'Y' and id not in (select id from listy))
也可以尝试 not exists
和简单连接。
在Oracle中测试数据并输出:
create table master_list (id int, cat varchar2(1), status varchar2(15));
insert into master_list values (101, 'X', 'Ready');
insert into master_list values (102, 'X', 'Ready');
insert into master_list values (103, 'Y', 'Dispatched');
insert into master_list values (104, 'X', 'Dispatched');
insert into master_list values (105, 'Y', 'Dispatched');
insert into master_list values (106, 'X', 'Ready');
insert into master_list values (107, 'X', 'Dispatched');
insert into master_list values (108, 'Y', 'Ready');
insert into master_list values (109, 'X', 'Dispatched');
insert into master_list values (110, 'Y', 'Dispatched');
insert into master_list values (111, 'X', 'Ready');
insert into master_list values (112, 'X', 'Dispatched');
insert into master_list values (113, 'X', 'Dispatched');
create table listx as (select column_value id
from table(sys.odcinumberlist(101, 102, 106, 109, 112)));
create table listy as (select column_value id
from table(sys.odcinumberlist(105, 110)));
ID CAT STATUS
---- --- ---------------
103 Y Dispatched
104 X Dispatched
107 X Dispatched
108 Y Ready
111 X Ready
113 X Dispatched
您可以在 WHERE 子句中使用 OR:
SELECT ID, CAT, STATUS
FROM MASTERLIST M
WHERE (M.ID NOT IN (SELECT ID FROM LISTX) AND CAT = 'X')
OR (M.ID NOT IN (SELECT ID FROM LISTY) AND CAT = 'Y');
其他替代方法是将两个查询中的规则分开,然后与 UNION ALL 组合:
SELECT ID, CAT, STATUS
FROM MASTERLIST M
WHERE M.ID NOT IN (SELECT ID FROM LISTX) AND CAT = 'X'
UNION ALL
SELECT ID, CAT, STATUS
FROM MASTERLIST M
WHERE M.ID NOT IN (SELECT ID FROM LISTY) AND CAT = 'Y';
两种技术都会产生相同的结果。实际上,一些优化器(如 Oracle 的)会尝试将第一个解决方案实时转换为第二个解决方案。这就是所谓的 OR 扩展。