[英]Oracle SQL - Rows into Columns
I was wondering if it is possible in to display some rows as columns in Oracle 11g. 我想知道在Oracle 11g中是否可以将某些行显示为列。
I want to the following 我要以下
id language text
--------------------------
1 english some example
1 german an example
2 english test
2 german test123
to be displayed like this: 像这样显示:
id english german
---------------------------------
1 some example an example
2 test test123
I tried using pivot
but wasn't able to figure out how to handle it correctly, since pivot
requires an aggregate function. 我尝试使用pivot
但由于pivot
需要聚合函数,因此无法弄清楚如何正确处理它。
Assuming you have one text per language per id, you can use max
or min
on the text column in pivot
. 假设每种ID每种语言只有一个文本,则可以在pivot
的文本列上使用max
或min
。
select * from tablename
pivot
(max(text) for language in ('english' as ENGLISH,'german' as GERMAN))
EDIT: Based on OP's comment I'm having trouble figuring out how the SELECT query works if I don't want to select everything in that case. 编辑:基于OP的注释,如果我不想在这种情况下选择所有内容,则我很难弄清楚SELECT查询的工作方式。 SELECT something, text FROM tablename or similar is not working 选择某些东西,文本从表名或类似名称不起作用
Remember you should select all the columns you are pivot
ing. 记住,您应该选择所有要pivot
的列。
select * from
(select text,language,id,something from tablename) t
pivot
(max(text) for language in ('english' as ENGLISH,'german' as GERMAN))
Edit: To unpivot
use 编辑:取消unpivot
使用
select *
from (
select * from
(select text,language,id,something from tablename) t
pivot
(max(text) for language in ('english' as ENGLISH,'german' as GERMAN))
) p
unpivot (text for language in (ENGLISH, GERMAN))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.