将一维相同数据转化成三列形式
A表如下:
ITEM
abc
abc
abc
abc
abc
abc
abc
希望可以弄出B表样式:
ITEM1 ITEM2 TIME3
abc abc abc
abc abc abc
abc
就是把A表的数据按照三列形式排列,请教代码,思路我知道了,但是不清楚怎么做?
[解决办法]
declare @s varchar(8000)
set @s=''
select @s=@s+Name+CHAR(9)+case when rn%3=0 then CHAR(10) else '' end
from (select Name,ROW_NUMBER() Over(order by Name) rn from tb where LogName<>'')t
print left(@s,len(@s)-1)
[解决办法]
SQL2000的方法,
create table A表(ITEM varchar(10))
insert into A表
select 'abc' union all
select 'abc' union all
select 'abc' union all
select 'abc' union all
select 'abc' union all
select 'abc' union all
select 'abc'
select ITEM,
identity(int,1,1) 'rn1',
0 'rn2'
into #t
from A表
update #t
set rn2=(rn1-1)/3
select max(case when (rn1-rn2*3)=1 then ITEM else '' end) 'ITEM1',
max(case when (rn1-rn2*3)=2 then ITEM else '' end) 'ITEM2',
max(case when (rn1-rn2*3)=3 then ITEM else '' end) 'ITEM3'
from #t
group by rn2
/*
ITEM1 ITEM2 ITEM3
---------- ---------- ----------
abc abc abc
abc abc abc
abc
(3 row(s) affected)
*/