成绩排名的问题

Code
create table tb(name varchar(10), grade int, banji int , kemu varchar(10), score int)
insert into tb values('A' , 1 , 1 , '语文' , 100)
insert into tb values('A' , 1 , 1 , '数学' , 100)
insert into tb values('A' , 1 , 1 , '英语' , 100)
insert into tb values('B' , 1 , 1 , '语文' , 99)
insert into tb values('B' , 1 , 1 , '数学' , 98)
insert into tb values('B' , 1 , 1 , '英语' , 97)
insert into tb values('C' , 1 , 1 , '语文' , 96)
insert into tb values('C' , 1 , 1 , '数学' , 95)
insert into tb values('C' , 1 , 1 , '英语' , 94)
insert into tb values('D' , 1 , 2 , '语文' , 100)
insert into tb values('D' , 1 , 2 , '数学' , 100)
insert into tb values('D' , 1 , 2 , '英语' , 100)
insert into tb values('E' , 1 , 2 , '语文' , 50)
insert into tb values('E' , 1 , 2 , '数学' , 40)
insert into tb values('E' , 1 , 2 , '英语' , 30)
drop table tb
select * from tb

declare @sql varchar(8000)
set @sql = 'select '
select @sql = @sql + ' max(case kemu when ''' + kemu + ''' then score else 0 end) [' + kemu + '] ,'
from (select distinct kemu from tb) as a
set @sql = left(@sql,len(@sql)-1+ ' from (select kemu , avg(score) score from tb group by kemu) t '
exec(@sql ) 
 
-- 年级排名和班级排名
declare @sql varchar(4000),@nclassid int,@nexamid int  select @sql='' select @nclassid=38 select @nexamid=19
select @sql=@sql+'max(case scoursename when '''+scoursename+''' then descore else 0 end) '+scoursename+','
from (select distinct scoursename from t_card_course where id in (select ncourseid from t_card_examcou where nclassid =+@nclassid and nexamid=+@nexamid )) a  
exec('
select nclassid, name  姓名,
'+ @sql+ 'sum(descore) 总分
into #temp 
from ( 
 select b.nclassid,b.name,a.descore,c.scoursename 
 from t_card_score a,t_stu b,t_card_course c,t_card_examcou d 
 where a.nstuid=b.id and a.ncouexamid in
 (select id from t_card_examcou where nclassid in (select id from t_card_class where ngradeid =(select ngradeid from t_card_class where id=
'+@nclassid+' and nschoolid=(select nschoolid from t_card_class where id='+@nclassid+')))  and nexamid='+@nexamid+' ) 
and a.ncouexamid=d.id and d.ncourseid=c.id group by b.nclassid,b.name,a.descore,c.scoursename )as a
 group by name,nclassid 
select *,班级名次=(select count(总分) from #temp where 总分>y.总分 and nclassid=y.nclassid)+1 into #tempclass from #temp y   
select *,年级名次 =(select count(总分) from #tempclass where 总分>x.总分)+1 into #tempok from #tempclass x where nclassid =
'+@nclassid+' order by 班级名次
alter table #tempok drop column nclassid
select * from #tempok
')


 
-- 具体班级各科目平均分
declare @nclassid int,@nexamid int set @nclassid=38 set @nexamid=19
exec('
select b.nclassid,b.name,a.descore,c.scoursename 
into #temp
 from t_card_score a,t_stu b,t_card_course c,t_card_examcou d 
 where a.nstuid=b.id and a.ncouexamid in
 (select id from t_card_examcou where nclassid in (select id from t_card_class where ngradeid =(select ngradeid from t_card_class where id=
'+@nclassid+' and nschoolid=(select nschoolid from t_card_class where id='+@nclassid+')))  and nexamid='+@nexamid+' ) 
and a.ncouexamid=d.id and d.ncourseid=c.id and b.nclassid=
'+@nclassid+' group by b.nclassid,b.name,a.descore,c.scoursename 
select scoursename 课程名, left(avg(descore),4) 平均分  from #temp group by scoursename
')

----各科的及格率
declare @sql varchar(4000),@nclassid int,@nexamid int,@avg varchar(8000set @sql='' set @avg='' set @nclassid=38 set @nexamid=19
select @sql=@sql+' max(case scoursename when '''+scoursename+''' then descore else 0 end) '+scoursename+','
from (select distinct scoursename from t_card_course where id in (select ncourseid from t_card_examcou where nclassid =+@nclassid and nexamid=+@nexamid )) a  
exec('
select b.nclassid,b.name,a.descore,c.scoursename 
into #temp
 from t_card_score a,t_stu b,t_card_course c,t_card_examcou d 
 where a.nstuid=b.id and a.ncouexamid in
 (select id from t_card_examcou where nclassid in (select id from t_card_class where ngradeid =(select ngradeid from t_card_class where id=
'+@nclassid+' and nschoolid=(select nschoolid from t_card_class where id='+@nclassid+')))  and nexamid='+@nexamid+' ) 
and a.ncouexamid=d.id and d.ncourseid=c.id and b.nclassid=
'+@nclassid+' group by b.nclassid,b.name,a.descore,c.scoursename 
select * from #temp
select count(descore) from #temp where scoursename=
''化学'' and descore>10
')

 

 

Code
-- 年级排名和班级排名(新加家长信息)
declare @sql varchar(4000),@nclassid int,@nexamid int  select @sql='' select @nclassid=38 select @nexamid=19
select @sql=@sql+'max(case scoursename when '''+scoursename+''' then descore else 0 end) '+scoursename+','
from (select distinct scoursename from t_card_course where id in (select ncourseid from t_card_examcou where nclassid =+@nclassid and nexamid=+@nexamid )) a  
exec('
select nclassid, name as 学生姓名,家长姓名,状态,家长手机,
'+ @sql+ 'sum(descore) 总分
into #temp 
from ( 
 select b.nclassid,b.name,a.descore,c.scoursename,(case e.state when 0 then 
''试用'' when 1 then ''正式'' when 2  then ''停用'' end) as 状态,f.name as 家长姓名,f.mobile as 家长手机 
 from t_card_score a,t_stu b,t_card_course c,t_card_examcou d,ext_formaluser e,t_par f 
 where b.id=f.nstuid and e.stugid=b.gid and a.nstuid=b.id and a.ncouexamid in
 (select id from t_card_examcou where nclassid in (select id from t_card_class where ngradeid =(select ngradeid from t_card_class where id=
'+@nclassid+' and nschoolid=(select nschoolid from t_card_class where id='+@nclassid+')))  and nexamid='+@nexamid+' ) 
and a.ncouexamid=d.id and d.ncourseid=c.id group by b.nclassid,b.name,a.descore,c.scoursename,e.state,f.name,f.mobile)as a
 group by name,nclassid,状态,家长姓名,家长手机
select *,班级名次=(select count(总分) from #temp where 总分>y.总分 and nclassid=y.nclassid)+1 into #tempclass from #temp y   
select *,年级名次 =(select count(总分) from #tempclass where 总分>x.总分)+1 into #tempok from #tempclass x where nclassid =
'+@nclassid+' order by 班级名次
alter table #tempok drop column nclassid
select * from #tempok
')
posted @ 2009-01-15 18:44  翁玉礼  阅读(836)  评论(0编辑  收藏  举报