select * from (
select indexno=(select count(*) from tb where f1<=a.f1),a.f1,a.f2
from tb a ) aa order by indexno

解决方案 »

  1.   

    如果f1唯一:select (select count(1) from t1 where f1<=t.f1) as indexno
           ,f1
           ,f2
    from t1 t
    order by f1
      

  2.   

    select
        indexno = (selcet count(*) from t1 where f1<=a.t1),
        a.f1,
        a.f2
    from
        t1 a
    order by
        a.f1
      

  3.   

    如果f1不唯一,还真的必须用临时表或者其他方法为每一列排序才能做!楼上的(selcet count(*) from t1 where f1<=a.t1),
    应该是(select count(*) from t1 where f1<=a.f1)
      

  4.   

    --try F1+F2 如果唯一
    select 序号=(select sum(1) from t1 where convert(varchar(10),f1)+convert(varchar(20),f2)<=
     convert(varchar(10),a.f1)+convert(varchar(20),a.f2)),
    f1,
    f2
    from t1 a
      

  5.   

    --测试环境
    declare @t table (f1 int, f2 varchar(20))
    insert into @t select 21,'A1'
    union all select 19,'A6'
    union all select 40,'A4'
    union all select 20,'A6'--查询语句
    select 序号=(select sum(1) from @t where convert(varchar(10),f1)+convert(varchar(10),f2)<=
    convert(varchar(10),a.f1)+convert(varchar(10),a.f2)),
    f1,f2
    from @t a
    order by 序号--结果
    序号          f1          f2                   
    ----------- ----------- -------------------- 
    1           19          A6
    2           20          A6
    3           21          A1
    4           40          A4(所影响的行数为 4 行)