mysql - 来自具有不同条件的不同表的多个 SUM

标签 mysql sql

我有 2 个表..其中一个被命名为“claim”

+----------+----------+--------+------------+
| claim_id | store_id | amount | created_by |
+----------+----------+--------+------------+
|        2 |        2 | 926.00 |          2 |
|        3 |        2 | 746.50 |          5 |
|        4 |        2 | 989.26 |          5 |
|        5 |        2 | 766.00 |          6 |
|        6 |        2 | 403.00 |          6 |
|        7 |        2 | 130.00 |          6 |
+----------+----------+--------+------------+

另一个“claim_progress”

+----------+------+------+---------------------+
| claim_id | type | paid | created             |
+----------+------+------+---------------------+
|        2 | S    |    0 | 2019-09-12 20:37:26 |
|        3 | S    |    0 | 2019-09-12 21:52:32 |
|        4 | S    |    0 | 2019-09-12 22:33:16 |
|        5 | S    |    0 | 2019-09-12 22:53:58 |
|        6 | S    |    0 | 2019-09-12 22:58:55 |
|        7 | S    |    0 | 2019-09-12 23:01:40 |
|        5 | A    |    0 | 2019-09-21 04:02:58 |
|        6 | A    |    0 | 2019-09-21 04:03:02 |
|        5 | PP   |  150 | 2019-09-21 04:03:10 |
|        5 | PP   |   45 | 2019-09-21 04:03:22 |
+----------+------+------+---------------------+

当然他们每个都有更多的列,但我将其简化为这样。 Claim_progress 是用于跟踪 claim 表的付款状态的表,从类型 S(提交)、A(批准)、PP(进度付款)、P(已付款)开始。 “created_by”列是提交 claim 的用户 ID。

我在这里试图做的是得到一个单行,它产生两列总金额和关于“created_by” claim 的剩余未付金额。这样我就需要获取每项 claim 的最新进展,确定其属于“A”类型还是“PP”类型。

“created_by”6 批准的 claim 总额应该是 1169,支付的总额应该是 195。顺便说一句,这是我到目前为止所得到的,但我想知道它是否是多余的,甚至是错误的?

SELECT SUM(c.amount),cp3.paid FROM claim c
JOIN (SELECT claim_id,MAX(created) as maxprogress FROM claim_progress GROUP BY claim_id) cp ON (c.id=cp.claim_id)
JOIN claim_progress cp2 ON (cp.maxprogress=cp2.created)
JOIN (SELECT claim_id,SUM(paid) AS paid FROM claim_progress GROUP BY claim_id) cp3 ON (cp3.claim_id=cp.claim_id)
WHERE cp2.type IN ('A','PP') AND c.created_by='6'

希望这里的DB高手能够指点迷津。谢谢。

编辑:

我期待的答案是这样的

+----------------+------+
| total_approved | paid |
+----------------+------+
|           1169 |  195 |
+----------------+------+

最佳答案

我觉得我应该能够通过另一个连接来压缩它,但我就是无法弄清楚,是时候关闭一周的电源了。您已经令人难以置信地接近最佳状态。这稍微简化了一些,但本质上与您所做的事情相同:

select sum(c.amount) Total_approved, sum(Progress.paid) paid from claim c
join (select claim_id, sum(paid) paid, max(created) created from claim_progress where type = 'A' group by Claim_id, type) Approved on c.claim_id = approved.claim_id
Left join (select claim_id, sum(paid) paid, max(created) created from claim_progress where type = 'PP' group by Claim_id, type) Progress on c.claim_id = progress.claim_id
Where c.created_by = 6

希望这有帮助!

关于mysql - 来自具有不同条件的不同表的多个 SUM,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58034905/

相关文章:

mysql - 我还可以使用这个多对多连接表执行哪些其他查询?

MySQL在临时表中创建任意日期或数字列表?

mysql - SQL - 无法使用多个聚合函数结果过滤表

mysql - 使用触发器自动增量列后不连续

php - 从数据库中提取数据并放入 html 表中

mysql - 为什么 MySQL 优化器不使用所有列索引?

mysql - 为每个 id 查找相同的记录

mysql - 查找同一单词的重音和非重音变体

mysql - 计算某个值在 case 语句中出现的次数

sql - 当您达到 SQL Server Express 4GB/10GB 限制时会发生什么?