当想要统计的两个指标不在一张表中时,需要做关联。但很多情况下,也没有办法保证其中一张表的维度是全的,用left join或right join可能会导致数据丢失。所以借助full join处理。
如,将下面的数据处理成表格中的效果(维度就是name)。
目标效果:
name | number | show_number |
---|---|---|
A | 300 | 0.80 |
B | 100 | NULL |
C | 150 | 0.20 |
D | NULL | 0.66 |
select if(r.name is null ,r.name2,r.name) as name, -- 保留有值的namer.number,r.show_number
from
( -- 数据合并select t1.name,t1.number,t2.name as name2,t2.show_numberfrom (select 'A' as name,300 as numberunion all select 'B' ,100union all select 'C' ,150 ) as t1full join (select 'A' as name,0.8 as show_numberunion all select 'D',0.66union all select 'C',0.2) as t2 on t1.name=t2.name
) as r
结果 :