sql server 2012 - Recursive SQL Query for 6 individual months of following query -
i'm wondering if there way perform following query recursively so 6 times recent 6 months.
select datename(month,getdate()) 'month', sum(case when overallriskrating = 1 1 end) 'low', sum(case when overallriskrating = 2 1 end) 'med', sum(case when overallriskrating = 3 1 end) 'high' dbo.changeevaluationform month(datesubmitted) = month(getdate())
the results query follows
i'd return 5 more rows data each of months prior current month. that's possible? i'd avoid performing 5 more individual queries if can.
thank in advance.
you can group by
, using dateadd()
move 6 months current month:
select datename(month, datesubmitted) 'month', sum(case when overallriskrating = 1 1 end) 'low', sum(case when overallriskrating = 2 1 end) 'med', sum(case when overallriskrating = 3 1 end) 'high' dbo.changeevaluationform datesubmitted >= dateadd(month, datediff(month, 0, dateadd(month, -6, getdate())), 0) group datename(month, datesubmitted)
wiki
Comments
Post a Comment