将表重新规整排序的问题
jgm kz sl rq
0102010580 p 500 2007-7-5 0:00:00
0102010580 z 100 2007-7-5 0:00:00
0102010580 f 50 2007-7-5 0:00:00
0103020525 p 1000 2007-8-5 0:00:00
0103020525 z 250 2007-8-5 0:00:00
0103020525 f 100 2007-8-5 0:00:00
0103020525 p 1000 2007-5-1 0:00:00
0102010580 z 250 2007-5-25 0:00:00
0103040535 p 500 2007-8-20 0:00:00
0103040535 z 100 2007-8-20 0:00:00
0103040535 f 50 2007-8-20 0:00:00
0103040535 f 50 2007-5-20 0:00:00
我想得到下面的结果
jgm kz(p)sl kz(z)sl kz(f)sl
0102010580 500 350 50
0103020525 2000 250 100
0103040535 500 100 100
得到一个jgm下kz为p,为z,为f的总的数量sl
------解决方案--------------------加上條件
Select
jgm,
SUM(Case kz When 'p ' Then sl Else 0 End) As [kz(p)sl],
SUM(Case kz When 'z ' Then sl Else 0 End) As [kz(z)sl],
SUM(Case kz When 'f ' Then sl Else 0 End) As [kz(f)sl]
From
表
Where
rq Between '2007-06-30 ' And '2007-09-30 '
Group By
jgm
或者
Select
jgm,
[kz(p)sl] = SUM(Case When kz = 'p ' Then sl Else 0 End),
[kz(z)sl] = SUM(Case When kz = 'z ' Then sl Else 0 End),
[kz(f)sl] = SUM(Case When kz = 'f ' Then sl Else 0 End)
From
表
Where
rq Between '2007-06-30 ' And '2007-09-30 '
Group By
jgm