I am trying to convert rows to columns in postgres using crosstab or any other ways
Table 1:
Order_Id | Order_line_id |
---|---|
1 | 1001 |
1 | 1 |
1 | 2 |
Table 2:
Order_Id | Order_line_id | Type | Amount |
---|---|---|---|
1 | 1001 | APPLE | 60 |
1 | 1001 | APPLE | 90 |
1 | 1 | APPLE | 0 |
1 | 1 | ORANGE | 32 |
1 | 1 | KIWI | 45 |
1 | 2 | APPLE | 12 |
1 | 2 | ORANGE | 76 |
1 | 2 | ORANGE | 98 |
Result:
Order_Id | Order_line_id | APPLE1 | APPLE2 | ORANGE1 | ORANGE2 | KIWI1 | KIWI2 |
---|---|---|---|---|---|---|---|
1 | 1001 | 60 | 90 | null | null | null | null |
1 | 1 | 0 | null | 32 | null | null | 45 |
1 | 2 | 12 | null | 76 | 98 | null | null |
Column names are known already but the column values might be duplicate and they should be go next to each other.
i tried hard with cross tab and json (atleast tried to bring in json) couldnt progress. any help pls?
I tried to transpose rows to columns but the columns values may be duplicate. duplicate values must still be in separate column. I tried to achieve in crosstab but it didnt work