wouter
wouter

Reputation: 67

SQL Report - Group by Query for getting data from 12 months ago (this month included)

So, I found this nice little query:

Select    count(*) 
FROM      Table_X 
WHERE     year(DATE) = '2012' 
GROUP BY  month(DATE)

Which does almost exactly what I need.. Except I need to query the data of current month, up until 1 year ago. So for October, sept, aug, jul, jun, may, apr, mar, feb, jan 2012 and dec, nov 2011..

So I could just wait till the end of december to make the report :) . But there must be some other way?

Upvotes: 1

Views: 3717

Answers (1)

Gidil
Gidil

Reputation: 4137

Try using DATEADD to calculate the date 12 months ago:

Select    count(*) 
FROM      Table_X 
WHERE     DATE > = DATEADD(YEAR,-1,GETDATE())
GROUP BY  month(DATE)

If the problem is more complicated than this, post it and I'll add to my answer.

Upvotes: 2

Related Questions