简体   繁体   中英

Transpose row to column SQL server 2015

Need help to transpose the data from rows to columns

col1  col2   col3
-------------------
d1    d2      d3

Result should be

col
----
d1
d2
d3

Using Cross Apply

;WITH CTE(col1,col2,col3)
AS
(
SELECT 'd1','d2','d3'
)
SELECT col FROM CTE
CROSS APPLY (VALUES(col1),(col2),(col3)) As Dt (col);

Result,Look Demo: http://rextester.com/LVXO57980

col
---
d1
d2
d3

You could use this:

SELECT col1 AS col
FROM table_name
UNION ALL 
SELECT col2 AS col
FROM table_name
UNION ALL 
SELECT col3 AS col
FROM table_name
ORDER BY col;

Use this

Select col 
from tablename 
     unpivot ( 
        col
        FOR unpivot_columns IN ([col1],[col2],[col3])
     ) as unpvt;

Use this link for UNPIVOT Example

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM