What I want to achieve to find the previous payment date for an account based on contact date and payment date.
select account, PaymentDate, contactdate, trans_amt
,lag(PaymentDate,1,'10010101')
over(PARTITION BY PaymentDate, account ORDER BY PaymentDate )
as Prev_PaymentDate
from Table1
Result of query:
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
In the sample, customer made 3 payments. Jan 4, Jan 11 and Feb 5.
What I want to achieve is for Prev_PaymentDate to be:

Prev_PaymentDate
10010101
10010101
20210104
20210104
20210111
20210111
20210111
The second part is to write a query to only show a table where the result will be 3 rows. The payment made on Jan 11th is removed because contact was made prior and paid on the 4th. The payment on Feb 5th is attributed to the contact from Jan 28th.
Thank you for any help extended.
