在 【传智播客.Net培训—SQL】10流控函数.avi 中最后面 有一道习题 如下:
有一张表:
SDate Name Score
2008-08-08 拜仁 胜
2008-08-09 奇才 胜
2008-08-09 湖人 胜
2008-08-10 拜仁 负
2008-08-08 拜仁 负
2008-08-12 奇才 胜
要求输出如下:
Name 胜 负
拜仁 1 2
湖人 1 0
奇才 2 0
我是通过先建立VIEW 再从VIEW中进行查询- create view view_win as
- select name,count(*) as '胜',0 as '负' from T_Scores where Score=N'胜' group by Name;
- create view view_fail as
- select name,0 as '胜',count(*) as '负' from T_Scores where Score=N'负' group by Name;
- select view_win.Name,view_win.胜,isnull(view_fail.负,0)as '负' from view_win left join view_fail on view_win.Name=view_fail.Name
复制代码 但是感觉太过复杂,看视频里老师的讲法,应该是有更方便的实现,那应该如何进行查询?? |