I have 2 tables:
Ticket_Report
Ticket_Report_Snapshot
The Ticket_Report_Snapshot table is an exact copy of the Ticket_Report table, but has 1 extra column:
Snapshot_Date
A snap shot of the Ticket report table is taken everyday, with the Snapshot_date being the date that the snapshot was taken.
The columns that both Tables have that I am working with are:
Project_group, Ticket_Status
I need to create a stored procedure that takes 2 Date parameters. From these 2 dates, I need to print the count of all open tickets for each project on the last day of each month in between the 2 dates passed (the last day of each month is to be searched for in the Snapshot_Date column of the Ticket_report_snapshot table).
This is what I have so far:
--This query gives me the last day of any particular month
DECLARE @dtDate DATETIME
SET @dtDate = '1/6/2016'
SELECT DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,@dtDate)+1,0))
-- ouput: 2016-01-31 23:59:59.000
SELECT Project_Group as Project_Name, count(ticket_status) as Open_Tickets
FROM Ticket_Report_SnapShot
WHERE ticket_status != 'closed' AND ticket_status != 'cancelled'
AND snapshot_date = '2016-01-06'
GROUP BY Project_Group
--Right now, the output is perfect for this 1 date, hard coded in
--OutPut:
Project_Name Open_Tickets
Project 1 77
Project 2 5
Project 3 118
Project 4 22 --I need this kind of output, but for the last
Project 5 1 --day of each month between the 2 parameters
Project 6 2 --instead of just 1 date
Project 7 1
So I have 2 queries so far, 1 to give me the last day of any particular month, and 1 to show me the open tickets for 1 particular hard coded date.
How can I edit/combine these queries to use 2 date parameters, and give me the open tickets for each project for the last day of every month in between 2 date ranges?
Ex. 1/1/2016 and 3/3/2016 (1/31, 2/29, 3/31, these 3 dates would be searched for in the ticket_report_snapshot table, in the snapshot_date column)