我有一张桌子,比如说,1000 行。其中一列是 URL。
因此,从表中选择 URL。
但是我想每行显示 12 个 URL。原因是我们正在发布供人们在线进行的工作,他们一次将审查 12 个网站。
因此,在这种情况下,我们将输出大约 90 行,每行有 12 列(除了最后一行会短一些)。
有没有一种简单的方法可以做到这一点?这是枢轴或其他功能的情况吗?
动态 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)
这是在线解决方案。虽然看起来有点复杂
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
这是没有 Pivot 和 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
请尝试以下查询,它适用于 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"));