This is a follow-up to a couple of questions I have already asked here, but additional information has made things more complicated.
I have the list of customers, the accounts they owned, and the date the account was renewed. Nth Renewal indicates the n-th time the account was renewed. So if Nth Renewal = '1', this was the
first time the account was renewed/opened. If Nth Renewal = n not equal to 1, then that was the
n-th and last time the account was renewed.
Here is an example of one complicated customer:
CREATE TABLE #Table (
[Renewal Date] date,
[Customer] nvarchar(255),
[Account] nvarchar(255),
[Nth Renewal] nvarchar(255),
)
INSERT INTO #TABLE ([Renewal Date], [Customer], [Account], [Nth Renewal])
VALUES ('2017-09-29', 'John', '123', '1')
INSERT INTO #TABLE ([Renewal Date], [Customer], [Account], [Nth Renewal])
VALUES ('2022-01-31', 'John', '123', '6')
INSERT INTO #TABLE ([Renewal Date], [Customer], [Account], [Nth Renewal])
VALUES ('2017-10-31', 'John', '789', '1')
INSERT INTO #TABLE ([Renewal Date], [Customer], [Account], [Nth Renewal])
VALUES ('2019-01-31', 'John', '789', '3')
INSERT INTO #TABLE ([Renewal Date], [Customer], [Account], [Nth Renewal])
VALUES ('2019-03-29', 'John', '456', '1')
INSERT INTO #TABLE ([Renewal Date], [Customer], [Account], [Nth Renewal])
VALUES ('2022-03-31', 'John', '456', '4')
Note that the accounts/information is sorted by the 1st Renewal Date where Nth Renewal = '1'
My objective is to add a column that tells me whether John switched accounts or owned two
accounts at the same time.
The logic compares the Renewal Date for each Account. If the 1st Renewal Date of the
account <= Last Renewal Date of the previous account, then it is an Overlap because it means the
customer owned 2 accounts at the same time. However, if the 1st Renewal Date of the
account > Last Renewal Date of the previous account, then it is a Switch because the customer
simply switched accounts. I implemented this logic with the following code which produced the following sample of my results:
WITH CTE AS (
SELECT [Renewal Date]
,[Customer]
,[Account]
,[Nth Renewal]
,MIN([Renewal Date]) OVER (PARTITION BY [Account], [Customer]) AS [First Date]
FROM [#FirstLastAnniv]
)
SELECT [Renewal Date]
,[Customer]
,[Account]
,[Nth Renewal]
,CASE WHEN [Customer] = LAG([Customer]) OVER (ORDER BY [Customer], [Renewal Date]) AND
[Nth Renewal] = '1'
THEN CASE WHEN [Renewal Date] > LAG([Renewal Date]) OVER (ORDER BY [Customer], [First Date], [Account], [Renewal Date])
THEN 'Switch'
WHEN [Renewal Date] <= LAG([Renewal Date]) OVER (ORDER BY [Customer], [First Date], [Account], [Renewal Date])
THEN 'Overlap'
END AS [Switch/Overlap]
FROM CTE
ORDER BY [Customer], [First Date], [Account], [Renewal Date]
Renewal Date Customer Account Nth Renewal Switch/Overlap
2017-09-29 John 123 1 NULL
2022-01-31 John 123 6 NULL
2017-10-31 John 789 1 Overlap
2019-01-31 John 789 3 NULL
2019-03-29 John 456 1 Switch
2022-03-31 John 456 4 NULL
So, the results show that John still owned Account = '123' when he opened Account = '789' so
it was an Overlap. But when he opened Account = '456', he had already closed Account = '789' so that's why it says it's a Switch.
However, I realize this this logic is incomplete because I cannot just compare the first Renewal Date with last Renewal Date of the previous account only. It should be with the last Renewal Date of ALL PREVIOUS ACCOUNTS. In this example, even though John dropped Account = '789', he still owned Account = '123' when he opened Account = '456', so it should be an Overlap, not a Switch. But I don't know how implement this in SQL.
Any ideas?
Thank you.