简体   繁体   English

仅使用SQL在Oracle中将列转换为行

[英]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. 如何在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 我已经在ms sql上工作了,可能对您有所帮助,您可以根据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 我正在使用Adventure Works 2012数据库,在这里使用了它

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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