sql server - SQL/Report Builder - Report to return data based on certain multple month entries -


i need create report returns range of data, based on open date , records opened within month each previous quarter.

so if run report @ start of august - show records have open date within may, feb, november, august no limit on year. open date month defined month report run.

i have basic code

select recordmonth, recordyear, caseref, casedescription,         caseopendate, casecloseddate, personname, personsurname   dbo.casereview  month(caseopendate) = 03     or month(casecopendate) = 06      or month(caseopendate) = 09      or month(caseopendate) = 12 

issue is, need run dynamically, whichever month run, can show cases open date falls within previous quarters/months.

you can use modulo calculate 4 month numbers need based on current system date filter on those.

in case work.

declare @month int = month(getdate())  declare @m1 int = ((@month -1 ) % 12) +1 declare @m2 int = ((@month + 2) % 12) +1 declare @m3 int = ((@month + 5) % 12) +1 declare @m4 int = ((@month + 8) % 12) +1  select recordmonth, recordyear, caseref, casedescription,         caseopendate, casecloseddate, personname, personsurname   dbo.casereview  month(caseopendate) in (@m1, @m2, @m3, @m4) 

so first current month number, using getdate() current date , month function return month number. today equal 8.

next use modulo (the % sign) work out months need. example if month = 6 @m4 worked out as

(@month + 8) = 14

14 % 12 = 2 (remainder of 14 divided 12)

2 + 1 = 3

note: i've made bit longer needs it's bit easier read. put calcs @m1, @m2 etc directly select statement doing separately makes clearer. notice used in statement rather bunch of ors in where clause. in matches on of values listed in parentheses.


Comments

Popular posts from this blog

What is happening when Matlab is starting a "parallel pool"? -

angular - DownloadURL return null in below code -

php - Cannot override Laravel Spark authentication with own implementation -