【发布时间】:2020-08-03 21:09:48
【问题描述】:
假设我有一张有两列的表 - customer_id 和 date:
customer_id date
1 2020-01-29
1 2020-03-14
2 2020-04-05
2 2020-02-18
我有另一个表格显示客户购买的日期和金额:
customer_id date amount
1 2019-12-03 10
1 2020-01-30 20
1 2020-03-10 30
1 2020-03-18 40
2 2020-02-29 50
2 2020-03-10 60
2 2020-04-01 70
我现在想在第一个表和第二个表上进行连接,创建一个新列,显示第一个表的每一行上迄今为止的购买金额的总和:
customer_id date amount_td
1 2020-01-29 10
1 2020-03-14 60
2 2020-04-05 180
2 2020-03-20 110
我该怎么做?我最初对逻辑的想法是这样的:
SELECT
table1.customer_id,
table1.date,
table2_agg as amount_td
FROM
table1
LEFT JOIN (
SELECT
customer_id,
SUM(amount)
FROM
table2
HAVING
table2.date <= table1.date
) table2_agg
ON
table1.customer_id = table2_agg.customer_id
当然,这在语法和逻辑上并不完全存在。
【问题讨论】:
-
还不错。您的子查询需要一个 GROUP BY。 HAVING 应该在 WHERE。 SUM() 列需要一个名称。
-
您使用的是哪个 dbms?
-
对不起,我复制粘贴到这里时错过了组。我正在使用 Presto,但我什至没有尝试运行这个想法,因为我对逻辑有点偏离
标签: sql join group-by aggregate-functions presto