以前做过一道题目,一直没有来得及总结下来。贴图:
记得以前曾经找到了两种方法,今天试了一下,还是可以的,贴出过程:
下面是具体的查询方法:
原来放的是图片,今天又练习了一下,附代码:
1 create TABLE #Match 2 ( 3 matchDate datetime, 4 matchResult nvarchar(20) 5 ) 6 7 insert #Match select '2014-09-01','lost' 8 select '2014-09-19','win' 9 union all 10 select '2014-09-19','win' 11 union all 12 select '2014-09-19','lost' 13 union all 14 select '2014-09-01','win' 15 union all 16 select '2014-09-19','lost' 17 union all 18 select '2014-09-01','lost' 19 20 21 select * from #Match 22 23 24 --方法1 25 select m.matchDate, 26 sum(case when m.matchResult='win' then 1 else 0 end) win, 27 sum(case when m.matchResult='lost' then 1 else 0 end) lost 28 from #Match m group by m.matchDate 29 30 31 --方法2 32 select m.matchDate, 33 sum(case m.matchResult when 'win' then 1 else 0 end) win, 34 sum(case m.matchResult when 'lost' then 1 else 0 end) lost 35 from #Match m group by m.matchDate