0

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!

2
  • Are you guaranteed to have only 3 categories? Commented Apr 12, 2016 at 16:58
  • I have more than 3 categories but the number is fixed, is not dynamic. I have a total of 10 categories. Some categories can have more than one value (like in the example with Cat03) and, for those categories that have one value, repeat the value on the categories that have multiple values Commented Apr 12, 2016 at 17:07

1 Answer 1

1

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'
Sign up to request clarification or add additional context in comments.

2 Comments

If I dont filter by ID, it will do all combinations and will have more values, right ??
Yes. I wasn't sure what you meant to do with ID, since your example only has a single value for it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.