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]
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.
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.