SQL笔试题
1.统计查询SQL练习
数据库中表结构如下,字段分别任rg(日期),shengfu(胜负),考察group by 语句的使用:
2005-05-09 胜
2005-05-09 胜
2005-05-09 负
2005-05-09 负
2005-05-10 胜
2005-05-10 负
2005-05-10 负
如果要生成下列结果, 该如何写sql 语句?
胜负
2005-05-09 2 2
2005-05-10 1 2
答案:
1)select rq, sum(case when shengfu='胜' then 1 else 0 end)'胜',sum(case when shengfu='负'
then 1 else 0 end)'负' from #tmp group by rq
2) select N.rq,N.胜,M.负from (
select rq,胜=count(*) from #tmp where shengfu='胜'group by rq)N inner join