zoukankan      html  css  js  c++  java
  • 成绩排名的问题

    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
    ')
  • 相关阅读:
    week8-A-区间选点
    C-TT的美梦
    B-dijkstra-TT的旅行日记
    week7-A-Floyd-TT的魔法猫
    Codeforces 437B The Child and Set (贡献+贪心)
    Codeforces 1236B Alice and the List of Presents(贡献)
    2019 牛客多校 第八场 B Beauty Values (贡献)
    Codeforces 1244G Running in Pairs 序列交换(贪心)
    Codeforces 1244F Chips(环修改,思维题)
    BZOJ 3784 树上的路径(点分治+ST+堆+贪心)待处理
  • 原文地址:https://www.cnblogs.com/wengyuli/p/1376562.html
Copyright © 2011-2022 走看看