我有一张表,比如1000行。其中一列是URL。
因此,请从表格中选择网址。
但是我想每行显示12个网址。原因是我们正在为人们发布在线工作,他们将一次审查12个网站。
因此,在这种情况下,我们将输出大约90行,每行有12列(最后一行除外几行)。
有直接的方法吗?这是枢轴或其他功能的情况吗?
答案 0 :(得分:0)
动态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)
答案 1 :(得分:0)
这是在线解决方案。虽然
,但看起来有点复杂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
答案 2 :(得分:0)
请尝试以下查询,它适用于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"));