I want to do conditional aggregation in Postgres. I have a table with an id column and a float items column.
The query I want to write is something like this:
SELECT ( ((SUM(items) * 3) WHERE id="123"))
+ ((SUM(items) * 2) WHERE items="124")) )
FROM mytable
And I'd like to get a single number out. So for example, if my table looked like this:
org_id items
123 10
123 3
124 12
I'd like to get back the single number 63 (i.e. 13*3 + 12*2).
I'm pretty sure I need a CASE statement, but I'm not sure how to implement it.