I have a table like this named people
:
first_name | id | total | type |
---|---|---|---|
Joe | 1234 | 13 | onsite |
Joe | 1234 | 9 | remote |
Jane | 4321 | 99 | remote |
Jane | 4321 | 1 | onsite |
I want to query it and have the results returned like this:
first_name | id | onsite | remote |
---|---|---|---|
Joe | 1234 | 13 | 9 |
Jane | 4321 | 1 | 99 |
I believe I need to use Pivot() but this is advanced for me
Thank you
I tried grouping by first_name but that doesn’t move the rows to columns
2
Answers
Here’s the
conditional aggregation
method, which is easy to comprehend and IMO recommended if you have just a couple different "types".Use aggregate
filter
clause. Demo at db<>fiddle:Or look into
crosstab()
.