Aleps
Aleps

Reputation: 21

Pivot table in PostgreSQL repeating values in columns

Having the following data in a table:
ID --------- Category --------- Value
1234 -------- Cat01 ----------- V001
1234 -------- Cat02 ----------- V002
1234 -------- Cat03 ----------- V003
1234 -------- Cat03 ----------- V004
1234 -------- Cat03 ----------- V005

I want to have the following output:

ID --------- Cat01 --------- Cat02 --------- Cat03
1234 ------- V001 ---------- V002 ---------- V003
1234 ------- V001 ---------- V002 ---------- V004
1234 ------- V001 ---------- V002 ---------- V005

How can it be done in PostgreSQL. As you can see, the value in Cat01 and Cat02 columns are repeated for each entry in Cat03 column
Many thanks for your help!

Upvotes: 0

Views: 71

Answers (1)

David Aman
David Aman

Reputation: 291

How about something like this:

SELECT a.val AS cat01, b.val AS cat02, c.val AS cat03
FROM
    test_pivot AS a,
    test_pivot AS b,
    test_pivot AS c
WHERE
    a.category = 'Cat01'
    AND
    b.category = 'Cat02'
    AND
    c.category = 'Cat03'

Upvotes: 1

Related Questions