--建立测试环境
set nocount on
create table test(A varchar(20),B varchar(20),C float)
insert into test select '2','150055','2239'
insert into test select '2','150056','2088'
insert into test select '2','143831','1341'
insert into test select '2','143830','800'
insert into test select '2','144298','799'
insert into test select '2','103018','638'
insert into test select '2','150054','542'
insert into test select '2','150007','504'
insert into test select '2','125301','460'
insert into test select '2','105004','449'
insert into test select '1','143831','3235'
insert into test select '1','125465','1244'
insert into test select '1','104002','1158'
insert into test select '1','111266','1081'
insert into test select '1','101065','963'
insert into test select '1','173091','906.8144'
insert into test select '1','143716','799'
insert into test select '1','144298','788'
insert into test select '1','150054','761'
insert into test select '1','104026','760'
go
--测试
select * from(
select *,(select count(*) from test where A=a.a and c>a.c)+1 as rn from test a)t
where rn<=10drop table test/*
A                    B                    C                      rn
-------------------- -------------------- ---------------------- -----------
2                    150055               2239                   1
2                    150056               2088                   2
2                    143831               1341                   3
2                    143830               800                    4
2                    144298               799                    5
2                    103018               638                    6
2                    150054               542                    7
2                    150007               504                    8
2                    125301               460                    9
2                    105004               449                    10
1                    143831               3235                   1
1                    125465               1244                   2
1                    104002               1158                   3
1                    111266               1081                   4
1                    101065               963                    5
1                    173091               906.8144               6
1                    143716               799                    7
1                    144298               788                    8
1                    150054               761                    9
1                    104026               760                    10*/

