将行转换为列 oracle SQL

Convert rows to columns oracle SQL

我没有找到任何 suitable 以前的答案,因此发布了这个问题。我需要将行转换为列。 PIVOT 示例都将行转换为单列,而我的要求是多列。我的 table 看起来像这样

Type     ID
test1    10
test1    20
test1    30
test2    10
test2    40

我希望它是

Type       ID        Type      ID
test1      10       test2      10
test1      20       test2      40
test1      30

感谢您的suggestions/inputs!

如果 'ID' 列是主键,则 table 中只能有一个列作为主键。

您可以使用 row_number() 枚举行并使 pivot:

SQLFiddle demo

select * 
  from (
    select d.*, row_number() over(partition by type order by id) rn from data d)
  pivot (max(type) type, max(id) id for type in ('test1' t1, 'test2' t2))