我正在努力实现 SQL 查询,以根据分组方案从 2 个 HIVE 表中识别丢失的记录。数据如下
表 1 - 日历
month_last_day
20190131
20190229
20190331
20190430
表 2 - 项目
itemid date
101 20190131
101 20190229
101 20190331
102 20190131
102 20190331
102 20190430
上面表格中的日历是一个包含所有日期的主表,项目表包含不同项目 ID 的数据,主表中的某些日期缺失。例如,itemid 101 缺少日期 20190430,缺少 102 缺少日期 20190229。
我需要将 2 行呈现为 101 20190430 和另一行 102 20190229 的输出。
我尝试过右外连接,存在概念但没有任何工作,因为需要对分组记录进行过滤。请建议。
cross join
日历到不同的项目和left join
项目表以获取丢失的行。
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
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句