我无法找到我的客户在哪一天达到他们赚取的收入的特定阈值。
customer_id | Amount | created_at
---------------------------
1134 | 10 | 01.01.2010
1134 | 15 | 02.01.2010
1134 | 5 | 03.24.2010
1235 | 10 | 01.03.2010
1235 | 15 | 01.03.2010
1235 | 30 | 01.03.2010
1756 | 50 | 01.05.2010
1756 | 100 | 01.25.2010
为了确定他们赚了多少,我运行了一个简单的查询,如下所示:
SELECT customer_id, SUM(amount)
FROM table GROUP BY customer_id
但我需要能够找到例如客户总金额达到 100 美元的日期。
非常感谢任何帮助。谢谢!
最佳答案
杰西,
我相信您正在寻找“运行总计”计算的版本。
看看这篇文章calculate-a-running-total . 那里有许多有用的链接。
这篇文章有很多代码,您也可以重用:http://www.sqlteam.com/article/calculating-running-totals .
关于sql - 从 SUM 中查找日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4627510/