日期:2014-05-17  浏览次数:20840 次

一道sql面试题目,有哪个高手写得出来,帮忙下
create table test2(
  tid integer primary key,
  ttime date,
  results varchar2(50)
)

create sequence test2_seq;
insert into test2 values(test2_seq.nextval,'1-3月-2012','赢');
insert into test2 values(test2_seq.nextval,'2-3月-2012','败');
insert into test2 values(test2_seq.nextval,'1-3月-2012','赢');
insert into test2 values(test2_seq.nextval,'2-3月-2012','败');
insert into test2 values(test2_seq.nextval,'1-3月-2012','赢');
insert into test2 values(test2_seq.nextval,'1-3月-2012','败');

查询后为:
  时间 赢 输
  2012/3/1 3 1
  2012/3/2 0 2

哪个高手帮忙写下。感激~!

------解决方案--------------------
SQL code

select to_char(ttime, 'yyyy/mm/dd') "时间",
       sum(decode(results, '赢', 1, 0)) "赢",
       sum(decode(results, '败', 1, 0)) "败"
  from test2
 group by ttime;

时间                赢          败
---------- ---------- ----------
2012/03/01          3          1
2012/03/02          0          2