SQL如何将查询结果合并成一行数据。 在这个查询语句前应该如何处理:

select bmdm,bmmc,sum(je) as je1,0 as je2 ,0 as je3,0 as je4 from gz_gzmxb where gsdm='001001' and ffcs='11' and lbdm='01' and ffnd='2016' and gzdm='99' and mxxmdm='1' group by bmdm,bmmc union select bmdm,bmmc,0 as je1,sum(je) as je2 ,0 as je3,0 as je4 from gz_gzmxb where gsdm='001001' and ffcs='11' and lbdm='01' and ffnd='2016' and gzdm='99' and mxxmdm='2' group by bmdm,bmmc union select bmdm,bmmc,0 as je1,0 as je2 ,sum(je) as je3,0 as je4 from gz_gzmxb where gsdm='001001' and ffcs='11' and lbdm='01' and ffnd='2016' and gzdm='99' group by bmdm,bmmc union select bmdm,bmmc,0 as je1,0 as je2 ,0 as je3,sum(SDS) as je4 from gz_gzmxb where gsdm='001001' and ffcs='11' and lbdm='01' and ffnd='2016' and gzdm='99' group by bmdm,bmmc

方案1:使用PIVOT()进行行列转换
方案2:使用GROUP BY,聚合时使用SUM(CASE WHEN)
温馨提示:答案为网友推荐,仅供参考
相似回答