id   type    name    order
1    001     中国    2
2    001     美国    4
3    001     巴西    1
4    002     日本    ...
5    002     印尼    ...
6    003     印度    ...
7    003     伊朗
8    003     伊拉克
9    003     越南
如何按type字段分组,然后每组返回两行数据,并按order字段排序?
要求结果:
id   type    name    order
3    001     巴西    1
1    001     中国    2
4    002     日本    ...
5    002     印尼    ...
6    003     印度    ...
7    003     伊朗

解决方案 »

  1.   

    select * from tb where order<=2 order by type,order 
      

  2.   

    select * from tb where [order] <=2 order by type,[order] 
      

  3.   


    ---------------------------------
    --  Author: htl258(Tony)
    --  Date  : 2009-08-05 08:51:12
    ---------------------------------
    --> 生成测试数据表:tbIf not object_id('[tb]') is null
    Drop table [tb]
    Go
    Create table [tb]([id] int,[type] int,[name] nvarchar(3),[order] int)
    Insert tb
    Select 1,001,'中国','2' union all
    Select 2,001,'美国','4' union all
    Select 3,001,'巴西','1' union all
    Select 4,002,'日本','3' union all
    Select 5,002,'印尼','7' union all
    Select 6,003,'印度','8' union all
    Select 7,003,'伊朗','9' union all
    Select 8,003,'伊拉克','5' union all
    Select 9,003,'越南','0'
    Go
    --Select * from tb-->SQL查询如下:
    select *
    from tb t
    where id in(
      select top 2 id 
      from tb 
      where type=t.type
      order by [order])
    /*
    id          type        name order
    ----------- ----------- ---- -----------
    1           1           中国   2
    3           1           巴西   1
    4           2           日本   3
    5           2           印尼   7
    8           3           伊拉克  5
    9           3           越南   0(6 行受影响)
    */
      

  4.   

    SELECT * FROM TB  T1
    WHERE (
    SELECT COUNT(1) FROM TB T2 WHERE T2.[TYPE]=T1.[TYPE] AND T2.[ORDER]<T1.[ORDER]
    )<3
    ORDER BY T1.[TYPE],T1.[ORDER]
      

  5.   

    select * from tb a
    where id in ( select top 2 id from tb where type=a.type order by order )
    order by type,order
      

  6.   

    select * 
    from (select orw=row_number() over(partition by type order by [order] ),* from tb)t
    where row<=2
    order by row
      

  7.   

    如果不是的就用:select 
        * 
    from 
        tb t
    where 
        id 
    in 
    ( select top 2 id from tb where type=t.type order by order )
    order by 
        type,order
      

  8.   

    难道列名是ORDER的不用加[]?试试去
      

  9.   

    declare @t table(id int, type  varchar(10),  name  varchar(10),  [order] int)
    insert @t values(1,'001','中国',2)
    insert @t values(2,'001','美国',4)
    insert @t values(3,'001','巴西',1)
    insert @t values(4,'002','日本',3)
    insert @t values(5,'002','印尼',5)
    insert @t values(6,'003','印度',6)
    insert @t values(7,'003','伊朗',7)
    insert @t values(8,'003','伊拉克',9)
    insert @t values(9,'003','越南',8)select * from @t a where (select count(1) from @t where a.type=type and a.name<>name and [order] <=a.[order])<2
    order by type,[order]/*
    id          type       name       order
    ----------- ---------- ---------- -----------
    3           001        巴西         1
    1           001        中国         2
    4           002        日本         3
    5           002        印尼         5
    6           003        印度         6
    7           003        伊朗         7
    */
      

  10.   


    这个还对啊,这招昨晚刚学的
    ;with sql2005 as

    select *,num=row_number()over(partition by type order by newid())
    from @t
    )
    select id,type,name,[order] from sql2005 where num<=2 order by [order]
      

  11.   

    declare @t table(id int, type  varchar(10),  name  varchar(10),  [order] int)
    insert @t values(1,'001','中国',2)
    insert @t values(2,'001','美国',4)
    insert @t values(3,'001','巴西',1)
    insert @t values(4,'002','日本',3)
    insert @t values(5,'002','印尼',5)
    insert @t values(6,'003','印度',6)
    insert @t values(7,'003','伊朗',7)
    insert @t values(8,'003','伊拉克',9)
    insert @t values(9,'003','越南',8)select *,DENSE_RANK() OVER(partition by a.type ORDER BY a.[order] asc) AS '排名'
    from @t a
     where (select count(1) from @t 
    where a.type=type and a.name<>name and [order] <=a.[order])<2
    order by type,[order]/*
    id type name order 排名
    3 001 巴西 1 1
    1 001 中国 2 2
    4 002 日本 3 1
    5 002 印尼 5 2
    6 003 印度 6 1
    7 003 伊朗 7 2
    */
    我的理解楼主要这种结果,呵呵,不知道是不是