数据表:
某年级的考试表(仅一个年级) 下面空行仅为醒目,无需考虑学号 班级 语文 数学
1    01   80    88
2    01   90    89
3    01   80    784    02   70    79
5    02   60    88
6    02   70    60查询结果为:
             
学号 班级 语文 语文班级名次 语文年级名次  数学 数学班级名次   数学年级名次
1    01   80         3              3       88        略...                略...
2    01   90         1              1       89
3    01   90         1              1       78        略...                略...4    02   70         1              4       79
5    02   60         3              6       88
6    02   70         1              4       60不考虑 班级名次 下面语句可以:
ADOQuery1.SQL.Text:=
'select 学号,班级,语文,语年级名次=(select isnull(count(*),0)+1 from a1 as t2 where t1.语文<t2.语文)'+
           ',数学,数学年级名次=(select isnull(count(*),0)+1 from a1as t2 where t1.数学<t2.数学)'+
' from a1 as t1 ';问题是考虑班级名次怎么实现?(可以考虑建临时表)

解决方案 »

  1.   

    參考--只用一句SQL计算名次,允许并列
    --方法改变世界
    --create table sc(name varchar(50),subj varchar(50), float,ord int)--参数:
    --sc   表名
    --name 人名
    -- 分数
    --ord  名次
    --1、当名次合并后,下一名次按人数加(在她前面有多少人,名次是不连续的)
    update sc set ord=(select count(*)+1 from sc B where B.>sc.)--2、当名次合并后,下一名次加1(名次总是连续的)
    update sc set ord=(select count(*) from (select distinct  from sc) as dist where dist. >=sc.)--不用循环,简单搞定SQL交叉表
    --参数同上
    create proc usp_cross
    as
    declare @s varchar(8000)
    select @s='select name'
    select @s=@s+',sum(case subj when '''+subj+''' then  end) as ['+subj+']'
           from (select distinct subj from sc) as distSj
    select @s=@s+' from sc group by name'
    print @s
    exec(@s) 
      

  2.   

    格式應該為SQL:--只用一句SQL计算名次,允许并列
    --方法改变世界
    --create table sc(name varchar(50),subj varchar(50), float,ord int)
    --参数:
    --sc   表名
    --name 人名
    -- 分数
    --ord  名次
    --1、当名次合并后,下一名次按人数加(在她前面有多少人,名次是不连续的)
    update sc set ord=(select count(*)+1 from sc B where B.>sc.)--2、当名次合并后,下一名次加1(名次总是连续的)
    update sc set ord=(select count(*) from (select distinct  from sc) as dist where dist. >=sc.)--不用循环,简单搞定SQL交叉表
    --参数同上
    create proc usp_cross
    as
    declare @s varchar(8000)
    select @s='select name'
    select @s=@s+',sum(case subj when '''+subj+''' then  end) as ['+subj+']'
           from (select distinct subj from sc) as distSj
    select @s=@s+' from sc group by name'
    print @s
    exec(@s)