如何在Oracle中实现数据透视输出

时间:2016-09-13 16:09:01

标签: sql oracle oracle11g pivot-table

来源如下:

NAME |SEX
-----+----
A    |MALE
B    |FEMALE
C    |MALE
D    |FEMALE
E    |MALE

必需的输出,将第二列中的值作为标题旋转,如下所示:

MALE FEMALE
A     B
C     D
E

1 个答案:

答案 0 :(得分:0)

对于Oracle:

select * from (
select * from [table name]
)src 
pivot
(max(name) for sex in ('Male' as MALE,'Female' as FEMALE))pvt

对于SQL Server:

select * from (
select * from [table name]
)src 
pivot
(max(name) for sex in ([Male],[Female]))pvt