Hey Everyone,
I am using the below query to try and group together values by the month of the date selected. the problem is that sometimes CTE2 will not have the same dates as CTE and those values end up omitted. Is there anyway I can just group these on a different parameter, or just on the month of the date?
Any help would be great.
;With CTE(total, InitDate) as
(
SELECT count(Inquiry.ID) as total, Inquiry.Date as InqDate from Inquiry
Inner Join Inquirer on Inquirer.ID = Inquiry.InquirerID_fk
Left Join Transfer on Transfer.TransferInquiryID_fk = Inquiry.ID
WHERE (Inquiry.Date >='6/1/2012' AND Inquiry.Date <= '9/6/2012' AND Inquiry.Date IS NOT NULL)
AND (Inquirer.Program = 'Res. Referral Coord.')AND TransferInquiryID_fk IS NULL
Group By Inquiry.Date
)
,CTE2(total, TransDate) as
(
SELECT count(Inquiry.ID) as total, Inquiry.Date as InqDate from Inquiry
Inner Join Inquirer on Inquirer.ID = Inquiry.InquirerID_fk
Left Join Transfer on Transfer.TransferInquiryID_fk = Inquiry.ID
WHERE (Inquiry.Date >='6/1/2012' AND Inquiry.Date <= '9/6/2012' AND Inquiry.Date IS NOT NULL)
AND (Inquirer.Program = 'Res. Referral Coord.') AND TransferInquiryID_fk IS NOT NULL
Group By Inquiry.Date
)
SELECT CASE
When Grouping(A.InitDate) = 1 THEN 'Date'
When Grouping(A.InitDate) = 1 THEN ''
ELSE A.InitDate
End As 'InitDate'
,Sum(A.total) As InitCount, Sum(ISNULL(B.total,0)) As TransCount from CTE A
left outer Join CTE2 B on A.InitDate = B.TransDate
group by A.InitDate with rollup
Thank you,
NickG