Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Creating a list of the past 12 dynamic months

Author  Topic 

craigwg
Posting Yak Master

154 Posts

Posted - 2010-06-07 : 11:14:36
Hello.

I have a dataset that looks basically like this:


Date Absenteeism
1/1/2010 20
2/1/2010 30
3/1/2010 25
etc....


As months progress into the future this table is updated. I need to write a SP that displays the last 12 months of data. So if it's June it needs to display June 2009-June 2010. If it's July it needs to display July 2009-July 2010. I hope that makes sense.

I'm not sure how to do this. Someone suggested 12 selfjoins, but I don't know what that would look like. Help!

Craig

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2010-06-07 : 11:17:01

where
date_col>=dateadd(month,datediff(month,0,getdate())-12,0) and
date_col<dateadd(month,datediff(month,0,getdate())+1,0)


Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -