Mon Mon - 2 months ago 11
SQL Question

Last Year’s YTD Number query

I need help getting the count of records for last year's YTD.

To get the records for this YTD numbers I have:

SELECT COUNT(columnname)
FROM ***
where "columnname" BETWEEN DATEADD(yy, DATEDIFF(yy,0,GETDATE()), 0) AND GETDATE()
AND user_code IN ('0410', '0411')) as Ag


This give the total records for 01/01/2016-06/02/2016 (today's date) which is right now I need to get the same total but for last's YTD it would be for 01/01/2015-06/02/2015 please help.

Answer

For this, in both places where you have GETDATE(), just replace with DATEADD(YEAR,-1,GETDATE())

WHERE [someColumn] BETWEEN DATEADD(yy, DATEDIFF(yy,0,DATEADD(YEAR,-1,GETDATE())), 0) AND DATEADD(YEAR,-1,GETDATE()) 
Comments