Search code examples
sqldb2unpivot

make 3 rows, 1 column from 1 row 3 colums


I have following table in db2:

id person 1 person 2 person 3
1 10 12 15

I now want to make a query that returns the following:

id person
1 10
1 12
1 15

how can I do this in db2?

Thanks in advance!


Solution

  • It's more efficiently not to scan the same potentially large table multiple times.
    Try this:

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