通过分组查找丢失的记录

Find missing records with grouping

我正在努力实施 SQL 查询,以根据分组情况从 2 个 HIVE table 中识别丢失的记录。数据如下

Table 1 - 日历

month_last_day
20190131
20190229
20190331
20190430


Table 2 - 项目

itemid date
101    20190131
101    20190229
101    20190331
102    20190131
102    20190331
102    20190430

上面的日历 tables 是一个主日历 table 包含所有日期和项目 table 包含不同项目 ID 的数据,主 table 中的一些日期不见了。例如,itemid 101 缺少日期 20190430,而 102 缺少 20190229。

我需要将 2 行呈现为 101 20190430 和另一行 102 20190229 的输出。

我已经尝试过右外连接,存在概念但没有任何效果,因为需要对分组记录进行过滤。请提出建议。

cross join 日历以区分项目和 left join 项目 table 以获取缺失的行。

select i.itemid,c.month_last_day
from calendar c 
cross join (select distinct itemid from items) i
left join items it on it.itemid = i.itemid and c.month_last_day = it.dt
where it.dt is null 

在 hive 中查询,使用交叉连接和左外连接。

with calendar as 
(select '20190131' last_day union all
 select '20190229' last_day union all
 select '20190331' last_day union all
 select '20190430' 
) 
,items as 
(select 101 itemid,'20190131' dt union all
 select 101 itemid,'20190229' dt union all
 select 101 itemid,'20190331' dt union all
 select 102 itemid,'20190131' dt union all
 select 102 itemid,'20190331' dt union all
 select 102 itemid,'20190430' dt
),
res1 as 
(select i.itemid, c.last_day from calendar c, (select distinct itemid from items) i)

select res1.itemid, res1.last_day from res1 left outer join items i on res1.itemid = i.itemid and res1.last_day=i.dt where i.dt is null;