Home > Back-end >  SQL how to group by ID and create another column based on value
SQL how to group by ID and create another column based on value

Time:10-28

I'm new to SQL and I would like to to group by ID and create another column based on value.

Let me give an example:

Foreign ID Type Value
1 'X' 10
1 'Y' 20
2 'X' 30
2 'Y' 40

My expected output would be

Foreign ID X Y
1 10 20
2 30 40

CodePudding user response:

Grouping is done with Group By. And making a column out of a row, which is what the example shows is done with Crosstab function.

CodePudding user response:

I believe that a basic Left Join should do: https://www.postgresqltutorial.com/postgresql-tutorial/postgresql-left-join/

Also, write the code, it hepls.

  • Related