我在Oracle中有以下数据
Number Text
12345 ab
12345 abc
12345 acbd
如何将其转换为:
Number Text1 Text2 Text3
12345 ab abc abcd
如何在Oracle中使用SQL执行此操作?
答案 0 :(得分:1)
为了将多行数据旋转到列中,首先我会使用row_number()
,然后您可以使用带有CASE表达式的聚合函数来获得最终结果:
select "Number",
max(case when seq = 1 then "Text" end) Text1,
max(case when seq = 2 then "Text" end) Text2,
max(case when seq = 3 then "Text" end) Text3
from
(
select "Number", "Text",
row_number() over(partition by "Number" order by "Text") seq
from yourtable
) d
group by "Number";