当前位置: 代码迷 >> SQL >> 一个sql有关问题的解决
  详细解决方案

一个sql有关问题的解决

热度:10   发布时间:2016-05-05 11:53:54.0
一个sql问题的解决

表内容: ??

2005-05-09 胜 ??

2005-05-09 胜 ??

2005-05-09 负 ??

2005-05-09 负 ??

2005-05-10 胜 ??

2005-05-10 负 ??

2005-05-10 负 ?

?

输出:

? ?比赛时间 ?胜 负 ??

2005-05-09 2 2 ??

2005-05-10 1 2 ?

?

自己完成建表语句,插入语句

create table bishai(	id int(11) AUTO_INCREMENT, 	time varchar(64),	fengshu int(4),	primary key(id));insert into bishai(time,fengshu) values('2005-05-09','胜'),('2005-05-09','胜'),('2005-05-09','负'),('2005-05-09','负'),('2005-05-10','胜'),('2005-05-10','负'),('2005-05-10','bishai负');

?注意这个地方,使用了多个values,使用带有多个VALUES列表的INSERT语句一次插入几行这将比使用一个单行插入语句快几倍。

我的sql如下:

select time as '比赛时间', sum(case  when fengshu = '胜' then 1 else 0 end) '胜',sum(case when fengshu = '负' then 1 else 0 end) '负'from bishaigroup by timeorder by time;

?

1 楼 asialee 前天  
oracle中可以用decode函数:
select time,sum(score1),sum(score2) from (select time,decode(fengshu,'胜',1,0) score1,decode(fengshu,'负',1,0) score2 from a) group by time
  相关解决方案