需要用两个不同的where子句返回两组数据
我有一个跟踪交易的表格。
该表设置为:
transactions: id, account_id, budget_id, points, type
我需要返回每个budget_id的点的总和,其中types=“分配”和点的总和,其中types=“问题”
我知道如何做每一个,但不是在一个查询。
预期结果集:
budget_id allocated issued 434 200000 100 242 100000 5020 621 45000 3940
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
select budget_ID, sum(case when type = 'allocated' then points else 0 end) as allocated, sum(case when type = 'issued' then points else 0 end) as issued ..rest of your query... group by budget_ID
只有满足某个标准时,才可以使用个案总和。