關于group by 的應用問題
數據庫內容為下面

寫一SQL得出下面內容:

貼出SQL結果如下:(MySQL版本)
create table gosin_temp(rq varchar(10),shengfu nchar(1));
insert into gosin_temp values('2009-05-09','勝');
insert into gosin_temp values('2009-05-09','勝');
insert into gosin_temp values('2009-05-09','負');
insert into gosin_temp values('2009-05-09','負');
insert into gosin_temp values('2009-05-10','勝');
insert into gosin_temp values('2009-05-10','負');
insert into gosin_temp values('2009-05-10','負');
select * from gosin_temp;
得到結果的SQL:
select a1.rq,a1.勝,b1.負 from
(select a.rq, count(a.shengfu) 勝 from gosin_temp a where a.shengfu='勝' group by a.rq) a1,
(select b.rq, count(b.shengfu) 負 from gosin_temp b where b.shengfu='負' group by b.rq) b1
where a1.rq = b1.rq
類似的題目還有很多,如:
勝 負
1 a b
2 b a
3 b a
要求寫一SQL語句,輸出如下結果:
勝 負
a 1 2
b 2 1
其實都一樣 只要熟悉使用group by 就不覺得難了。