我正在尝试返回客户ID列表,其中包含他们在首次付款后30天内完成的所有付款之和。我尝试了
select
P.CustomerID,
SUM(P.TransactionAmount)
from Customer_Payments P
where P.dDatePaymentReceived < date_add(min(P.dDatePaymentReceived), INTERVAL 30 DAY)
group by P.iCustomerID;
但是这行不通,因为我无法在where子句的min()
中包含date_add
。有没有简单的方法解决这个问题?
答案 0 :(得分:0)
您将必须执行子查询来获取min(),因为否则您将跨越不同级别/聚合时间。因此,请在子查询中进行汇总,以使用相关子查询获取客户的min(dDatePaymentsReceived),然后像在主查询中计划的那样进行汇总。
select
P.CustomerID,
SUM(P.TransactionAmount)
from Customer_Payments P
where P.dDatePaymentReceived < date_add((SELECT min(P.dDatePaymentReceived) FROM Customer_Payments CP WHERE cp.Customer_id = p.CustomerID) , INTERVAL 30 DAY)
group by P.iCustomerID;