sql server - SQL - Search table for last day of each month in date range and output data? -
i have 2 tables:
ticket_report ticket_report_snapshot
the ticket_report_snapshot table exact copy of ticket_report table, has 1 column:
snapshot_date
a snap shot of ticket report table taken everyday, snapshot_date being date snapshot taken.
the columns both tables have working are:
project_group, ticket_status
i need create stored procedure takes 2 date parameters. these 2 dates, need print count of open tickets each project on last day of each month in between 2 dates passed (the last day of each month searched in snapshot_date column of ticket_report_snapshot table).
this have far:
--this query gives me last day of 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 project_name, count(ticket_status) open_tickets ticket_report_snapshot ticket_status != 'closed' , ticket_status != 'cancelled' , snapshot_date = '2016-01-06' group project_group --right now, output perfect 1 date, hard coded in --output: project_name open_tickets project 1 77 project 2 5 project 3 118 project 4 22 --i need kind of output, last project 5 1 --day of each month between 2 parameters project 6 2 --instead of 1 date project 7 1
so have 2 queries far, 1 give me last day of particular month, , 1 show me open tickets 1 particular hard coded date.
how can edit/combine these queries use 2 date parameters, , give me open tickets each project last day of every month in between 2 date ranges?
ex. 1/1/2016 , 3/3/2016 (1/31, 2/29, 3/31, these 3 dates searched in ticket_report_snapshot table, in snapshot_date column)
you can use recursive cte dates , join snapshot table cte
declare @startdate datetime = '2016-01-01', @enddate datetime = '2016-03-03'; datecte ( select eomonth(@startdate) snapshot_date union select eomonth(dateadd(month,1,snapshot_date)) datecte eomonth(dateadd(month,1,snapshot_date)) <= eomonth(@enddate) ) select project_group project_name, trs.snapshot_date, count(ticket_status) open_tickets ticket_report_snapshot trs inner join datecte cte on trs.snapshot_date = cte.snapshot_date ticket_status != 'closed' , ticket_status != 'cancelled' group project_group, trs.snapshot_date
you can use if still using sql server 2008
declare @startdate datetime = '2016-01-01', @enddate datetime = '2016-03-03'; datecte ( select dateadd(dd,-1,dateadd(mm,datediff(m,0,@startdate) + 1,0)) snapshot_date union select dateadd(dd,-1,dateadd(mm,datediff(m,0,dateadd(month,1,snapshot_date)) + 1,0)) datecte dateadd(dd,-1,dateadd(mm,datediff(m,0,dateadd(month,1,snapshot_date)) + 1,0)) <= dateadd(dd,-1,dateadd(mm,datediff(m,0,@enddate) + 1,0)) ) select project_group project_name, trs.snapshot_date, count(ticket_status) open_tickets ticket_report_snapshot trs inner join datecte cte on trs.snapshot_date = cte.snapshot_date ticket_status != 'closed' , ticket_status != 'cancelled' group project_group, trs.snapshot_date
Comments
Post a Comment