日期:2014-05-17  浏览次数:20501 次

这个统计sql怎么写
图1转换到图2那种结果。。
voteValue列1代表A,2代表B,3,代表C,4代表D,5代表E

图1


图2



谢谢。。。
------解决方案--------------------
case when
------解决方案--------------------
try this,

select ListID,[1] 'A',[2] 'B',[3] 'C',[4] 'D',[5] 'E'
from
(select ListID,VoteValue,count(1) 'ct'
 from [tab] group by ListID,VoteValue) t
pivot(max(ct) for VoteValue in([1],[2],[3],[4],[5])) p

------解决方案--------------------
isnull(字段,0)
------解决方案--------------------

select ListID,
       isnull([1],0) 'A',
       isnull([2],0) 'B',
       isnull([3],0) 'C',
       isnull([4],0) 'D',
       isnull([5],0) 'E'
from
(select ListID,VoteValue,count(1) 'ct'
 from [tab] group by ListID,VoteValue) t
pivot(max(ct) for VoteValue in([1],[2],[3],[4],[5])) p