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
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Aggregate previous 3 months of data

Author  Topic 

Brittney10
Posting Yak Master

154 Posts

Posted - 2011-07-21 : 16:09:29
I want to aggregate the previous 3 months of data that I have. Is there a way to do this using something like getdate() -1, getdate() -2, getdate() -3. Thanks!

robvolk
Most Valuable Yak

15732 Posts

Posted - 2011-07-21 : 16:17:56
SELECT SUM(col1) FROM myTable WHERE dateCol>=DATEADD(month, -3, GETDATE())

That is sensitive to day of month and time values. If you want the entire calendar month to be included:

SELECT SUM(col1) FROM myTable WHERE dateCol>=DATEADD(month, DATEDIFF(month, 0, GETDATE())-3, 0)
Go to Top of Page
   

- Advertisement -