Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

need to return two sets of data with two different where clauses

Tags:

sql

mysql

I have a table that keeps track of transactions.

The table is setup as:

transactions:

id, account_id, budget_id, points, type

I need to return each budget_id's sum of points where type = 'allocation' and sum of points where type = 'issue'

I know how to do each, but not both in one query.

expected result set:

budget_id   allocated   issued
   434       200000      100
   242       100000      5020
   621       45000       3940
like image 598
Brad Avatar asked Sep 05 '25 09:09

Brad


1 Answers

SELECT budget_id, 
       SUM(IF(type = 'allocation', points, 0)) AS allocated,
       SUM(IF(type = 'issue', points, 0)) AS issued
FROM transactions
GROUP BY budget_id
like image 177
Barmar Avatar answered Sep 08 '25 02:09

Barmar