表结构:id name value
a1  a    4.0
a2  b    2.0
a3  c    3.0按照value排序后的结果:id name value
a2  b    2.0
a3  c    3.0
a1  a    4.0最终希望返回的结果:id name value 排名
a2  b    2.0     1
a3  c    3.0     2
a1  a    4.0     3即,插入一列,该列的内容是当前该记录在结果集的排名(也可以是0,1,2)。
请问这样的sql如何写?谢谢。

解决方案 »

  1.   

    select *,(select count(*) from tb where [value]>=t.[value])=排名 from tb t
      

  2.   

    select *,rank() over(patition by name  order by value desc) from tb
    应该语法是这样的
    2005/2008才行的,2000用临时表可以解决
      

  3.   


    select *,排名=(select count(1)+1 from tb a where a.value<value)
    from tb
    order by value desc
      

  4.   

    select * ,px=(select count(1) from tb where value<=a.value) from tb a  order by value
      

  5.   

    SELECT * ,
    (SELECT COUNT(*) FROM TB WHERE [value]<=T.[value ] ) AS 排名 
    FROM TB T 
    ORDER BY (SELECT COUNT(*) FROM TB WHERE [value]<=T.[value ] )
      

  6.   

    select 
    排名=(select COUNT(*) from tb where T.value<=value),
    *
    from tb  t
    order by value
      

  7.   


    /***********************************************--> 测试数据:[tb]
    --> 测试时间:2009-08-10 16:58:51
    --> 我的淘宝:<<戒色坊>> http://shop36766744.taobao.com/***********************************************/if object_id('[tb]') is not null drop table [tb]
    create table [tb]([id] varchar(2),[name] varchar(1),[value] numeric(2,1))
    insert [tb]
    select 'a1','a',4.0 union all
    select 'a2','b',2.0 union all
    select 'a3','c',3.0select *,flag=(select count(id)+1 from tb where t.value>value) from [tb]t
    order by cast(value as int)/*
    id   name value                                   flag
    ---- ---- --------------------------------------- -----------
    a2   b    2.0                                     1
    a3   c    3.0                                     2
    a1   a    4.0                                     3(3 行受影响)
    */drop table TB
      

  8.   

    select *,排名 =(select count(*) from tb where [value]>=t.[value])from tb t order by value
      

  9.   

    select 
    排名=(select COUNT(*) from tb where T.value>=value),
    *
    from tb  t
    order by value
      

  10.   

    select *,排名 =(select count(1)+1 from tb where [value]>=t.[value])from tb t order by value
      

  11.   

    if object_id('[tb]') is not null 
    drop table [tb]
    go
    create table [tb]([id] varchar(2),[name] varchar(1),[value] numeric(2,1))
    insert [tb]
    select 'a1','a',4.0 union all
    select 'a2','b',2.0 union all
    select 'a3','c',3.0select 
    排名=(select COUNT(*) from tb where T.value>=value),
    *
    from tb  t
    order by value
    /*
    排名          id   name value
    ----------- ---- ---- ---------------------------------------
    1           a2   b    2.0
    2           a3   c    3.0
    3           a1   a    4.0
    */
      

  12.   

    --反了
    declare @t table(id varchar(10), [name] varchar(10), [value] dec(10,2)) 
    insert @t select 'a1'  ,'a',    4.0 
    insert @t select 'a2' , 'b' ,   2.0 
    insert @t select 'a3',  'c'  ,  3.0 
    select *,(select count(*) from @t where [value]<=t.[value])as 排名 from @t t
    order by [value]
    /*id         name       value                                   排名
    ---------- ---------- --------------------------------------- -----------
    a2         b          2.00                                    1
    a3         c          3.00                                    2
    a1         a          4.00                                    3(3 個資料列受到影響)*/
      

  13.   

    if object_id('[tb]') is not null drop table [tb]
    create table [tb]([id] varchar(2),[name] varchar(1),[value] numeric(2,1))
    insert [tb]
    select 'a1','a',4.0 union all
    select 'a2','b',2.0 union all
    select 'a3','c',3.0SELECT * ,
    (SELECT COUNT(*) FROM TB WHERE [value]<=T.[value ] ) AS 排名 
    FROM TB T 
    ORDER BY (SELECT COUNT(*) FROM TB WHERE [value]<=T.[value ] )(所影响的行数为 3 行)id   name value 排名          
    ---- ---- ----- ----------- 
    a2   b    2.0   1
    a3   c    3.0   2
    a1   a    4.0   3(所影响的行数为 3 行)
      

  14.   

    if OBJECT_ID('tt') is not null drop table tt
    create table tt(id varchar(10),name varchar(10),value decimal(14,2))
    insert tt
    select 'a1',  'a',    4.0 
    union all select 'a2',  'b',    2.0 
    union all select 'a3',  'c',    3.0select *,排序=row_number() over(order by value) from tt 
    select *,排序=rank() over(order by value) from tt /*
    id name value 排序
    a2 b 2.00 1
    a3 c 3.00 2
    a1 a 4.00 3
    */