如何使用 DB2 sql 检查另外三分之一 table 中不在两个 table 中的记录?
How to check records not in two tables for a date in another third table using DB2 sql?
我有一个员工 table,其中包含带有 ID 的员工姓名列表,它们从这里流向具有以下结构的两个 table:
电磁脉冲Table
ID
EMP_NAME
100
BOB
create table EMP (ID varchar(20),EMP_NAME varchar(20));
insert into EMP values('100','BOB')
Table 1
ID
NAME
DATE
100
BOB
01-10-2021
100
BOB
01-11-2021
create table Table_1(ID varchar(20),NAME varchar(20), DATE date);
insert into Table_1 values('100','BOB','01-10-2021');insert into Table_1 values('100','BOB','01-11-2021');
Table 2
ID
NAME
DATE
100
BOB
01-11-2021
100
BOB
01-12-2021
create table Table_2(ID varchar(20),NAME varchar(20), DATE date);
insert into Table_2 values('100','BOB','01-11-2021');insert into Table_1 values('100','BOB','01-12-2021');
Table 日期
Month
DATE
Sep
01-09-2021
Oct
01-10-2021
Nov
01-11-2021
Dec
01-12-2021
create table DATE(Month varchar(20), DATE date);
insert into DATE values('Sep','01-09-2021');insert into DATE values('Sep','01-10-2021');insert into DATE values('Sep','01-11-2021'); insert into DATE values('Sep','01-12-2021'))
我想参考table table 3 (Date Table) 以确定 TABLE 3 记录在哪一天没有出现在 TABLE 1 和 2(在给定的情况下,日期 = 01-09-2021 的记录是预期的输出)
预期输出
ID
NAME
DATE
100
BOB
01-09-2021
试试这个:
SELECT E.ID, E.EMP_NAME, D.DATE
FROM DATE D, EMP E
WHERE
NOT EXISTS (SELECT 1 FROM Table_1 T WHERE T.ID = E.ID AND T.DATE = D.DATE)
AND NOT EXISTS (SELECT 1 FROM Table_2 T WHERE T.ID = E.ID AND T.DATE = D.DATE)
I want to refer table table 3 (Date Table) to identify on which date of TABLE 3 record did not appear in TABLE 1 and 2
如果你只是想要不出现的整体日期,你可以使用not exists
或left join
:
select d.*
from dates d
where not exists (select 1 from table1 t1 where t1.date = d.date) and
not exists (select 1 from table2 t2 where t2.date = d.date);
这就是您要问的问题。但是,您想要的结果表明您希望 employee/date 组合不出现。如果是这种情况,则 CROSS JOIN
emp
和 dates
表并过滤掉不存在的表。一种方法:
select e.*, d.*
from emp e cross join
dates d left join
table1 t1
on t1.date = d.date and t1.id = e.id left join
table2 t2
on t2.date = d.date and t2.id = e.id
where t1.id is null and t2.id is null;
我有一个员工 table,其中包含带有 ID 的员工姓名列表,它们从这里流向具有以下结构的两个 table:
电磁脉冲Table
ID | EMP_NAME |
---|---|
100 | BOB |
create table EMP (ID varchar(20),EMP_NAME varchar(20));
insert into EMP values('100','BOB')
Table 1
ID | NAME | DATE |
---|---|---|
100 | BOB | 01-10-2021 |
100 | BOB | 01-11-2021 |
create table Table_1(ID varchar(20),NAME varchar(20), DATE date);
insert into Table_1 values('100','BOB','01-10-2021');insert into Table_1 values('100','BOB','01-11-2021');
Table 2
ID | NAME | DATE |
---|---|---|
100 | BOB | 01-11-2021 |
100 | BOB | 01-12-2021 |
create table Table_2(ID varchar(20),NAME varchar(20), DATE date);
insert into Table_2 values('100','BOB','01-11-2021');insert into Table_1 values('100','BOB','01-12-2021');
Table 日期
Month | DATE |
---|---|
Sep | 01-09-2021 |
Oct | 01-10-2021 |
Nov | 01-11-2021 |
Dec | 01-12-2021 |
create table DATE(Month varchar(20), DATE date);
insert into DATE values('Sep','01-09-2021');insert into DATE values('Sep','01-10-2021');insert into DATE values('Sep','01-11-2021'); insert into DATE values('Sep','01-12-2021'))
我想参考table table 3 (Date Table) 以确定 TABLE 3 记录在哪一天没有出现在 TABLE 1 和 2(在给定的情况下,日期 = 01-09-2021 的记录是预期的输出)
预期输出
ID | NAME | DATE |
---|---|---|
100 | BOB | 01-09-2021 |
试试这个:
SELECT E.ID, E.EMP_NAME, D.DATE
FROM DATE D, EMP E
WHERE
NOT EXISTS (SELECT 1 FROM Table_1 T WHERE T.ID = E.ID AND T.DATE = D.DATE)
AND NOT EXISTS (SELECT 1 FROM Table_2 T WHERE T.ID = E.ID AND T.DATE = D.DATE)
I want to refer table table 3 (Date Table) to identify on which date of TABLE 3 record did not appear in TABLE 1 and 2
如果你只是想要不出现的整体日期,你可以使用not exists
或left join
:
select d.*
from dates d
where not exists (select 1 from table1 t1 where t1.date = d.date) and
not exists (select 1 from table2 t2 where t2.date = d.date);
这就是您要问的问题。但是,您想要的结果表明您希望 employee/date 组合不出现。如果是这种情况,则 CROSS JOIN
emp
和 dates
表并过滤掉不存在的表。一种方法:
select e.*, d.*
from emp e cross join
dates d left join
table1 t1
on t1.date = d.date and t1.id = e.id left join
table2 t2
on t2.date = d.date and t2.id = e.id
where t1.id is null and t2.id is null;