日期:2014-05-18  浏览次数:20463 次

求SQL语句合并查询结果
数据标准为:
Select   A01.A0128   as   '部门 '
,count(*)   as   '人数 '
,sum(GZ02.GZ0201)   as   '基本薪金 '
,sum(GZ02.GZ0204)   as   '总加班费 '
,sum(GZ02.GZ0208)   as   '总津贴 '
,sum(GZ02.GZ0211)   as   '总奖金 '
,sum(GZ02.GZ0216)   as   '缺勤扣减薪金 '
,sum(GZ02.GZ0219)   as   '调整更改 '
,sum(GZ02.GZ0218)   as   '社会保险公司 '
,sum(GZ02.GZ0213)   as   '住房公积金(公司) '
,sum(GZ02.GZ0247)   as   '红海劳务费 '
from   GZ02
,A01
Where
(GZ02.A0188=A01.A0188)
and
left(gz02.gz_ym,4)   =     :统计年份  
and
right(gz02.gz_ym,2)= '01 '
group   by   A01.A0128

上面是一个月的记录,但是我想得到一年的记录,按月顺序依次往后排~

结果就像

,sum(GZ02.GZ0201)   as   '基本薪金 '
,sum(GZ02.GZ0204)   as   '总加班费 '
,sum(GZ02.GZ0208)   as   '总津贴 '
,sum(GZ02.GZ0211)   as   '总奖金 '
,sum(GZ02.GZ0216)   as   '缺勤扣减薪金 '
,sum(GZ02.GZ0219)   as   '调整更改 '
,sum(GZ02.GZ0218)   as   '社会保险公司 '
,sum(GZ02.GZ0213)   as   '住房公积金(公司) '
,sum(GZ02.GZ0247)   as   '红海劳务费 '
--后面接着
,sum(GZ02.GZ0201)   as   '基本薪金2月 '
,sum(GZ02.GZ0204)   as   '总加班费2月 '
,sum(GZ02.GZ0208)   as   '总津贴2月 '
,sum(GZ02.GZ0211)   as   '总奖金 '
,sum(GZ02.GZ0216)   as   '缺勤扣减薪金2月 '
,sum(GZ02.GZ0219)   as   '调整更改2月 '
,sum(GZ02.GZ0218)   as   '社会保险公司2月 '
,sum(GZ02.GZ0213)   as   '住房公积金(公司)2月 '
,sum(GZ02.GZ0247)   as   '红海劳务费2月 '
有没有什么直接的语句可以实现啊?不通过临时表



------解决方案--------------------
Select A01.A0128 as '部门 '
,count(*) as '人数 '
,sum(case when right(gz02.gz_ym,2)= '01 ' then GZ02.GZ0201 else 0 end) as '基本薪金1月 '
,sum(case when right(gz02.gz_ym,2)= '01 ' then GZ02.GZ0204 else 0 end) as '总加班费1月 '
,sum(case when right(gz02.gz_ym,2)= '01 ' then GZ02.GZ0208 else 0 end) as '总津贴1月 '
,sum(case when right(gz02.gz_ym,2)= '01 ' then GZ02.GZ0211 else 0 end) as '总奖金1月 '
,sum(case when right(gz02.gz_ym,2)= '01 ' then GZ02.GZ0216 else 0 end) as '缺勤扣减薪金1月 '
,sum(case when right(gz02.gz_ym,2)= '01 ' then GZ02.GZ0219 else 0 end) as '调整更改1月 '
,sum(case when right(gz02.gz_ym,2)= '01 ' then GZ02.GZ0218 else 0 end) as '社会保险公司1月 '
,sum(case when right(gz02.gz_ym,2)= '01 ' then GZ02.GZ0213 else 0 end) as '住房公积金(公司)1月 '
,sum(case when right(gz02.gz_ym,2)= '01 ' then GZ02.GZ0247 else 0 end) as '红海劳务费1月 '

,sum(case when right(gz02.gz_ym,2)= '02 ' then GZ02.GZ0201 else 0 end) as '基本薪金2月 '
,sum(case when right(gz02.gz_ym,2)= '02 ' then GZ02.GZ0204 else 0 end) as '总加班费2月 '
,sum(case when right(gz02.gz_ym,2)= '02 ' then GZ02.GZ0208 else 0 end) as '总津贴2月 '
,sum(case when right(gz02.gz_ym,2)= '02 ' then GZ02.GZ0211 else 0 end) as '总奖金2月 '
,sum(case when right(gz02.gz_ym,2)= '02 ' then GZ02.GZ0216 else 0 end) as '缺勤扣减薪金2月 '
,sum(case when right(gz02.gz_ym,2)= '02 ' then GZ02.GZ0219 else 0 end) as '调整更改2月 '
,sum(case when right(gz02.gz_ym,2)= '02 ' then GZ02.GZ