表中2个字段
--------------------------------------------------
----id----------------fenshu------------
----1                  22
----2                  23
----3                   1
----4                   1
----5                   21
----6                    2
----7                    2
----8                   22类似这样的一个表结构,现在要查询出这个样子----分数        ----------名次      ----id
     23                     1            2
     22                     2            1
     22                     2            8  
     21                     4
     2                      5
     2                      5也就是按名次查询出分数,这个能不能用case when 实现? 或者其他办法也行。

解决方案 »

  1.   

    select 
       分数,
       名次=(select count(*) from tb where 分数>=t.分数),
       id
    from tb t
       
      

  2.   

    select 分数,名次=(select count(分数)+1 from tb where t.分数<分数),id  from tb t
      

  3.   

    select 分数,名次=rank() over(order by 分数),id from tb
      

  4.   

    select 分数,名次=(select count(分数)+1 from tb where t.分数<分数),id  from tb t
    order by 分数
      

  5.   

    --> Title  : Generating test data [tb]
    --> Author : wufeng4552
    --> Date   : 2009-11-02 10:40:59
    if object_id('[tb]') is not null drop table [tb]
    go
    create table [tb] (id int,fenshu int)
    insert into [tb]
    select 1,22 union all
    select 2,23 union all
    select 3,1 union all
    select 4,1 union all
    select 5,21 union all
    select 6,2 union all
    select 7,2 union all
    select 8,22
    select 
       fenshu,
       名次=(select count(*) from tb where fenshu>=t.fenshu),
       id
    from tb t
    order by (select count(*) from tb where fenshu>=t.fenshu)
    /*
    fenshu      名次          id
    ----------- ----------- -----------
    23          1           2
    22          3           1
    22          3           8
    21          4           5
    2           6           6
    2           6           7
    1           8           3
    1           8           4(8 個資料列受到影響)
    */
      

  6.   

    ----------------------------------------------------------------
    -- Author  :fredrickhu(我是小F,向高手学习)
    -- Date    :2009-11-02 10:41:46
    -- Version:
    --      Microsoft SQL Server 2005 - 9.00.4035.00 (Intel X86) 
    -- Nov 24 2008 13:01:59 
    -- Copyright (c) 1988-2005 Microsoft Corporation
    -- Developer Edition on Windows NT 5.2 (Build 3790: Service Pack 1)
    --
    ----------------------------------------------------------------
    --> 测试数据:[tb]
    if object_id('[tb]') is not null drop table [tb]
    go 
    create table [tb]([id] int,[fenshu] int)
    insert [tb]
    select 1,22 union all
    select 2,23 union all
    select 3,1 union all
    select 4,1 union all
    select 5,21 union all
    select 6,2 union all
    select 7,2 union all
    select 8,22
    --------------开始查询--------------------------
    select fenshu as 分数,名次=(select count(fenshu)+1 from tb where t.fenshu<fenshu),id  from tb t order by 2
    ----------------结果----------------------------
    /* 分数          名次          id
    ----------- ----------- -----------
    23          1           2
    22          2           1
    22          2           8
    21          4           5
    2           5           6
    2           5           7
    1           7           3
    1           7           4(8 行受影响)*/
      

  7.   

    select fenshu  as 分数, 名次=(select count(*) from 表1 where 分数>=表.分数),id from 表
      

  8.   

    ----------------------------------------------------------------
    -- Author  :fredrickhu(我是小F,向高手学习)
    -- Date    :2009-11-02 10:41:46
    -- Version:
    --      Microsoft SQL Server 2005 - 9.00.4035.00 (Intel X86) 
    -- Nov 24 2008 13:01:59 
    -- Copyright (c) 1988-2005 Microsoft Corporation
    -- Developer Edition on Windows NT 5.2 (Build 3790: Service Pack 1)
    --
    ----------------------------------------------------------------
    --> 测试数据:[tb]
    if object_id('[tb]') is not null drop table [tb]
    go 
    create table [tb]([id] int,[fenshu] int)
    insert [tb]
    select 1,22 union all
    select 2,23 union all
    select 3,1 union all
    select 4,1 union all
    select 5,21 union all
    select 6,2 union all
    select 7,2 union all
    select 8,22
    --------------开始查询--------------------------
    select fenshu as 分数,名次=(select count(fenshu)+1 from tb where t.fenshu>fenshu),id  from tb t order by 2
    ----------------结果----------------------------
    /* 分数          名次          id
    ----------- ----------- -----------
    1           1           3
    1           1           4
    2           3           6
    2           3           7
    21          5           5
    22          6           1
    22          6           8
    23          8           2(8 行受影响)
    */
      

  9.   

    修正
    --> Title  : Generating test data [tb]
    --> Author : wufeng4552
    --> Date   : 2009-11-02 10:40:59
    if object_id('[tb]') is not null drop table [tb]
    go
    create table [tb] (id int,fenshu int)
    insert into [tb]
    select 1,22 union all
    select 2,23 union all
    select 3,1 union all
    select 4,1 union all
    select 5,21 union all
    select 6,2 union all
    select 7,2 union all
    select 8,22
    select 
       fenshu,
       名次=(select count(distinct fenshu) from tb where fenshu>=t.fenshu),
       id
    from tb t
    order by (select count(distinct fenshu) from tb where fenshu>=t.fenshu)
    /*
    fenshu      名次          id
    ----------- ----------- -----------
    23          1           2
    22          2           1
    22          2           8
    21          3           5
    2           4           6
    2           4           7
    1           5           3
    1           5           4(8 個資料列受到影響)(8 個資料列受到影響)
    */
      

  10.   

    select fenshu  as 分数, 名次=(select count(*) from 表1 where 分数<表.分数)+1,id from 表
      

  11.   


    貌似这个正解,呵呵,ms-sql区好多牛人在活动撒,j2ee区都是星星一下的。呵呵这个用case when 能实现么?
    select id , fen 
    (
    case fen
    when t.fen>=fen then 1;
    else 0;
    end;
    )
    from s  as t 当然我写的这个肯定是错的,我的意思是类似这个样子的语句能实现么? 这是我最初的想法
      

  12.   

    count(distinct fenshu)
    我被妳忽悠了
      

  13.   


    忘记加DESC
    select 分数,名次=rank() over(order by 分数 desc),id from tb
      

  14.   


    彻底倒了 count(distinct fenshu) 这个结果可以满足另一种要求,j2ee区怎么解决个问题就那么麻烦呢???
    以后来ms区混了。