I have a query which returns the sum of records in a month for a given practice. I am limiting the range to look at today’s date - 2 years and <= today’s date. I want to show a zero for the records where a service_date doesn’t exist. How can I do this? I have tried left, right, full outer joins but none seem to work.
Query:
SELECT EH.TIN, Dates.Daily_date, DATEADD(m, DATEDIFF(m, 0, Dates.Daily_date), 0),ISNULL(count(ed.detail_key),0) as 'Claim_Detail'
FROM [dbo].[ENCOUNTER_DETAIL] ED
FULL OUTER JOIN
(
SELECT TOP(73049) DATEADD(d, -1, ROW_NUMBER() OVER (ORDER BY o.object_id)) AS Daily_Date
FROM master.sys.objects o, master.sys.objects o1, master.sys.objects o2
) Dates On Dates.Daily_Date = ED.Service_Date
FULL OUTER JOIN [dbo].[ENCOUNTER_HEADER] EH with (nolock) ON ED.encounter_key = EH.encounter_key
WHERE ed.data_source_id = '1234'
AND Dates.Daily_date > DATEADD(month, -24, GETDATE())
AND Dates.Daily_date <= getdate()
group by EH.TIN, Dates.Daily_date, DATEADD(m, DATEDIFF(m, 0, Dates.Daily_date), 0)
For example: My output is
Practice_Name Year_Month Claim_Detail
Prac_1 10/1/2017 75
Prac_1 12/1/2017 8
But I would like my output to be:
Practice_Name Month_Year Claim_Detail
Prac_1 5/1/2016 0
Prac_1 6/1/2016 0
Prac_1 7/1/2016 0
Prac_1 8/1/2016 0
Prac_1 9/1/2016 0
Prac_1 10/1/2016 0
Prac_1 11/1/2016 0
Prac_1 12/1/2016 0
Prac_1 1/1/2017 0
Prac_1 2/1/2017 0
Prac_1 3/1/2017 0
Prac_1 4/1/2017 0
Prac_1 5/1/2017 0
Prac_1 6/1/2017 0
Prac_1 7/1/2017 0
Prac_1 8/1/2017 0
Prac_1 9/1/2017 0
Prac_1 10/1/2017 75
Prac_1 12/1/2017 8
Prac_1 1/1/2018 0
Prac_1 2/1/2018 0
Prac_1 3/1/2018 0
Prac_1 4/1/2018 0
Context: The reason I need to update my query is because this data is being pulled by an SSRS report and I want to show the peaks/valleys of this practice where they have not sent records using a Sparkline.