Skip to content
Advertisement

How to join grouped and filtered fields on table?

I have this dataset that I want to group by user_id and sum the values by category

id user_id category value
0 1 1 10
1 1 1 20
2 1 2 10
3 1 2 30
4 1 3 10
5 2 3 40

And I want a select returning a table with one element for user_id with one field for each category value (These are known and can only be 1, 2 or 3)

user_id category_1 category_2 category_3
1 30 40 10
2 0 0 40

The only solution I’ve found is:

Advertisement

Answer

Perhaps a simple conditional aggregation

Example

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement