I am unsure why you think you cannot accomplish this with an UNPIVOT
and a PIVOT
:
select [1], [2], [3], [4], [5]
from
(
select *
from
(
select col1, col2, col3,
row_number() over(order by col1) rn
from yourtable
) x
unpivot
(
val for col in (col1, col2, col3)
) u
) x1
pivot
(
max(val)
for rn in ([1], [2], [3], [4], [5])
) p
See SQL Fiddle with Demo. This could also be performed dynamically if needed.
Edit, if the column order needs to be kept, then you can use something like this, which applies the row_number()
without using a order by
on one of the columns in your table (here is an article about using non-deterministic row numbers):
select [1], [2], [3], [4], [5]
from
(
select *
from
(
select col1, col2, col3,
row_number()
over(order by (select 1)) rn
from yourtable
) x
unpivot
(
val for col in (col1, col2, col3)
) u
) x1
pivot
(
max(val)
for rn in ([1], [2], [3], [4], [5])
) p;
See SQL Fiddle with Demo
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…