Hi @db042190 ,
Please refer below query and check whether it is helpful to you.
;with cte as
(
select id,bizkey,bizvalue, LastUpdateDate startdate ,
case when lag(app) OVER (PARTITION BY bizkey ORDER BY bizvalue desc)<>'PURGE' then lag(LastUpdateDate) OVER (PARTITION BY bizkey ORDER BY bizvalue desc) else '9999-12-31' end enddate
from [msdn])
select a.id,a.bizkey,a.bizvalue,CONVERT(nvarchar(30), a.LastUpdateDate, 101) LastUpdateDate,
case when isnull(a.JournalUpdateCode,'')='' then 'Y' when isnull(JournalUpdateCode,'') in ('D','I') then NULL else 'N' end currentflag,
case when isnull(JournalUpdateCode,'') not in ('D','I') then CONVERT(nvarchar(30), b.startdate, 101) else NULL end startdate,
case when isnull(JournalUpdateCode,'') not in ('D','I') then CONVERT(nvarchar(30), b.enddate, 101) else NULL end enddate
from [msdn] a
left join cte b
on a.id=b.id
where a.BizKey in (1,3)
Output:
id bizkey bizvalue LastUpdateDate currentflag startdate enddate
1 1 10 10/28/2020 NULL NULL NULL
2 1 10 10/27/2020 N 10/27/2020 12/31/9999
3 1 9 10/26/2020 N 10/26/2020 10/27/2020
4 1 8 10/25/2020 N 10/25/2020 10/26/2020
5 1 7 10/24/2020 N 10/24/2020 10/25/2020
6 1 7 10/24/2020 NULL NULL NULL
13 3 11 09/28/2020 NULL NULL NULL
14 3 11 09/28/2020 Y 09/28/2020 09/28/2020
You could modify above query if necessary or depend on your own rules.
If above is not working, please provide more details about the rule, for example, the output of bizkey=2.
In addition, agreed with Erland, there is usually not any problem with LAG when it comes to performance.
Best regards
Melissa
If the answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
Hot issues October--Users always get connection timeout problem when using multi subnet AG via listener. Especially after failover to another subnet