一个script

ktoya 2013-07-24 06:08:08
result
1
2
3
4
5
6
7
8
9
10

想把他变成3列挨个排下来,如上面的表把他变成
c1 c2 c3
1 2 3
4 5 6
7 8 9
10
...全文
191 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
Shawn 2013-08-01
  • 打赏
  • 举报
回复
;with t as
(
	select result, (row_number() over(order by getdate())-1)/3 'rn'
	from kt
)
select [1] 'a',[2] 'b',[3] 'c'
FROM
(
	select result,rn,row_number() over(PARTITION BY rn order by rn) 'rn2'	--这里可以直接用partition by
	from t
) a
PIVOT
(
	max(result) for rn2 in([1],[2],[3])
) b
唐诗三百首 2013-08-01
  • 打赏
  • 举报
回复

create table kt(result int)

insert into kt
 select 1 union all
 select 2 union all
 select 3 union all
 select 4 union all
 select 5 union all
 select 6 union all
 select 7 union all
 select 8 union all
 select 9 union all
 select 10


with t as
(select result,
        (row_number() over(order by getdate())-1)/3 'rn'
 from kt
)
select [1] 'a',[2] 'b',[3] 'c'
from
(select result,rn,row_number() over(order by getdate())-3*rn 'rn2'
 from t) a
pivot(max(result) for rn2 in([1],[2],[3])) p

/*
a           b           c
----------- ----------- -----------
1           2           3
4           5           6
7           8           9
10          NULL        NULL

(4 row(s) affected)
*/
唐诗三百首 2013-08-01
  • 打赏
  • 举报
回复

create table kt(result int)

insert into kt
 select -1 union all
 select -1 union all
 select -1 union all
 select 1 union all
 select -1 union all
 select 1 union all
 select -1 union all
 select -1 union all
 select -1


with t as
(select result,
        (row_number() over(order by getdate())-1)/3 'rn'
 from kt
)
select [1] 'a',[2] 'b',[3] 'c'
from
(select result,rn,row_number() over(order by getdate())-3*rn 'rn2'
 from t) a
pivot(max(result) for rn2 in([1],[2],[3])) p

/*
a           b           c
----------- ----------- -----------
-1          -1          -1
1           -1          1
-1          -1          -1

(3 row(s) affected)
*/
KeepSayingNo 2013-08-01
  • 打赏
  • 举报
回复
这个放到代码里面去做多好啊,这不是折腾DBA吗
---涛声依旧--- 2013-07-31
  • 打赏
  • 举报
回复
请问楼主这些数是存放在一个表里的吗?
ktoya 2013-07-31
  • 打赏
  • 举报
回复
对不起,问题补充,并不是1,2,3,4这样排下来的。有可能是这样 -1 -1 -1 1 -1 1 -1 -1 -1 一共9个数,那么就排成 -1,-1,-1 1,-1,-1 -1,-1,-1
Leon_He2014 2013-07-25
  • 打赏
  • 举报
回复

select 1 result 
into #t
union all select 2
union all select 3
union all select 4
union all select 5
union all select 6
union all select 7
union all select 8
union all select 9
union all select 10

select c1=max( case when result%3=1 then result else null end )
,c2=max( case when result%3=2 then result else null end )
,c3=max( case when result%3=0 then result else null end )
from #t
group by (result-1)/3
---涛声依旧--- 2013-07-25
  • 打赏
  • 举报
回复
Andy__Huang 2013-07-24
  • 打赏
  • 举报
回复
declare @s varchar(8000) set @s='' select @s=@s+CAST(number as varchar)+CHAR(9)+case when number %3=0 then CHAR(13) else CHAR(9) end from master..spt_values where type='P' and number between 1 and 20 print left(@s,len(@s)-1) /* 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 */

34,590

社区成员

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

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