求两表合并的SQL,需求如下:
AA表
区域,产品,规格,季度1销售
area,product,spec,sale1
bb表,
区域,产品,规格,季度2销售
area,product,spec,sale2
cc表,产品总表
product,spec
求:(各个季度销售的产品有可能不一样)
区域,产品,规格,季度1销售,季度2销售
area,product,spec,sale1,sale2
------解决方案--------------------select area,product,spec,sum(case when 季=1 then 季销 else 0 end)季度1销售,
sum(case when 季=2 then 季销 else 0 end)季度2销售 from
(select area,product,spec,sale1 as 季销,1 季 from AA union all select *,2 from BB)XX
group by area,product,spec