本帖最后由 1550425634 于 2018-3-26 17:52 编辑
先B表和C表实现外连接,再与A表内连接
select cid,collect_set(bid) as bids,collect_set(case when isnotnull(uid) then uid end as uu) as uids from (select a.cid cid, bc.bid bid, bc.uid uid from t_a a left join (select b.cid cid,b.bid bid,c.uid uid from t_b b left outer join t_c c on (b.bid=c.oid)) as bc on (a.cid=bc.cid)) as abc;
连接前的B表
1 1
1 2
2 3
3 4
3 5
3 6
连接前的C表
1 1
3 2
5 3
6 4
连接前的A表
1 苹果
2 梨子
3 核桃
连接之后的abc表
1 1 1
1 2 NULL
2 3 2
3 4 NULL
3 5 3
3 6 4
之后再求集合;主要是uid,这里只合并了uid,bid合并比较简单:
select cid,collect_set( case when isnotnull (uid) then uid end) as uids from abc group by cid;
结果如下:
1 [1]
2 [2]
3 [3,4]
先对B表和C表外连接效率比先对A表和B表进行连接效率要高;
|