【问题标题】:make 3 rows, 1 column from 1 row 3 colums从 1 行 3 列制作 3 行 1 列
【发布时间】:2021-12-22 04:47:06
【问题描述】:

我在 db2 中有下表:

id person 1 person 2 person 3
1 10 12 15

我现在想做一个返回以下内容的查询:

id person
1 10
1 12
1 15

如何在 db2 中做到这一点?

提前致谢!

【问题讨论】:

    标签: sql db2 unpivot


    【解决方案1】:

    不多次扫描同一个可能很大的表会更有效。
    试试这个:

    SELECT T.ID, V.PERSON
    FROM MYTAB T, TABLE (VALUES T.PERSON1, T.PERSON2, T.PERSON3) V (PERSON);
    

    【讨论】:

    • 它确实有更好的性能,你!
    【解决方案2】:

    使用 UNION:

    select id, person1 as person
    from the_table
    union all 
    select id, person2
    from the_table
    union all 
    select id, person3
    from the_table
    

    【讨论】:

    • 感谢您的快速回复
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-11-06
    • 1970-01-01
    • 2015-05-25
    • 2020-03-31
    • 1970-01-01
    • 2020-02-12
    • 1970-01-01
    相关资源
    最近更新 更多