【发布时间】:2014-10-04 22:04:32
【问题描述】:
我有一张记录交易的表格。
表格设置为:
transactions:
id, account_id, budget_id, points, type
我需要返回每个budget_id 的type = 'allocation' 的总和和type = 'issue' 的总和
我知道如何做每一个,但不能在一个查询中同时做。
预期结果集:
budget_id allocated issued
434 200000 100
242 100000 5020
621 45000 3940
【问题讨论】:
-
字段列表中的子查询或子查询的连接
-
例如让你开始
SELECT budget_id, A.all_sum AS allocated, I.iss_sum AS issued FROM transactions INNER JOIN (SELECT SUM(points) AS all_sum FROM transactions WHERE type='Allocation') AS A INNER JOIN (SELECT SUM(points) FROM transactions WHERE type='Issue') AS I -
@scrowler - 会起作用,但如果分组相同,您可以使用大小写条件求和。
-
嗨布拉德。以下任何一个答案对您有帮助吗?如果是这样,请考虑将其中一项标记为已接受。