我有两个表,如下所示。
表1:
Day Group
---------
1 A
1 B
1 C
2 B
2 C
2 D
3 C
3 D
3 E
表2:
Group
-------
A
B
C
D
E
我想创建一个SQL查询,该查询标识在表2中存在但在按天划分的表1中不存在的每个组。
所需的结果如下:
Day Group
---------
1 D
1 E
2 A
2 E
3 A
3 B
答案 0 :(得分:0)
使用cross join
生成所有组合,然后清除掉您拥有的东西:
select d.day, t1.group
from (select distinct day from table1) d cross join
table2 g left join
table1 t1
on t1.day = d.day and t1.group = g.group
where t1.day is null;
答案 1 :(得分:0)
SELECT
*
FROM
(
SELECT DISTINCT
Day
FROM
Table1
) AS Days
,
Table2
WHERE
NOT EXISTS (
SELECT
*
FROM
Table1
WHERE
Table1.day=Days.Day AND
Table1.Group=Table2.Group
)