一道sql面试题

如果要生成下列结果, 该如何写sql语句?

            胜 负
2005-05-09 2 2
2005-05-10 1 2
------------------------------------------
create table tmp(rq varchar2(10),shengfu char(2))

insert into tmp values('2005-05-09','胜');
insert into tmp values('2005-05-09','胜');
insert into tmp values('2005-05-09','负');
insert into tmp values('2005-05-09','负');
insert into tmp values('2005-05-10','胜');
insert into tmp values('2005-05-10','负');
insert into tmp values('2005-05-10','负');
解决方案一:
1.select a.rq as " ",a.s as 胜,b.f as 负 from (select rq,count(*) as s from tmp where shengfu='胜' group by rq) a inner join (select rq,count(*) as f from tmp where shengfu='负' group by rq) b
on a.rq=b.rq;

方案二:

2.select rq "  ", count(decode(t.shengfu,'胜',1)) 胜,count(decode(t.shengfu,'负',1)) 负 from tmp t group by rq

说明:decode是oracle中的一个函数!

猜你喜欢

转载自gaoquanyang.iteye.com/blog/1388955