在Oracle中将列转置为行,并保留列标题。

huangapple go评论48阅读模式
英文:

Transpose columns to rows with column header in Oracle

问题

已尝试在Oracle中将列转换为行,但列标题应为第一列的值。

注意:带有WHERE子句的SELECT语句。尝试了PIVOT和UNPIVOT,但不幸的是,我没有获得期望的输出。

它应该转换为

在Oracle中将列转置为行,并保留列标题。

请协助。

英文:

Have tried to convert the columns to rows in oracle but the column headers should be the first column values.

Note: The select statement with WHERE clause. Have tried PIVOT and UNPIVOT but unfortunately I am not getting the desired output.

在Oracle中将列转置为行,并保留列标题。

It should be converted to

在Oracle中将列转置为行,并保留列标题。

Please assist.

答案1

得分: 1

Unpivot, as you said.

Sample data:

SQL> with test (column1, column2, column3) as
2 (select 385, 0, 29 from dual)

Query:

3 select *
4 from test
5 unpivot (value for type in (column1, column2, column3));

TYPE VALUE


COLUMN1 385
COLUMN2 0
COLUMN3 29

SQL>;

英文:

Unpivot, as you said.

Sample data:

SQL> with test (column1, column2, column3) as
  2    (select 385, 0, 29 from dual)

Query:

  3  select *
  4  from test
  5  unpivot (value for type in (column1, column2, column3));

TYPE         VALUE
------- ----------
COLUMN1        385
COLUMN2          0
COLUMN3         29

SQL>

huangapple
  • 本文由 发表于 2023年2月27日 18:20:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/75579204.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定