Hi @thejesh 00 ,
I modified some sample data and you could refer below query and check whether it is helpful to you.
create table claimstable
(id int ,
claimnumber varchar(20),
claimstate varchar(100),
claimsdate date)
insert into claimstable (id ,claimnumber ,claimstate,claimsdate )
values (8,' CDX20-221708' ,'Patient Statements Prepared',' 2021-04-29'),
(8, 'CDX20-221707',' Ready for Patient Statements', '2021-04-13 '),
(8, 'THX20-050567', 'Claim Status Requested', '2021-02-06'),
(8, 'THX20-050566',' Claim Status Requested', '2021-01-31'),
(8 ,'CP0G002823', 'Exception', '2021-03-29 '),
(8, 'CP0G002813', 'Exception' ,'2021-03-26 '),
(8, 'CP0G002814 ','Exception' ,'2021-02-23 '),
(8, 'MML20-003758', 'Accepted', '2021-04-29 '),
(8 ,'CP0G002817', 'Exception', '2021-02-15 '),
(8, 'CP0G002818 ','Exception' ,'2021-01-19 ')
;with cte as (
select b.idname,a.claimnumber,a.claimstate,DATEDIFF(DD,claimsdate,GETDATE()) range
from claimstable a
inner join org b on a.id=b.id)
select idname,claimstate,'0-20 days' ageroup ,count(claimnumber) count
from cte
where range>=0 and range<20
group by idname,claimstate
union
select idname,claimstate,'20-40 days' ageroup ,count(claimnumber) count
from cte
where range>=20 and range<40
group by idname,claimstate
union
select idname,claimstate,'40-60 days' ageroup ,count(claimnumber) count
from cte
where range>=40 and range<60
group by idname,claimstate
union
select idname,claimstate,'60-80 days' ageroup ,count(claimnumber) count
from cte
where range>=60 and range<80
group by idname,claimstate
union
select idname,claimstate,'80-100 days' ageroup ,count(claimnumber) count
from cte
where range>=80 and range<100
group by idname,claimstate
union
select idname,claimstate,'100-120 days' ageroup ,count(claimnumber) count
from cte
where range>=100 and range<120
group by idname,claimstate
Output:
idname claimstate ageroup count
THERANOSTIX INC. Claim Status Requested 100-120 days 1
THERANOSTIX INC. Ready for Patient Statements 20-40 days 1
THERANOSTIX INC. Accepted 0-20 days 1
THERANOSTIX INC. Claim Status Requested 80-100 days 1
THERANOSTIX INC. Exception 100-120 days 1
THERANOSTIX INC. Exception 40-60 days 2
THERANOSTIX INC. Exception 80-100 days 2
THERANOSTIX INC. Patient Statements Prepared 0-20 days 1
If above is not working, please provide your updated sample data and expected output.
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.