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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SQL to provide month/year vs. prev. month/year compares?

Status
Not open for further replies.

pk400i

MIS
Feb 10, 2009
102
US
HI I have to write a Report that will show groups such as Food products sold, e.g. Meat, Fish, others, etc.
comparing this year month with last year/month going back 4 months as well.

I was not sure if this is something you group on or how to set up such buckets here. My exisitng SQL is posted below. I wanted to setup my group totals there going from the previous month, i.e. if it ran today, we would start with April 2009 and April 2008, totaling the quotes field and then the same for 3 months prior on both years.

Thank you for any pointers and assistance.
 
an example may be

Code:
declare @v_Startdate datetime
select @v_StartDate = dateadd(mm,-3,getdate())
select [productid], [quantity], [value], convert(varchar,month(mydateField)) + convert(varchar,month(mydateField)) as yearmonth
from myTable
where
convert(varchar,month(mydateField)) + convert(varchar,month(mydateField)) >
convert(varchar,month(@v_StartDate )) + convert(varchar,month(@v_StartDate )) 

group by [productid],convert(varchar,month(mydateField)) + convert(varchar,month(mydateField))

not tested, and not even sure if it works

"I'm living so far beyond my income that we may almost be said to be living apart
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top