有一张学生表:stuInfo(id,name,check,evaluate)分别是学生的id,姓名,考核项,考核内容;多个老师可以对同一个学生考核相同的内容;
如图:
现在是需要对考核的内容进行分类统计,显示效果如图:
由于对数据库这一窍不通,所以想请问大家这个SQL语句怎么写?非常感谢!
思路是对name、check进行分组统计,其中evaluate固定是“优良差”中的一个,那么可以通过case进行判断后统计
select name, check, count(*), sum(case when evaluate='优' then 1 else 0 end,sum(case when evaluate='良' then 1 else 0 end,sum(case when evaluate='差' then 1 else 0 end
from stuInfo
group by name, check
非常感谢您的回答,效果很好。
select name 姓名, check 考核项, count(*) 考核总数,
sum(decode(evaluate,'优',1,0)) 优,
sum(decode(evaluate,'良',1,0)) 良,
sum(decode(evaluate,'差',1,0)) 差,
from stuInfo
group by name, check
这个方法使用于oracle数据库中,sqlserver的好像不行,不过写的很对,很感谢
1 create table stuinfo( 2 id int identity(1,1), 3 name nvarchar(200), 4 [check] nvarchar(200), 5 evaluate nvarchar(200), 6 ) 7 insert into stuinfo(name,[check],evaluate) values('张三','学习','优') 8 insert into stuinfo(name,[check],evaluate) values('张三','学习','优') 9 insert into stuinfo(name,[check],evaluate) values('张三','纪律','优') 10 insert into stuinfo(name,[check],evaluate) values('张三','纪律','良') 11 insert into stuinfo(name,[check],evaluate) values('张三','学习','差') 12 insert into stuinfo(name,[check],evaluate) values('张三','学习','优') 13 insert into stuinfo(name,[check],evaluate) values('李四','纪律','优') 14 insert into stuinfo(name,[check],evaluate) values('李四','学习','优') 15 insert into stuinfo(name,[check],evaluate) values('李四','纪律','良') 16 insert into stuinfo(name,[check],evaluate) values('李四','学习','差') 17 insert into stuinfo(name,[check],evaluate) values('李四','纪律','优') 18 insert into stuinfo(name,[check],evaluate) values('李四','学习','良') 19 20 21 select * from stuinfo 22 23 24 select name as 姓名,[check] as 考核项, 25 (select COUNT(*) from stuinfo as total where total.name=s.name and total.[check]=s.[check] ) as 考核总数, 26 (select COUNT(*) from stuinfo as you where you.name=s.name and you.[check]=s.[check] and you.evaluate='优') as 优, 27 (select COUNT(*) from stuinfo as liang where liang.name=s.name and liang.[check]=s.[check] and liang.evaluate='良') as 良, 28 (select COUNT(*) from stuinfo as cha where cha.name=s.name and cha.[check]=s.[check] and cha.evaluate='差') as 差 from stuinfo as s 29 group by s.name,s.[check]
真是麻烦您了,还把表创建出来,虽然语句有些多,但是也很实用,非常感谢
好像 优良差 的个数没有显示啊