【发布时间】:2020-12-16 08:40:04
【问题描述】:
我有3张表,一张是只包含日期的datetable,另外2张的数据如下。
日期表:
表1:
表2:
我正在使用日期表进行左连接,如下所示:
select * from
(select distinct t.d,
coalesce(tab1.name,tab2.name,"") as name,
coalesce(tab1.id,tab2.id,"") as id,
coalesce(tab1.tgt_cnt,0) as tgt_cnt,
coalesce(tab2.a_cnt,0) as a_cnt,
coalesce(tab2.b_cnt,0) as b_cnt,
coalesce(tab2.c_cnt,0) as v_cnt
from datetable t
LEFT JOIN (select * from table1) tab1 on t.d = tab1.dt
LEFT JOIN (select * from table2) tab2 on t.d = tab2.dt) a
where (tgt_cnt <> 0 or a_cnt <> 0 or b_cnt <> 0 or c_cnt <> 0);
我得到以下结果。
我的问题是记录 TOM 发生了什么。我不确定为什么 CG 和 Bob 重复两次。我的查询有问题吗。
能否请您指出为什么 TOM 记录没有进入 Left Join 以及为什么 CG 和 BOB 重复。
非常感谢您的帮助。
谢谢,巴布
【问题讨论】: