Im kinda new to SQL Server.
-
Dataset: Fact_Centre_Txn
Centre_Id Cust_Id Visit_Date Transaction_Id Transaction_Amt
N02 2 2022-04-02 16:13:00.000 1 209
N02 14 2022-04-28 13:22:00.000 2 187
N01 8 2022-02-14 00:12:00.000 3 15
E01 6 2022-02-11 16:36:00.000 4 111
N01 1 2022-03-10 03:51:00.000 5 -298
W01 10 2022-03-04 03:05:00.000 6 -401
W01 4 2022-03-09 22:39:00.000 7 65
N01 5 2022-02-13 23:14:00.000 8 44
S02 15 2022-05-02 15:36:00.000 9 -30
S02 15 2022-02-13 05:28:00.000 10 -27
…and so on -
Requirement: Display the average number of days between a customer’s last and second to the last transaction
-
What I got: Figured out maybe I can use ROW_NUMBER and select the Visit_Date with row_num 2 and 3 (PARTITION BY Cust_Id and ORDER BY Visit_Date DESC) corresponding with the last date and second date to the last transaction. Not quite figuring out what to do next with that idea
All helps are appreciated!
phong nguyễn is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.