Id   postId
2788 45753530
6417 46862065
61773 47407456
61773 47436468
61773 47448259
61773 47474393
61773 47475474
83604 41671947
83604 45858681
83604 45887599
83604 45914793
83604 45917692
83604 45938542
83604 47408568
84783 45654007
84783 45838979
84783 46361312
84783 46446338
84783 46620045
84783 46714868
84783 46909938
84783 47191058
115566 43574799
115745 47293172
120448 45666599
120448 45728607
120448 45736301以上是数据表部分数据,要求是取得每个ID对应postId的前三条,出来如下结果:
2788 45753530
6417 46862065
61773 47407456
61773 47436468
61773 47448259
83604 41671947
83604 45858681
83604 45887599
84783 45654007
84783 45838979
84783 46361312
115566 43574799
115745 47293172
120448 45666599
120448 45728607
120448 45736301
先谢谢了。

解决方案 »

  1.   

    ---------------------------------
    --  Author: liangCK 小梁
    --  Title : 查每个分组前N条记录
    --  Date  : 2008-11-13 17:19:23
    -----------------------------------> 生成测试数据: #T
    IF OBJECT_ID('tempdb.dbo.#T') IS NOT NULL DROP TABLE #T
    CREATE TABLE #T (ID VARCHAR(3),GID INT,Author VARCHAR(29),Title VARCHAR(39),Date DATETIME)
    INSERT INTO #T
    SELECT '001',1,'邹建','深入浅出SQLServer2005开发管理与应用实例','2008-05-10' UNION ALL
    SELECT '002',1,'胡百敬','SQLServer2005性能调校','2008-03-22' UNION ALL
    SELECT '003',1,'格罗夫Groff.J.R.','SQL完全手册','2009-07-01' UNION ALL
    SELECT '004',1,'KalenDelaney','SQLServer2005技术内幕存储引擎','2008-08-01' UNION ALL
    SELECT '005',2,'Alex.Kriegel.Boris.M.Trukhnov','SQL宝典','2007-10-05' UNION ALL
    SELECT '006',2,'飞思科技产品研发中心','SQLServer2000高级管理与开发','2007-09-10' UNION ALL
    SELECT '007',2,'胡百敬','SQLServer2005数据库开发详解','2008-06-15' UNION ALL
    SELECT '008',3,'陈浩奎','SQLServer2000存储过程与XML编程','2005-09-01' UNION ALL
    SELECT '009',3,'赵松涛','SQLServer2005系统管理实录','2008-10-01' UNION ALL
    SELECT '010',3,'黄占涛','SQL技术手册','2006-01-01'--SQL查询如下:--按GID分组,查每个分组中Date最新的前2条记录
    --1.字段ID唯一时:
    SELECT * FROM #T AS T WHERE ID IN(SELECT TOP 2 ID FROM #T WHERE GID=T.GID ORDER BY Date DESC)--2.如果ID不唯一时:
    SELECT * FROM #T AS T WHERE 2>(SELECT COUNT(*) FROM #T WHERE GID=T.GID AND Date>T.Date)--SQL Server 2005 使用新方法--3.使用ROW_NUMBER()进行排位分组
    SELECT ID,GID,Author,Title,Date
    FROM
    (
       SELECT rid=ROW_NUMBER() OVER(PARTITION BY GID ORDER BY Date DESC),*
       FROM #T
    ) AS T
    WHERE rid<=2--4.使用APPLY
    SELECT DISTINCT b.*
    FROM #T AS a
    CROSS APPLY
    (
        SELECT TOP(2) * FROM #T WHERE a.GID=GID ORDER BY Date DESC
    ) AS b
    --结果
    /*ID   GID         Author                        Title                                   Date
    ---- ----------- ----------------------------- --------------------------------------- -----------------------
    003  1           格罗夫Groff.J.R.                 SQL完全手册                                 2009-07-01 00:00:00.000
    004  1           KalenDelaney                  SQLServer2005技术内幕存储引擎                   2008-08-01 00:00:00.000
    005  2           Alex.Kriegel.Boris.M.Trukhnov SQL宝典                                   2007-10-05 00:00:00.000
    007  2           胡百敬                           SQLServer2005数据库开发详解                    2008-06-15 00:00:00.000
    009  3           赵松涛                           SQLServer2005系统管理实录                     2008-10-01 00:00:00.000
    010  3           黄占涛                           SQL技术手册                                 2006-01-01 00:00:00.000(6 行受影响)
    */--得到每组前几条数据
    --假設每組Col1中, Col3不會重復--建立測試環境
    Create Table TEST
    (Col1 Varchar(10),
     Col2 Varchar(10),
     Col3 Int)
    --插入數據
    Insert TEST Select 'BD1V','Label', 4
    Union All Select 'BD1V', 'BATT', 2
    Union All Select 'BD1V', 'ODD', 3
    Union All Select 'BD1V', 'HDD', 5
    Union All Select 'BD1V', 'LCD', 1
    Union All Select 'BD1W','HDD', 3
    Union All Select 'BD1W','RAM', 8
    Union All Select 'BD1W','TP CABLE', 5
    Union All Select 'BD1W','LCD', 6
    Union All Select 'BD1W','Label', 2
    Union All Select 'BL3', 'LCD CABLE', 7
    Union All Select 'BL3', 'LABEL', 6
    Union All Select 'BL3', 'LCD', 5
    Union All Select 'BL3', 'RAM', 1
    Union All Select 'BL3D', 'Label', 4
    GO
    --測試
    --方法一:
    Select Col1, Col2, Col3 From TEST A
    Where (Select Count(*) From TEST Where Col1 = A.Col1 And Col3 > A.Col3) < 3
    Order By Col1, Col3 Desc
    --方法二:
    Select Col1, Col2, Col3 From TEST A
    Where Exists (Select Count(*) From TEST Where Col1 = A.Col1 And Col3 > A.Col3 Having Count(*) < 3)
    Order By Col1, Col3 Desc
    --方法三:
    Select Col1, Col2, Col3 From TEST A
    Where Col3 In (Select TOP 3 Col3 From TEST Where Col1 = A.Col1 Order By Col3 Desc)
    Order By Col1, Col3 Desc
    GO
    --刪除測試環境
    Drop Table TEST
    --結果
    /*
    Col1  Col2   Col3
    BD1V HDD  5
    BD1V Label  4
    BD1V ODD  3
    BD1W RAM  8
    BD1W LCD   6
    BD1W TP CABLE 5
    BL3  LCD CABLE 7
    BL3  LABEL  6
    BL3  LCD   5
    BL3D Label  4
    */
      

  2.   

    select *
    from (
    select *,row_number() over (partition by Id order by postId) as seq
    from tb
    ) t
    where seq<=3
      

  3.   

    select *
    from (
    select *,row_number() over(partition by id desc,order by postId desc) as row_numer
    from tb
    )T
    where row_num <=3
      

  4.   

    ---------------------------------
    --  Author: HEROWANG(让你望见影子的墙)
    --  Date  : 2009-08-13 11:10:53
    ---------------------------------
     
    IF OBJECT_ID('[tb]') IS NOT NULL 
        DROP TABLE [tb]
    go
    CREATE TABLE [tb] (Id INT,postId INT)
    INSERT INTO [tb]
    SELECT 2788,45753530 UNION ALL
    SELECT 6417,46862065 UNION ALL
    SELECT 61773,47407456 UNION ALL
    SELECT 61773,47436468 UNION ALL
    SELECT 61773,47448259 UNION ALL
    SELECT 61773,47474393 UNION ALL
    SELECT 61773,47475474 UNION ALL
    SELECT 83604,41671947 UNION ALL
    SELECT 83604,45858681 UNION ALL
    SELECT 83604,45887599 UNION ALL
    SELECT 83604,45914793 UNION ALL
    SELECT 83604,45917692 UNION ALL
    SELECT 83604,45938542 UNION ALL
    SELECT 83604,47408568 UNION ALL
    SELECT 84783,45654007 UNION ALL
    SELECT 84783,45838979 UNION ALL
    SELECT 84783,46361312 UNION ALL
    SELECT 84783,46446338 UNION ALL
    SELECT 84783,46620045 UNION ALL
    SELECT 84783,46714868 UNION ALL
    SELECT 84783,46909938 UNION ALL
    SELECT 84783,47191058 UNION ALL
    SELECT 115566,43574799 UNION ALL
    SELECT 115745,47293172 UNION ALL
    SELECT 120448,45666599 UNION ALL
    SELECT 120448,45728607 UNION ALL
    SELECT 120448,45736301select * from [tb]select id,postid
    from (select row=row_number() over(partition by id order by id),* from tb)K
    where row<=3id postid
    2788 45753530
    6417 46862065
    61773 47407456
    61773 47436468
    61773 47448259
    83604 41671947
    83604 45858681
    83604 45887599
    84783 45654007
    84783 45838979
    84783 46361312
    115566 43574799
    115745 47293172
    120448 45666599
    120448 45728607
    120448 45736301
      

  5.   

    SELECT * FROM #T AS T WHERE ID IN(SELECT TOP 3 ID FROM #T WHERE ID=T.ID )
      

  6.   

    SELECT * FROM TB T WHERE POSTID=(SELECT TOP 3 POSTID FROM TB WHERE T.ID=ID ORDER BY POSTID DESC)
      

  7.   

    ----------------------------------------------------------------
    -- Author :fredrickhu(小F 向高手学习)
    -- Date   :2009-08-13 11:12:01
    ----------------------------------------------------------------
    --> 测试数据:[tb]
    if object_id('[tb]') is not null drop table [tb]
    go
    create table [tb]([Id] int,[postId] int)
    insert [tb]
    select 2788,45753530 union all
    select 6417,46862065 union all
    select 61773,47407456 union all
    select 61773,47436468 union all
    select 61773,47448259 union all
    select 61773,47474393 union all
    select 61773,47475474 union all
    select 83604,41671947 union all
    select 83604,45858681 union all
    select 83604,45887599 union all
    select 83604,45914793 union all
    select 83604,45917692 union all
    select 83604,45938542 union all
    select 83604,47408568 union all
    select 84783,45654007 union all
    select 84783,45838979 union all
    select 84783,46361312 union all
    select 84783,46446338 union all
    select 84783,46620045 union all
    select 84783,46714868 union all
    select 84783,46909938 union all
    select 84783,47191058 union all
    select 115566,43574799 union all
    select 115745,47293172 union all
    select 120448,45666599 union all
    select 120448,45728607 union all
    select 120448,45736301
    --------------开始查询--------------------------
    SELECT * FROM TB AS T WHERE 3>(SELECT COUNT(1) FROM TB WHERE ID=T.ID AND postId>T.postId)----------------结果----------------------------
    /*
    (所影响的行数为 27 行)Id          postId      
    ----------- ----------- 
    2788        45753530
    6417        46862065
    61773       47448259
    61773       47474393
    61773       47475474
    83604       45917692
    83604       45938542
    83604       47408568
    84783       46714868
    84783       46909938
    84783       47191058
    115566      43574799
    115745      47293172
    120448      45666599
    120448      45728607
    120448      45736301(所影响的行数为 16 行)
    */
      

  8.   

    SELECT * FROM TB T WHERE POSTID IN (SELECT TOP 3 POSTID FROM TB WHERE T.ID=ID ORDER BY POSTID DESC)
      

  9.   

    IF OBJECT_ID('[tb]') IS NOT NULL 
        DROP TABLE [tb]
    go
    CREATE TABLE [tb] (Id INT,postId INT)
    INSERT INTO [tb]
    SELECT 2788,45753530 UNION ALL
    SELECT 6417,46862065 UNION ALL
    SELECT 61773,47407456 UNION ALL
    SELECT 61773,47436468 UNION ALL
    SELECT 61773,47448259 UNION ALL
    SELECT 61773,47474393 UNION ALL
    SELECT 61773,47475474 UNION ALL
    SELECT 83604,41671947 UNION ALL
    SELECT 83604,45858681 UNION ALL
    SELECT 83604,45887599 UNION ALL
    SELECT 83604,45914793 UNION ALL
    SELECT 83604,45917692 UNION ALL
    SELECT 83604,45938542 UNION ALL
    SELECT 83604,47408568 UNION ALL
    SELECT 84783,45654007 UNION ALL
    SELECT 84783,45838979 UNION ALL
    SELECT 84783,46361312 UNION ALL
    SELECT 84783,46446338 UNION ALL
    SELECT 84783,46620045 UNION ALL
    SELECT 84783,46714868 UNION ALL
    SELECT 84783,46909938 UNION ALL
    SELECT 84783,47191058 UNION ALL
    SELECT 115566,43574799 UNION ALL
    SELECT 115745,47293172 UNION ALL
    SELECT 120448,45666599 UNION ALL
    SELECT 120448,45728607 UNION ALL
    SELECT 120448,45736301SELECT * FROM tb AS T WHERE postId IN(SELECT TOP 3 postId FROM tb WHERE ID=T.ID ) /*
    Id          postId
    ----------- -----------
    2788        45753530
    6417        46862065
    61773       47407456
    61773       47436468
    61773       47448259
    83604       41671947
    83604       45858681
    83604       45887599
    84783       45654007
    84783       45838979
    84783       46361312
    115566      43574799
    115745      47293172
    120448      45666599
    120448      45728607
    120448      45736301(16 行受影响)
      

  10.   

    IF OBJECT_ID('[tb]') IS NOT NULL 
        DROP TABLE [tb]
    go
    CREATE TABLE [tb] (Id INT,postId INT)
    INSERT INTO [tb]
    SELECT 2788,45753530 UNION ALL
    SELECT 6417,46862065 UNION ALL
    SELECT 61773,47407456 UNION ALL
    SELECT 61773,47436468 UNION ALL
    SELECT 61773,47448259 UNION ALL
    SELECT 61773,47474393 UNION ALL
    SELECT 61773,47475474 UNION ALL
    SELECT 83604,41671947 UNION ALL
    SELECT 83604,45858681 UNION ALL
    SELECT 83604,45887599 UNION ALL
    SELECT 83604,45914793 UNION ALL
    SELECT 83604,45917692 UNION ALL
    SELECT 83604,45938542 UNION ALL
    SELECT 83604,47408568 UNION ALL
    SELECT 84783,45654007 UNION ALL
    SELECT 84783,45838979 UNION ALL
    SELECT 84783,46361312 UNION ALL
    SELECT 84783,46446338 UNION ALL
    SELECT 84783,46620045 UNION ALL
    SELECT 84783,46714868 UNION ALL
    SELECT 84783,46909938 UNION ALL
    SELECT 84783,47191058 UNION ALL
    SELECT 115566,43574799 UNION ALL
    SELECT 115745,47293172 UNION ALL
    SELECT 120448,45666599 UNION ALL
    SELECT 120448,45728607 UNION ALL
    SELECT 120448,45736301SELECT * FROM TB T WHERE POSTID IN (SELECT TOP 3 POSTID FROM TB WHERE T.ID=ID ORDER BY POSTID DESC)(所影响的行数为 27 行)Id          postId      
    ----------- ----------- 
    2788        45753530
    6417        46862065
    61773       47448259
    61773       47474393
    61773       47475474
    83604       45917692
    83604       45938542
    83604       47408568
    84783       46714868
    84783       46909938
    84783       47191058
    115566      43574799
    115745      47293172
    120448      45666599
    120448      45728607
    120448      45736301(所影响的行数为 16 行)
      

  11.   


    借小F数据
    if object_id('[tb]') is not null drop table [tb]
    go
    create table [tb]([Id] int,[postId] int)
    insert [tb]
    select 2788,45753530 union all
    select 6417,46862065 union all
    select 61773,47407456 union all
    select 61773,47436468 union all
    select 61773,47448259 union all
    select 61773,47474393 union all
    select 61773,47475474 union all
    select 83604,41671947 union all
    select 83604,45858681 union all
    select 83604,45887599 union all
    select 83604,45914793 union all
    select 83604,45917692 union all
    select 83604,45938542 union all
    select 83604,47408568 union all
    select 84783,45654007 union all
    select 84783,45838979 union all
    select 84783,46361312 union all
    select 84783,46446338 union all
    select 84783,46620045 union all
    select 84783,46714868 union all
    select 84783,46909938 union all
    select 84783,47191058 union all
    select 115566,43574799 union all
    select 115745,47293172 union all
    select 120448,45666599 union all
    select 120448,45728607 union all
    select 120448,45736301;with szy as
    (
    select *,px=row_number()over(partition by id order by POSTID )
    from Tb
    )select id,postid from szy where px<=3id          postid
    ----------- -----------
    2788        45753530
    6417        46862065
    61773       47407456
    61773       47436468
    61773       47448259
    83604       41671947
    83604       45858681
    83604       45887599
    84783       45654007
    84783       45838979
    84783       46361312
    115566      43574799
    115745      47293172
    120448      45666599
    120448      45728607
    120448      45736301(16 行受影响)
      

  12.   

    if object_id('tb') is not null
    drop table tb
    go
    create table tb (id int ,postid int)
    insert into tb select 2788 ,   45753530 union all select
    6417 ,   46862065union all select
    61773 ,   47407456union all select
    61773 ,   47436468union all select
    61773,    47448259union all select
    61773,    47474393union all select
    61773 ,   47475474union all select
    83604 ,   41671947union all select
    83604,    45858681union all select
    83604 ,   45887599union all select
    83604,    45914793union all select
    83604,    45917692union all select
    83604 ,   45938542union all select
    83604 ,   47408568union all select
    84783,    45654007union all select
    84783,    45838979union all select
    84783,    46361312union all select
    84783,    46446338union all select
    84783 ,   46620045union all select
    84783,    46714868union all select
    84783,    46909938union all select
    84783,    47191058union all select
    115566 ,   43574799union all select
    115745,    47293172union all select
    120448,    45666599union all select
    120448,    45728607union all select
    120448,    45736301
    select id, postid from (select * ,cid=row_number() over (partition by id order by id) from tb ) as t
    where t.cid<=3
    /*
    id          postid
    ----------- -----------
    2788        45753530
    6417        46862065
    61773       47407456
    61773       47436468
    61773       47448259
    83604       41671947
    83604       45858681
    83604       45887599
    84783       45654007
    84783       45838979
    84783       46361312
    115566      43574799
    115745      47293172
    120448      45666599
    120448      45728607
    120448      45736301(16 行受影响)
    */
      

  13.   


    IF OBJECT_ID('[tb]') IS NOT NULL 
        DROP TABLE [tb]
    go
    CREATE TABLE [tb] (Id INT,postId INT)
    INSERT INTO [tb]
    SELECT 2788,45753530 UNION ALL
    SELECT 6417,46862065 UNION ALL
    SELECT 61773,47407456 UNION ALL
    SELECT 61773,49436468 UNION ALL
    SELECT 61773,47448259 UNION ALL
    SELECT 61773,47474393 UNION ALL
    SELECT 61773,47475474 UNION ALL
    SELECT 83604,41671947 UNION ALL
    SELECT 83604,45858681 UNION ALL
    SELECT 83604,45887599 UNION ALL
    SELECT 83604,45914793 UNION ALL
    SELECT 83604,45917692 UNION ALL
    SELECT 83604,45938542 UNION ALL
    SELECT 83604,47408568 UNION ALL
    SELECT 84783,45654007 UNION ALL
    SELECT 84783,45838979 UNION ALL
    SELECT 84783,46361312 UNION ALL
    SELECT 84783,46446338 UNION ALL
    SELECT 84783,46620045 UNION ALL
    SELECT 84783,46714868 UNION ALL
    SELECT 84783,46909938 UNION ALL
    SELECT 84783,47191058 UNION ALL
    SELECT 115566,43574799 UNION ALL
    SELECT 115745,47293172 UNION ALL
    SELECT 120448,45666599 UNION ALL
    SELECT 120448,45728607 UNION ALL
    SELECT 120448,45736301SELECT * FROM 
    (SELECT Id,postid, Row_Number() over(Partition by id Order by Id,postid) as RowId
    FROM TB) A
    WHERE A.RowId<=3--执行结果
    -----------------------
    2788 45753530 1
    6417 46862065 1
    61773 47407456 1
    61773 47448259 2
    61773 47474393 3
    83604 41671947 1
    83604 45858681 2
    83604 45887599 3
    84783 45654007 1
    84783 45838979 2
    84783 46361312 3
    115566 43574799 1
    115745 47293172 1
    120448 45666599 1
    120448 45728607 2
    120448 45736301 3
      

  14.   

    IF OBJECT_ID('[tb]') IS NOT NULL 
        DROP TABLE [tb]
    go
    CREATE TABLE [tb] (Id INT,postId INT)
    INSERT INTO [tb]
    SELECT 2788,45753530 UNION ALL
    SELECT 6417,46862065 UNION ALL
    SELECT 61773,47407456 UNION ALL
    SELECT 61773,47436468 UNION ALL
    SELECT 61773,47448259 UNION ALL
    SELECT 61773,47474393 UNION ALL
    SELECT 61773,47475474 UNION ALL
    SELECT 83604,41671947 UNION ALL
    SELECT 83604,45858681 UNION ALL
    SELECT 83604,45887599 UNION ALL
    SELECT 83604,45914793 UNION ALL
    SELECT 83604,45917692 UNION ALL
    SELECT 83604,45938542 UNION ALL
    SELECT 83604,47408568 UNION ALL
    SELECT 84783,45654007 UNION ALL
    SELECT 84783,45838979 UNION ALL
    SELECT 84783,46361312 UNION ALL
    SELECT 84783,46446338 UNION ALL
    SELECT 84783,46620045 UNION ALL
    SELECT 84783,46714868 UNION ALL
    SELECT 84783,46909938 UNION ALL
    SELECT 84783,47191058 UNION ALL
    SELECT 115566,43574799 UNION ALL
    SELECT 115745,47293172 UNION ALL
    SELECT 120448,45666599 UNION ALL
    SELECT 120448,45728607 UNION ALL
    SELECT 120448,45736301--sql server 2000方法select *
    from (select row=row_number() over(partition by id order by id),* from tb)K
    where row<=3--sql server 2005-2008方法
    select *
    from (select row=
                   (select COUNT(1)+1 
                      from tb 
                       where Id=t.id 
                             and postId<t.postid),* from tb t
          )K
    where row<=3
    /*
    row Id postId
    1 2788 45753530
    1 6417 46862065
    1 61773 47407456
    2 61773 47436468
    3 61773 47448259
    1 83604 41671947
    2 83604 45858681
    3 83604 45887599
    1 84783 45654007
    2 84783 45838979
    3 84783 46361312
    1 115566 43574799
    1 115745 47293172
    1 120448 45666599
    2 120448 45728607
    3 120448 45736301
    */
      

  15.   

    select *
    from (select row=
                   (select COUNT(1)+1                         
                      from tb 
                       where Id=t.id 
                             and postId<t.postid),* from tb t  --可改成postId<=t.postid,count(1)后面+1就不用了      )K
    where row<=3                 --返回前面三行数据