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!

Sum of filed from Todays date to year ago date 1

Status
Not open for further replies.

Cpreston

MIS
Mar 4, 2015
972
GB
Hi

I want to be able to select and sum figures for ProducID in a table from todays date back to same date last year. Is this possible and if so how can I do this please. Thanks in advance.

SELECT SUM(TotalSales) AS YTD, Productid
From SalesAnalysis3


 
Something like:
[tt]
SELECT SUM(TotalSales) AS YTD, Productid
From SalesAnalysis3
WHERE SomeDateField BETWEEN (DATEADD(year, -1, GETDATE()))
AND GETDATE()
GROUP BY Productid[/tt]


---- Andy

There is a great need for a sarcasm font.
 
That's great thanks.

If I wanted to go to the last month before this month last year, is this possible.

So from todays date back to last day of February 28/02/2018

MM - 11 perhaps
 
Explore SQL Server DATEADD() Function to do all the calculations, but if you want "last day of February 28/02/2018", you may want to get the first day of March, and subtract 1 day.


---- Andy

There is a great need for a sarcasm font.
 
Thanks or the reply, all working as it should

Thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top