一道 sql 语句面试题,有关 group by表内容:-05-09 胜-05-09 胜-05-09 负-05-09 负-05-10 胜-05-10 负-05-10 负假如要生成下列成果, 该怎样写 sql 语句
胜 负-05-09 2 2-05-10 1 2------------------------------------------create table #tmp(rq varchar(10),shengfu nchar(1))insert into #tmp values(-05-09,胜)insert into #tmp values(-05-09,胜)insert into #tmp values(-05-09,负)insert into #tmp values(-05-09,负)insert into #tmp values(-05-10,胜)insert into #tmp values(-05-10,负)insert into #tmp values(-05-10,负)1)select rq, sum(case when shengfu=胜 then 1 else 0 end)胜,sum(case when shengfu=负 then 1 else 0 end)负 from #tmpgroup by rq2)select n
負 from (select rq,勝=count(*) from #tmp where shengfu=胜 group by rq)n inner join (select rq,負=count(*) from #tmp where shengfu=负 group by rq)m on n
rq3)select a
a1 胜,b
b1 负 from (select rq,count(