【问题标题】:Convert rows to columns oracle SQL将行转换为列 oracle SQL
【发布时间】:2015-07-18 02:09:22
【问题描述】:

我没有找到任何合适的先前答案,因此发布了这个问题。我需要将行转换为列。 PIVOT 示例都将行转换为单列,而我的要求是多列。我的桌子是这样的

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

感谢您的建议/意见!

【问题讨论】:

    标签: oracle pivot


    【解决方案1】:

    您可以使用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))
    

    【讨论】:

      【解决方案2】:

      如果“ID”列是主键,则表中只能有一个列作为主键。

      【讨论】:

      • Het CB,我只是称它为 ID,没有它不是主键
      • ok,你可以这样创建四列:Type|ID|Type_2|_ID_2 or Type_1|ID_1|Type_2|_ID_2 然后进行数据迁移
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-12-08
      • 2020-07-01
      • 2020-02-23
      • 2015-04-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多