`

一个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 bishai
group by time
order by time;

 

分享到:
评论
1 楼 asialee 2013-12-04  
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

相关推荐

Global site tag (gtag.js) - Google Analytics