Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Monthly history query 1

Status
Not open for further replies.

baugie

Programmer
Apr 9, 2003
56
US
I am trying to create a query to group on the data by month for the previous year. I have the query grouping by month, but can't get it to stop at the data prior to one year earlier. Here is an example of my query:

SELECT (Format([shDate],"mmm yyyy")) AS [Month], (equation as [Results])
FROM [TableName]
GROUP BY (Format([shDate],"mmm yyyy"))
HAVING ((((Format([shDate],"mmm yyyy")))>format(DateAdd("y",-1,Now()),"mmm yyy")))

This retrieves every month in the database. Suggestions?

Thanks

baugie
 
Haven't tested this but you only have 3 y's in the final Format statement. Try this:

SELECT (Format([shDate],"mmm yyyy")) AS [Month], (equation as [Results])
FROM [TableName]
GROUP BY (Format([shDate],"mmm yyyy"))
HAVING ((((Format([shDate],"mmm yyyy")))>format(DateAdd("y",-1,Now()),"mmm yyyy")))


Bob Scriver
 
Sorry, you have to turn around the year and month for the comparison:
SELECT (Format([shDate],"mmm yyyy")) AS [Month], (equation as [Results])
FROM [TableName]
GROUP BY (Format([shDate],"mmm yyyy"))
HAVING ((((Format([shDate],"yyyy mmm")))>format(DateAdd("y",-1,Now()),"yyyy mmm")))

Let me know how this works.
Bob Scriver
 
OOPS...I had caught that earlier also. This only takes out all months equal to the current month. However, all other months are still included.

The key is stopping it from going back beyond one year. For some reason it is not using the DateAdd function to correctly identify the date.

Thanks though

Brian
 
I was a bit late with my reply. Using the reversed "yyyy mmm" formating, it only selects months from the current year because it is comparing all years greater than last year. Perhaps I need to use a conditional based upon the year of the date in the table. I'll give it a go here.

Thanks,

Brian
 
Sorry, about the previous posts. I think this should work for you. We have to use the actual date rather than the formated representation of the date in your Having statement.
SELECT (Format([shDate],"mmm yyyy")) AS [Month], (equation as [Results])
FROM [TableName]
GROUP BY (Format([shDate],"mmm yyyy"))
HAVING [shDate] >= DateSerial(DatePart("yyyy", date) - 1, DatePart("m", date), 1) Bob Scriver
 
Looks like you got it for me Bob!

Thanks!!!

Brian
 
Well if you try and try and try. Every once in a while you get it right in the end. That is what testing is for. Bob Scriver
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top