【发布时间】:2021-08-28 16:03:24
【问题描述】:
我想要根据联系日期和付款日期查找帐户的上一个付款日期。
select account, PaymentDate, contactdate, trans_amt
,lag(PaymentDate,1,'10010101')
over(PARTITION BY PaymentDate, account ORDER BY PaymentDate )
as Prev_PaymentDate
from Table1
查询结果:
Row account paymentDate contactdate trans_amt Prev_PaymentDate
1 123 20210104 20210102 29.7 10010101
2 123 20210104 20210104 29.7 20210104
3 123 20210111 20210102 6.3 10010101
4 123 20210111 20210104 6.3 20210111
5 123 20210205 20210102 33 10010101
6 123 20210205 20210104 33 20210205
7 123 20210205 20210128 33 20210205
在示例中,客户进行了 3 次付款。 1 月 4 日、1 月 11 日和 2 月 5 日。
Prev_PaymentDate
10010101
10010101
20210104
20210104
20210111
20210111
20210111
第二部分是编写查询以仅显示结果为 3 行的表。 1 月 11 日的付款已被删除,因为之前已联系并在 4 日付款。 2 月 5 日的付款归因于 1 月 28 日的联系人。
感谢您提供的任何帮助。
【问题讨论】: