问题
I have a table with, say, 1000 rows. One of those columns is URL.
So, select URL from table.
However I want to display 12 URLs per row. The reason is that we are publishing work for people to do online and they will review 12 websites at a time.
So in this case we would have about 90 rows being output, each having 12 columns (except the last row which will be short a few).
Is there a straightforward way to do this? Is this a case for pivot or some other function?
回答1:
Simple example with dynamic PIVOT
CREATE TABLE dbo.Url
(
Id int IDENTITY,
url nvarchar(max)
)
INSERT dbo.Url
VALUES ('http://www.url1.com'),
('http://www.url2.com'),
('http://www.url3.com'),
('http://www.url4.com')
DECLARE @cols AS nvarchar(MAX),
@query AS nvarchar(MAX)
SELECT @cols = STUFF((SELECT DISTINCT ',' + QUOTENAME(ROW_NUMBER() OVER (ORDER BY Url))
FROM dbo.url
GROUP BY Url
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)'), 1, 1, '')
SET @query = 'SELECT' + @cols + ' FROM
(
SELECT Id, url
FROM dbo.url
) x
PIVOT
(
MAX(url)
FOR Id IN (' + @cols + ')
) p '
EXECUTE(@query)
回答2:
Here's online solution. It may looks a little complicated, though
declare @Temp_URLS table (URL nvarchar(128))
declare @Temp_Reviewers table (Name nvarchar(128))
insert into @Temp_Reviewers (Name)
select top 5
'Reviewer' +
right('0000' +
cast(row_number() over (order by number) as nvarchar(128)), 4)
from master.dbo.spt_values
insert into @Temp_URLS (URL)
select top 30
'http://URL' +
right('0000' +
cast(row_number() over (order by number) as nvarchar(128)), 4) + '.com'
from master.dbo.spt_values
select
Name,
[1], [2], [3], [4], [5], [6], [7], [8], [9], [10], [11], [12]
from
(
select TT.Name, row_number() over (order by TT.Name asc) as RowNum
from @Temp_Reviewers as TT
) as R
outer apply
(
select top 12 TT.URL, row_number() over (order by TT.URL) as RowNum
from
(
select TTT.URL, 1 + row_number() over (order by TTT.URL asc) / 12 as RowNum
from @Temp_URLS as TTT
) as TT
where TT.RowNum = R.RowNum
) as CALC
pivot
(
min(CALC.URL)
for CALC.RowNum in
([1], [2], [3], [4], [5], [6], [7], [8], [9], [10], [11], [12])
) as PVT
Here's solution without Pivot and Row_Number
declare @Temp_URLS table (URL nvarchar(128), csikey int identity(1, 1))
insert into @Temp_URLS (URL)
select top 30
'http://URL' +
right('0000' +
cast(row_number() over (order by number) as nvarchar(128)), 4) + '.com'
from master.dbo.spt_values
select
A.RowNum1,
min(case when A.RowNum2 = 1 then A.URL else null end) as [1],
min(case when A.RowNum2 = 2 then A.URL else null end) as [2],
min(case when A.RowNum2 = 3 then A.URL else null end) as [3],
min(case when A.RowNum2 = 4 then A.URL else null end) as [4],
min(case when A.RowNum2 = 5 then A.URL else null end) as [5],
min(case when A.RowNum2 = 6 then A.URL else null end) as [6],
min(case when A.RowNum2 = 7 then A.URL else null end) as [7],
min(case when A.RowNum2 = 8 then A.URL else null end) as [8],
min(case when A.RowNum2 = 9 then A.URL else null end) as [9],
min(case when A.RowNum2 = 10 then A.URL else null end) as [10],
min(case when A.RowNum2 = 11 then A.URL else null end) as [11],
min(case when A.RowNum2 = 12 then A.URL else null end) as [12]
from
(
select
T1.csikey, T1.URL,
count(T2.csikey) / 12 + 1 as RowNum1,
count(T2.csikey) % 12 + 1 as RowNum2
from @Temp_URLS as T1
left outer join @Temp_URLS as T2 on T2.csikey < T1.csikey
group by T1.csikey, T1.URL
) as A
group by A.RowNum1
回答3:
Please try below query, it works in Oracle 11G:
SELECT * FROM(
SELECT URL,
CAST(FLOOR((NUM+2)/3) AS INT) AS Row_Num,
MOD(NUM+2, 3)+1 AS OrderNum
FROM(
SELECT
ROW_NUMBER() OVER (ORDER BY URL) as NUM, URL
FROM
TBL_URL
)T1
)T2 PIVOT (MIN(URL) FOR OrderNum IN ('1' AS "1", '2' AS "2", '3' AS "3"));
来源:https://stackoverflow.com/questions/12890467/split-rows-into-12-columns