Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Empty Records

Author  Topic 

jcb267
Constraint Violating Yak Guru

291 Posts

Posted - 2015-04-09 : 10:53:56
Im using this code to pull claims by month of service, this is just a partial examle:

select case
when month(cl.servicedatefrom) < 10 then convert(char(4), year(cl.servicedatefrom)) + '-0' + convert(char(1), month(cl.servicedatefrom))
else convert(char(4), year(cl.servicedatefrom)) + '-' + convert(char(2), month(cl.servicedatefrom))
end as IncurredDt
, sum(cl.NHPLiability) as Mental_Health

from dw..MHClaimLine cl
inner join dw..MHClaimHeader ch
on ch.ClaimID = cl.ClaimID

where cl.ServiceDateFrom between '01/01/2014' and '12/31/2014'
and ch.PaidDate between '01/01/2014' and '03/31/2014'
and ch.PolicyNumber = '040MG'
and ch.ClaimStatus in ('PAID','PAY')
and ch.ResubmittedClaimId = ''

group by case
when month(cl.servicedatefrom) < 10 then convert(char(4), year(cl.servicedatefrom)) + '-0' + convert(char(1), month(cl.servicedatefrom))
else convert(char(4), year(cl.servicedatefrom)) + '-' + convert(char(2), month(cl.servicedatefrom))
end

order by case
when month(cl.servicedatefrom) < 10 then convert(char(4), year(cl.servicedatefrom)) + '-0' + convert(char(1), month(cl.servicedatefrom))
else convert(char(4), year(cl.servicedatefrom)) + '-' + convert(char(2), month(cl.servicedatefrom))
end

The results like this:

IncurredDt Mental_Health
2014-01 93.42
2014-02 79.54

It is typically run for 12 months, sometimes 24. If there is no data in any one month I want to see it with a 0.....like this:

IncurredDt Mental_Health
2014-01 $93.42
2014-02 $80.54
2014-03 $0.00
2014-04 $0.00
2014-05 $0.00
2014-06 $0.00
2014-07 $0.00
2014-08 $0.00
2014-09 $0.00
2014-10 $0.00
2014-11 $0.00
2014-12 $0.00

Is it possible to adjust the code above to do this?

Thanks

John

Ifor
Aged Yak Warrior

700 Posts

Posted - 2015-04-09 : 11:55:45
Outer join to a calendar table.
(Search calendar table)

This puts a simple one inline:

DECLARE @basedate datetime;
SET @basedate = '20140101';

WITH N1(N) AS (SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1)
,N2(N) AS (SELECT N1.N FROM N1, N1 AS N2)
,N(N) AS (SELECT ROW_NUMBER() OVER (ORDER BY N) - 1 AS N FROM N2)
,Calendar
AS
(
SELECT CONVERT(char(7), DATEADD(month, N, @basedate), 120) AS MonthStr
,DATEADD(month, N, @basedate) AS MonthStart
,DATEADD(month, N + 1, @basedate) AS MonthEnd
FROM N
WHERE N < 12
)
--select * from Calendar
SELECT C.MonthStr AS IncurredDt
,COALESCE(SUM(CL.NHPLiability), 0) AS Mental_Health
FROM dw.dbo.MHClaimLine CL
JOIN dw.dbo.MHClaimHeader CH
ON CL.ClaimID = CH.ClaimID
AND CH.PaidDate BETWEEN '20140101' AND '20140331'
AND CH.PolicyNumber = '040MG'
AND CH.ClaimStatus IN ('PAID','PAY')
AND CH.ResubmittedClaimId = ''
RIGHT JOIN Calendar C
ON CL.servicedatefrom >= C.MonthStart
AND CL.servicedatefrom < C.MonthEnd
GROUP BY C.MonthStr
ORDER BY IncurredDt;
Go to Top of Page

jcb267
Constraint Violating Yak Guru

291 Posts

Posted - 2015-04-09 : 12:47:29
Thank you! So I would just select the date from the calendar table but keep the date filtering criteria in the where clause?
Go to Top of Page
   

- Advertisement -