I’m pulling from a database which has multiple records for the same id, and I need to pull it all into the same table output:
ID | Type | Value | Value2 |
---|---|---|---|
1 | Age | 10 | |
1 | Height | 136 | |
1 | Name | Jeff | |
2 | Age | 12 | |
2 | Height | 156 | |
2 | Name | Ben |
And I want my output to look like this:
ID | Name | Age | Height |
---|---|---|---|
1 | Jeff | 10 | 136 |
2 | Ben | 12 | 156 |
I’ve tried self refrencing the table, but I can’t work out how to do multiple WHERE statements to return different values based on different inputs.
Thanks!
2
Answers
We can use conditional aggregation here to unpivot the data:
You can use aggregation with case when in order to built something like this: