将查询结果拆成多个列

jishengzu 2012-03-30 02:56:03
一个查询语句得到下列结果
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 灶强

能不能通过查询语句实现
...全文
82 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
迪迦凹凸曼 2012-03-30
  • 打赏
  • 举报
回复
如果M条数据需要分N列的话
那么行数就为rowCount=(M+N-1)/N=(M-1)/N
迪迦凹凸曼 2012-03-30
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 的回复:]

--sql 2005用row_number实现:
SQL code
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 valu……
[/Quote]
[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>
jishengzu 2012-03-30
  • 打赏
  • 举报
回复
谢谢了。
dawugui 2012-03-30
  • 打赏
  • 举报
回复
--楼主,你的显示要么是这样:

--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'炎松')
go

select 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.cnt

drop 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'炎松')
go

select 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.cnt

drop 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'炎松')
go

select 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)/3

drop 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'炎松')
go

select 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)/3

drop 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 行受影响)

*/
dawugui 2012-03-30
  • 打赏
  • 举报
回复
--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'炎松')
go

select 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.cnt

drop 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 行受影响)

*/
dawugui 2012-03-30
  • 打赏
  • 举报
回复
--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 ,'炎松')
go

select 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.cnt

drop 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 行)

*/
SQL777 2012-03-30
  • 打赏
  • 举报
回复
生成连续ID
(ID-1)/3分组

34,594

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