简体   繁体   中英

Converting columns into rows in oracle using sql only

How can I convert the columns as a rows of a table including the headers also in oracle.

slno id name
1    A  XXXX
2    B  YYYY
3    C  ZZZZ
4    D  MMMM

I want output like

     col1 col2 col3 col4
slno 1    2    3    4
id   A    B    C    D
name XXXX YYYY ZZZZ MMMM

I have worked it on ms sql, may be this should help, you can make some changes according to the oracle

select * from(
  select slno,id,name from [tablename]
) abc
pivot
(
  slno,id,name for [Group]
  in (col1,col2,col3)
)
as pvt

i was working with the adventure works 2012 database and there i used it here

select * from(
  select [CountryRegionCode],[Group],[SalesYTD] from [Sales].[SalesTerritory]
) abc
pivot
(
  sum([SalesYTD]) for [Group]
  in ([North America], [Europe],[Pacific])
)
as pvt

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