解决方案 »

  1.   

    A        B      C 
    select * from(select ,Row_number() over(partition by b order c) rank from tb) T where rank<=10
      

  2.   

    不过这个功能对于sql2000来说可能会比较慢,你要把A字段加索引。
      

  3.   

    DECLARE @TB TABLE(A INT, B INT,C FLOAT)
    INSERT @TB
    SELECT 2,  150055,  2239 UNION ALL 
    SELECT 2,  150056,  2088 UNION ALL 
    SELECT 2,  143831,  1341 UNION ALL 
    SELECT 2,  143830,  800 UNION ALL 
    SELECT 2,  144298,  799 UNION ALL 
    SELECT 2,  103018,  638 UNION ALL 
    SELECT 2,  150054,  542 UNION ALL 
    SELECT 2,  150007,  504 UNION ALL 
    SELECT 2,  125301,  460 UNION ALL 
    SELECT 2,  105004,  449 UNION ALL 
    SELECT 1,  143831,  3235 UNION ALL 
    SELECT 1,  125465,  1244 UNION ALL 
    SELECT 1,  104002,  1158 UNION ALL 
    SELECT 1,  111266,  1081 UNION ALL 
    SELECT 1,  101065,  963 UNION ALL 
    SELECT 1,  173091,  906.8144 UNION ALL 
    SELECT 1,  143716,  799 UNION ALL 
    SELECT 1,  144298,  788 UNION ALL 
    SELECT 1,  150054,  761 UNION ALL 
    SELECT 1,  104026,  760SELECT *
    FROM (
    SELECT *,RANK=RANK() OVER (PARTITION BY A ORDER BY C DESC)
    FROM @TB) T
    WHERE RANK<=10
    /*(20 row(s) affected)
    A           B           C                      RANK
    ----------- ----------- ---------------------- --------------------
    1           143831      3235                   1
    1           125465      1244                   2
    1           104002      1158                   3
    1           111266      1081                   4
    1           101065      963                    5
    1           173091      906.8144               6
    1           143716      799                    7
    1           144298      788                    8
    1           150054      761                    9
    1           104026      760                    10
    2           150055      2239                   1
    2           150056      2088                   2
    2           143831      1341                   3
    2           143830      800                    4
    2           144298      799                    5
    2           103018      638                    6
    2           150054      542                    7
    2           150007      504                    8
    2           125301      460                    9
    2           105004      449                    10(20 row(s) affected)
    */
      

  4.   


    select a,b,c from (select row_number() over(partition by a order by b desc) rank,* from tb) T
    where rank<=10
      

  5.   

    修改下
    create table test(A varchar(20),B varchar(20),C float)
    insert into test select '2','150055','2239'
    insert into test select '2','150056','2088'
    insert into test select '2','143831','1341'
    insert into test select '2','143830','800'
    insert into test select '2','144298','799'
    insert into test select '2','103018','638'
    insert into test select '2','150054','542'
    insert into test select '2','150007','504'
    insert into test select '2','125301','460'
    insert into test select '2','105004','449'
    insert into test select '1','143831','3235'
    insert into test select '1','125465','1244'
    insert into test select '1','104002','1158'
    insert into test select '1','111266','1081'
    insert into test select '1','101065','963'
    insert into test select '1','173091','906.8144'
    insert into test select '1','143716','799'
    insert into test select '1','144298','788'
    insert into test select '1','150054','761'
    insert into test select '1','104026','760'select * from(select *,Row_number() over(partition by A order by c) rank from test) T where rank<=10A                    B                    C                      rank
    -------------------- -------------------- ---------------------- --------------------
    1                    104026               760                    1
    1                    150054               761                    2
    1                    144298               788                    3
    1                    143716               799                    4
    1                    173091               906.8144               5
    1                    101065               963                    6
    1                    111266               1081                   7
    1                    104002               1158                   8
    1                    125465               1244                   9
    1                    143831               3235                   10
    2                    105004               449                    1
    2                    125301               460                    2
    2                    150007               504                    3
    2                    150054               542                    4
    2                    103018               638                    5
    2                    144298               799                    6
    2                    143830               800                    7
    2                    143831               1341                   8
    2                    150056               2088                   9
    2                    150055               2239                   10(20 行受影响)
      

  6.   


    select * from 
    (
    SELECT *,(select count(1) from tableName where a.A=A) count
    FROM tableName a 
    ) a  where count<=10
      

  7.   

    SELECT * FROM tb a WHERE C IN(SELECT TOP 10 C FROM TB WHERE cola=a.cola ORDER BY C DESC) order by cola
      

  8.   


    --刚刚笔误
    select * from 
    (
    SELECT *,(select count(1) from tableName where a.A=A and c<a.c) count
    FROM tableName a 
    ) a  where count<=10
      

  9.   

    --这个事2000 的写法
    select a,
           b,
           c,
           rank=(select count(1)+1 from test where t.a=a and t.c>c)
    from test t A                    B                    C                      rank
    -------------------- -------------------- ---------------------- --------------------
    1                    104026               760                    1
    1                    150054               761                    2
    1                    144298               788                    3
    1                    143716               799                    4
    1                    173091               906.8144               5
    1                    101065               963                    6
    1                    111266               1081                   7
    1                    104002               1158                   8
    1                    125465               1244                   9
    1                    143831               3235                   10
    2                    105004               449                    1
    2                    125301               460                    2
    2                    150007               504                    3
    2                    150054               542                    4
    2                    103018               638                    5
    2                    144298               799                    6
    2                    143830               800                    7
    2                    143831               1341                   8
    2                    150056               2088                   9
    2                    150055               2239                   10
      

  10.   


    select * from #t A left join (
    select id row_number over(partition by id order by id) Cnt from #t 
    where Cnt <= 10
    group by id) B 
    on A.id = B.id 
      

  11.   

    select * from #T tb where c in(select top 10 c from #t where id=tb.id)
      

  12.   

    select * from #T tb where b in(select top 10 b from #t where id=tb.id order by c)
      

  13.   

    用11楼,13楼的办法行了(基本原理一致)12楼的办法貌似跟TOP 10无关吧3,8,7楼的办法是2005的函数,2000无法使用10,15,16楼的办法基本一样,测试多次了,都不行