背景:对投票的结果按照单位进行汇总统计,数据库中表记录的各个账号对各个选项的投票记录.马上想到一个解决方案,先根据单位和选项进行Group By,然后再行转列得出单位对各个选项的投票情况. with list as ( select GroupId,ANSWER,count(1) as num from Vote GROUP BY GroupId,ANSWER ) select * from list pivot(sum(num) for Answer in (1,2,3,4,5,6)) pvt