Hi @ajax6785 ,
Welcome to Microsoft Q&A!
Please also refer below:
create table tableaj
(ID int,
[Month] date,
Sub_created date,
status varchar(10),
tenure int)
insert into tableaj values
(100,'2017-02-01', '2017-02-01', 'active', 1),
(100 ,'2017-03-01', NULL,'active', 2),
(100 ,'2017-04-01',NULL,'active ',3),
(100 ,'2017-05-01', NULL,'churned', 3),
(100 ,'2021-02-01','2021-02-01', 'active', 1),
(100 ,'2021-03-01', NULL,'active ',2),
(100 ,'2021-04-01', NULL,'active', 3),
(100 ,'2021-05-01', NULL,'active', 4),
(100 ,'2021-06-01', NULL,'active', 5),
(100 ,'2021-07-01', NULL,'active ',6)
select id,[month],Sub_created=MAX(Sub_created) OVER (PARTITION BY c),status,tenure
from
(
select *,c=COUNT(Sub_created) OVER (PARTITION BY id ORDER BY [month])
from tableaj
) d
order by [month]
Output:
id month Sub_created status tenure
100 2017-02-01 2017-02-01 active 1
100 2017-03-01 2017-02-01 active 2
100 2017-04-01 2017-02-01 active 3
100 2017-05-01 2017-02-01 churned 3
100 2021-02-01 2021-02-01 active 1
100 2021-03-01 2021-02-01 active 2
100 2021-04-01 2021-02-01 active 3
100 2021-05-01 2021-02-01 active 4
100 2021-06-01 2021-02-01 active 5
100 2021-07-01 2021-02-01 active 6
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.