在进行外航订座分析的时候,需要同时分析某个office的多个指标,主要分布在三个表里:出票(A表),出票取消(B表),订座(C表)。主键是office和年月。有的office存在A表,有的在B表,有的在C表。那么我想取的所有office的在ABC表里的指标项,也就是把几个小表拉平成一个大表。
最直接的做法是外连接,但是我用greenplum数据库用outer join的时候,发现有的office还是没有进到结果里。
最后采取了一个比较笨的方法:
select distinct
AA.year
,AA.month
,AA.office
,a.tkt_qty
,b.cncl_tkt_qty
,c.bk_seg
(
select a.year,a.month,a.office from a
union
select b.year,b.month,b.office from b
union
select c.year,c.month,c.office from c
)
AA
left outer join a
on AA.year=a.year and AA.month=a.month and AA.office=a.office
left outer join b
on AA.year=b.year and AA.month=b.month and AA.office=b.office
left outer join c
on AA.year=c.year and AA.month=c.month and AA.office=c.office
还有要注意的是:不同的表的office字段可能有的没有trim 导致关联问题,一定要先检查下!!!