简体   繁体   English

在Oracle SQL中将行转换为列

[英]converting rows into column in Oracle SQL

Hi i want to convert column into rows. 嗨,我想将列转换为行。 Can anyone please tell me how can i do it. 谁能告诉我我该怎么做。

Input:- 输入:

id name
10 shruti
20 Wipro

Output should be 输出应为

id   10     20
name shruti wipro

Please note there can be n number of columns. 请注意,列数可以为n。 What would be the most efficient way of doing it. 什么是最有效的方法。

declare
sqlqry clob;
cols clob;
begin
 select String_agg('''' || id || ''' as "' || id || '"', ',') 
 into   cols
 from   (select distinct id from your_table);


 sqlqry :=
  '      
  select * from
 (
  select *
  from your_table
 )
 pivot
(
max(name) for id in (' || cols  || ')
 )';

execute immediate sqlqry;
end;
/

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

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