有这样一张节目投票表 tblvote(id是自动增长列)id     节目    得票数
1     花好月圆    4
2     一生有你    4
3     柠檬树      2
4     加洲旅馆    3
5     我爱英语    3现在要通过一条sql语句实现这样的效果:得到排名数、节目名称、得票数,得票数相同的节目,它们的排名是一样的,
即: 排名    节目       得票数
      1    花好月圆      4
     1    一生有你      4
     2    加洲旅馆      3
     2    我爱英语      3
     3    柠檬树        2请各位大哥大姐给小弟指点指点.

解决方案 »

  1.   

    create table tblvote(id int,节目 varchar(30),得票数 int) 
    insert tblvote
    select 1           ,'花好月圆',         4 
    union select 2           ,'一生有你',         4 
    union select 3           ,'柠檬树',             2 
    union select 4           ,'加洲旅馆',         3 
    union select 5           ,'我爱英语',         3 select 排名=(select count(1)+1 from (select distinct 得票数 from tblvote) t where a.得票数<t.得票数),节目,得票数
    from tblvote a
    order by 排名
    drop table tblvote
      

  2.   

    select * ,(select count(distinct 得票数) from tb where 得票数>=a.得票数) from tb a
      

  3.   

    create table tblvote(id int,节目 varchar(30),得票数 int) 
    insert tblvote
    select 1           ,'花好月圆',         4 
    union select 2           ,'一生有你',         4 
    union select 3           ,'柠檬树',             2 
    union select 4           ,'加洲旅馆',         3 
    union select 5           ,'我爱英语',         3 select 排名=(select count(1)+1 from (select distinct 得票数 from tblvote) t where a.得票数<t.得票数),节目,得票数
    from tblvote a
    order by 排名drop table tblvote
    /*    结果
    排名          节目                             得票数         
    ----------- ------------------------------ ----------- 
    1           花好月圆                           4
    1           一生有你                           4
    2           加洲旅馆                           3
    2           我爱英语                           3
    3           柠檬树                             2(所影响的行数为 5 行)
    */
      

  4.   

    declare @t table(id int,节目 varchar(20),得票数 int)
    insert @t select 1,'花好月圆',4 
    insert @t select 2,'一生有你',4 
    insert @t select 3,'柠檬树',2 
    insert @t select 4,'加洲旅馆',3 
    insert @t select 5,'我爱英语',3 
    select * ,[count] =(select count(distinct 得票数)+1 from @t where a.得票数 > 得票数 )
    from @t a
    order by 得票数 desc
    /*id          节目                   得票数         count       
    ----------- -------------------- ----------- ----------- 
    1           花好月圆                 4           3
    2           一生有你                 4           3
    4           加洲旅馆                 3           2
    5           我爱英语                 3           2
    3           柠檬树                  2           1*/
      

  5.   


    declare @t table(id int,节目 varchar(20),得票数 int)
    insert @t select 1,'花好月圆',4 
    insert @t select 2,'一生有你',4 
    insert @t select 3,'柠檬树',2 
    insert @t select 4,'加洲旅馆',3 
    insert @t select 5,'我爱英语',3 select rank() over (order by 得票数 desc) as 排名 , 节目,得票数 from @t/*
    排名                   节目                   得票数
    -------------------- -------------------- -----------
    1                    花好月圆                 4
    1                    一生有你                 4
    3                    加洲旅馆                 3
    3                    我爱英语                 3
    5                    柠檬树                  2(5 行受影响)*/
      

  6.   


    --使用05的rank()函数
    --返回结果集的分区内每行的排名。行的排名是相关行之前的排名数加一。
    --RANK 函数并不总返回连续整数
    select *,rank() over (order by 得票数 desc) as 排名 from t
      

  7.   

    select * ,[count] =(select count(distinct 得票数)+1 from tblvote where a.得票数 < 得票数 )
    from tblvote a
    order by 得票数 descid          节目         得票数         count       
    ----------- ---------- ----------- ----------- 
    1           花好月圆       4           1
    2           一生有你       4           1
    4           加洲旅馆       3           2
    5           我爱英语       3           2
    3           柠檬树        2           3(所影响的行数为 5 行)