比如表A 
学号 姓名
330  小张
331  小王
332 小陈
预想结果序号 学号 姓名
1     330  小张
2     331  小王
3     332 小陈
如果是SQL 2005以上应该可以写成
select row_number() (over order by 学号)  as 序号,学号,姓名 from A 
现在2000的该如何写呢?

解决方案 »

  1.   

    临时表加一个统计的字段
    select *,rid=identity(int,1,1)
    into #temp
    from a
    order by 姓名,学号select *,px=(select count(*) from #temp where 姓名=t.姓名 and rid<=t.rid)
    from #temp t
      

  2.   


    create table 表A
    (学号 int, 姓名 varchar(8))insert into 表A
    select 330, '小张' union all
    select 331, '小王' union all
    select 332, '小陈'
    select identity(int,1,1) '序号',学号,姓名
    into #t
    from 表Aselect * from #t序号          学号          姓名
    ----------- ----------- --------
    1           330         小张
    2           331         小王
    3           332         小陈(3 row(s) affected)