一个查询语句得到下列结果
code      name
-----------------
8 饶平五
9 江成
10 陈明
17 清水
20 灶强
23 伟雄
24 后面1号
26 老高
27 818
32 亚辉
33 亚中
34 迎满
38 炎松
-------------------------------------
我想处理成code      name     code1   name1       code2  name2
------------------------------------------------------
8 饶平五     23 伟雄         32 亚辉
9 江成       24 后面1号      33 亚中
10 陈明       26 老高         34 迎满
17 清水       27 818          38 炎松
20 灶强       能不能通过查询语句实现

解决方案 »

  1.   

    --sql 2000用子查询实现:
    create table tb(code int,name varchar(10))
    insert into tb values(8  ,'饶平五')
    insert into tb values(9  ,'江成')
    insert into tb values(10 ,'陈明')
    insert into tb values(17 ,'清水')
    insert into tb values(20 ,'灶强')
    insert into tb values(23 ,'伟雄')
    insert into tb values(24 ,'后面1号')
    insert into tb values(26 ,'老高')
    insert into tb values(27 ,'818')
    insert into tb values(32 ,'亚辉')
    insert into tb values(33 ,'亚中')
    insert into tb values(34 ,'迎满')
    insert into tb values(38 ,'炎松')
    goselect max(case when (px - 1)/n.cnt = 0 then code else null end) code,
           max(case when (px - 1)/n.cnt = 0 then name else null end) name,
           max(case when (px - 1)/n.cnt = 1 then code else null end) code1,
           max(case when (px - 1)/n.cnt = 1 then name else null end) name1,
           max(case when (px - 1)/n.cnt = 2 then code else null end) code2,
           max(case when (px - 1)/n.cnt = 2 then name else null end) name2
    from
    (
      select t.* , px = (select count(1) from tb where code < t.code) + 1 from tb t
    ) m , (select case when count(1)%3 = 0 then count(1)/3 else count(1)/3 + 1 end cnt from tb ) n
    group by (px - 1)%n.cntdrop table tb/*
    code        name       code1       name1      code2       name2      
    ----------- ---------- ----------- ---------- ----------- ---------- 
    8           饶平五        23          伟雄         33          亚中
    9           江成         24          后面1号       34          迎满
    10          陈明         26          老高         38          炎松
    17          清水         27          818        NULL        NULL
    20          灶强         32          亚辉         NULL        NULL(所影响的行数为 5 行)*/
      

  2.   

    --sql 2005用row_number实现:
    create table tb(code int,name nvarchar(10))
    insert into tb values(8  ,N'饶平五')
    insert into tb values(9  ,N'江成')
    insert into tb values(10 ,N'陈明')
    insert into tb values(17 ,N'清水')
    insert into tb values(20 ,N'灶强')
    insert into tb values(23 ,N'伟雄')
    insert into tb values(24 ,N'后面1号')
    insert into tb values(26 ,N'老高')
    insert into tb values(27 ,N'818')
    insert into tb values(32 ,N'亚辉')
    insert into tb values(33 ,N'亚中')
    insert into tb values(34 ,N'迎满')
    insert into tb values(38 ,N'炎松')
    goselect max(case when (px - 1)/n.cnt = 0 then code else null end) code,
           max(case when (px - 1)/n.cnt = 0 then name else null end) name,
           max(case when (px - 1)/n.cnt = 1 then code else null end) code1,
           max(case when (px - 1)/n.cnt = 1 then name else null end) name1,
           max(case when (px - 1)/n.cnt = 2 then code else null end) code2,
           max(case when (px - 1)/n.cnt = 2 then name else null end) name2
    from
    (
      select t.* , px = row_number() over(order by code) from tb t
    ) m , (select case when count(1)%3 = 0 then count(1)/3 else count(1)/3 + 1 end cnt from tb ) n
    group by (px - 1)%n.cntdrop table tb/*
    code        name       code1       name1      code2       name2
    ----------- ---------- ----------- ---------- ----------- ----------
    8           饶平五        23          伟雄         33          亚中
    9           江成         24          后面1号       34          迎满
    10          陈明         26          老高         38          炎松
    17          清水         27          818        NULL        NULL
    20          灶强         32          亚辉         NULL        NULL
    警告: 聚合或其他 SET 操作消除了空值。(5 行受影响)*/
      

  3.   

    --楼主,你的显示要么是这样:--sql 2000
    create table tb(code int,name nvarchar(10))
    insert into tb values(8  ,N'饶平五')
    insert into tb values(9  ,N'江成')
    insert into tb values(10 ,N'陈明')
    insert into tb values(17 ,N'清水')
    insert into tb values(20 ,N'灶强')
    insert into tb values(23 ,N'伟雄')
    insert into tb values(24 ,N'后面1号')
    insert into tb values(26 ,N'老高')
    insert into tb values(27 ,N'818')
    insert into tb values(32 ,N'亚辉')
    insert into tb values(33 ,N'亚中')
    insert into tb values(34 ,N'迎满')
    insert into tb values(38 ,N'炎松')
    goselect max(case when (px - 1)/n.cnt = 0 then code else null end) code,
           max(case when (px - 1)/n.cnt = 0 then name else null end) name,
           max(case when (px - 1)/n.cnt = 1 then code else null end) code1,
           max(case when (px - 1)/n.cnt = 1 then name else null end) name1,
           max(case when (px - 1)/n.cnt = 2 then code else null end) code2,
           max(case when (px - 1)/n.cnt = 2 then name else null end) name2
    from
    (
      select t.* , px = (select count(1) from tb where code < t.code) + 1 from tb t
    ) m , (select case when count(1)%3 = 0 then count(1)/3 else count(1)/3 + 1 end cnt from tb ) n
    group by (px - 1)%n.cntdrop table tb/*
    code        name       code1       name1      code2       name2      
    ----------- ---------- ----------- ---------- ----------- ---------- 
    8           饶平五        23          伟雄         33          亚中
    9           江成         24          后面1号       34          迎满
    10          陈明         26          老高         38          炎松
    17          清水         27          818        NULL        NULL
    20          灶强         32          亚辉         NULL        NULL(所影响的行数为 5 行)*/--sql 2005
    create table tb(code int,name nvarchar(10))
    insert into tb values(8  ,N'饶平五')
    insert into tb values(9  ,N'江成')
    insert into tb values(10 ,N'陈明')
    insert into tb values(17 ,N'清水')
    insert into tb values(20 ,N'灶强')
    insert into tb values(23 ,N'伟雄')
    insert into tb values(24 ,N'后面1号')
    insert into tb values(26 ,N'老高')
    insert into tb values(27 ,N'818')
    insert into tb values(32 ,N'亚辉')
    insert into tb values(33 ,N'亚中')
    insert into tb values(34 ,N'迎满')
    insert into tb values(38 ,N'炎松')
    goselect max(case when (px - 1)/n.cnt = 0 then code else null end) code,
           max(case when (px - 1)/n.cnt = 0 then name else null end) name,
           max(case when (px - 1)/n.cnt = 1 then code else null end) code1,
           max(case when (px - 1)/n.cnt = 1 then name else null end) name1,
           max(case when (px - 1)/n.cnt = 2 then code else null end) code2,
           max(case when (px - 1)/n.cnt = 2 then name else null end) name2
    from
    (
      select t.* , px = row_number() over(order by code) from tb t
    ) m , (select case when count(1)%3 = 0 then count(1)/3 else count(1)/3 + 1 end cnt from tb ) n
    group by (px - 1)%n.cntdrop table tb/*
    code        name       code1       name1      code2       name2
    ----------- ---------- ----------- ---------- ----------- ----------
    8           饶平五        23          伟雄         33          亚中
    9           江成         24          后面1号       34          迎满
    10          陈明         26          老高         38          炎松
    17          清水         27          818        NULL        NULL
    20          灶强         32          亚辉         NULL        NULL
    警告: 聚合或其他 SET 操作消除了空值。(5 行受影响)*/
    --要么是这样:--sql 2000
    create table tb(code int,name nvarchar(10))
    insert into tb values(8  ,N'饶平五')
    insert into tb values(9  ,N'江成')
    insert into tb values(10 ,N'陈明')
    insert into tb values(17 ,N'清水')
    insert into tb values(20 ,N'灶强')
    insert into tb values(23 ,N'伟雄')
    insert into tb values(24 ,N'后面1号')
    insert into tb values(26 ,N'老高')
    insert into tb values(27 ,N'818')
    insert into tb values(32 ,N'亚辉')
    insert into tb values(33 ,N'亚中')
    insert into tb values(34 ,N'迎满')
    insert into tb values(38 ,N'炎松')
    goselect max(case when (px - 1)%3 = 0 then code else null end) code,
           max(case when (px - 1)%3 = 0 then name else null end) name,
           max(case when (px - 1)%3 = 1 then code else null end) code1,
           max(case when (px - 1)%3 = 1 then name else null end) name1,
           max(case when (px - 1)%3 = 2 then code else null end) code2,
           max(case when (px - 1)%3 = 2 then name else null end) name2
    from
    (
      select t.* , px = (select count(1) from tb where code < t.code) + 1 from tb t
    ) m 
    group by (px - 1)/3drop table tb/*
    code        name       code1       name1      code2       name2      
    ----------- ---------- ----------- ---------- ----------- ---------- 
    8           饶平五        9           江成         10          陈明
    17          清水         20          灶强         23          伟雄
    24          后面1号       26          老高         27          818
    32          亚辉         33          亚中         34          迎满
    38          炎松         NULL        NULL       NULL        NULL(所影响的行数为 5 行)
    */--sql 2005
    create table tb(code int,name nvarchar(10))
    insert into tb values(8  ,N'饶平五')
    insert into tb values(9  ,N'江成')
    insert into tb values(10 ,N'陈明')
    insert into tb values(17 ,N'清水')
    insert into tb values(20 ,N'灶强')
    insert into tb values(23 ,N'伟雄')
    insert into tb values(24 ,N'后面1号')
    insert into tb values(26 ,N'老高')
    insert into tb values(27 ,N'818')
    insert into tb values(32 ,N'亚辉')
    insert into tb values(33 ,N'亚中')
    insert into tb values(34 ,N'迎满')
    insert into tb values(38 ,N'炎松')
    goselect max(case when (px - 1)%3 = 0 then code else null end) code,
           max(case when (px - 1)%3 = 0 then name else null end) name,
           max(case when (px - 1)%3 = 1 then code else null end) code1,
           max(case when (px - 1)%3 = 1 then name else null end) name1,
           max(case when (px - 1)%3 = 2 then code else null end) code2,
           max(case when (px - 1)%3 = 2 then name else null end) name2
    from
    (
      select t.* , px = row_number() over(order by code) from tb t
    ) m 
    group by (px - 1)/3drop table tb/*
    code        name       code1       name1      code2       name2
    ----------- ---------- ----------- ---------- ----------- ----------
    8           饶平五        9           江成         10          陈明
    17          清水         20          灶强         23          伟雄
    24          后面1号       26          老高         27          818
    32          亚辉         33          亚中         34          迎满
    38          炎松         NULL        NULL       NULL        NULL
    警告: 聚合或其他 SET 操作消除了空值。(5 行受影响)*/
      

  4.   

    [code=sql]
    select case when count(1)%3 = 0 then count(1)/3 else count(1)/3 + 1 end cnt from tb 对于需要多少行这句话可以写成
    select (count(1)+3-1)/3 as cnt from tb
    这样就可以减少一次取余运算,一次判断运算(case ..),一次除法,提高了效率
    有点类似web中分页哦
    [/sql]
    <a href="http://topic.csdn.net/u/20120328/17/19cd16c8-3551-4f9f-b755-9f5c8cbbf403.html">详情</a>
      

  5.   

    如果M条数据需要分N列的话
    那么行数就为rowCount=(M+N-1)/N=(M-1)/N