As a part of my work I need to create profiles of claims settlement. Here is a quick idea about my work.
We receive claims data. I need to create report of profiles of claims where we mention range of claim amount and corresponding number of claims and total reimbursement within the range. The example could be-
625 claims of $0 - $1000 with total reimbursement of $510k,
862 claims of $1001 - $5000 with total reimbursement of $3.01 million,
334 claims of $5001 - $10000 with total reimbursement of $2.6 million.
There is always some change in the band or even number of bands. So next time the same range could be requirement of 6 bands, instead of 3. I have automated almost all other part of report. But not able to do this. I could create a procedure where I could mention the number of bands required and the lower/upper bounds of the bands. But then it is not fully automated. My idea is that I mention at the beginning of the query, the number of bands and the bounds. After that I need not change any part of the query.
Below is my effort
DECLARE @Range1 varchar(10)
DECLARE @Range2 varchar(10)
DECLARE @Range3 varchar(10)
SET @Range1 = '0'
SET @Range2 = '1000'
SET @Range3 = '5000'
SELECT
CASE WHEN ROUND(clmamt, 0) between ' + @Range1 + ' and ' + @Range2 + ' THEN ''1. $' + @Range1 + ' - $' + @Range2 + '''
WHEN ROUND(clmamt, 0) between ' + @Range2 + ' and ' + @Range3 + ' THEN ''2. $' + @Range2 + ' - $' + @Range3 + '''
WHEN ROUND(clmamt, 0) > ' + @Range3 + ' THEN ''3. > $' + @Range3 + '''
ELSE ''ERROR'' END AS 'Range',
SUM(clmamt) 'Total Reimbursement', COUNT(l.CLMID ) as 'Total Claims'
from
TableA l join
(
select CLMID, SUM(clm) 'clmamt' from TableAB
group by CLMID
)lc on l.CLMID = lc.CLMID join
TableB pt on l.ACCNTID = pt.ACCNTID join
TableC pn on pt.GROUPID = pn.GROUPID
where pt.GROUPID in (1,2)
Group by CASE WHEN ROUND(clmamt, 0) between 0 and 1000 THEN '1. $0- $1000'
WHEN ROUND(clmamt, 0) between 1001 and 5000 THEN '2. $1001- $5000'
WHEN ROUND(clmamt, 0) > 5001 THEN '3. > $5000'
ELSE 'ERROR' END
If I could do something where I can add a variable to denote number of bands and range of each band. And automatically query adds the number of when clauses in CASE statement, that will be great.