I'm trying to use the MAX()
function as a subquery to filter results of my greater query by the last couple of whole years. I realized this was a little bit more complicated than what I initially thought it would need so I created a much simpler query to test out what I would need to do with the HAVING
clause in order to get it to work.
I've checked these two posts but it didn't really help (SQL, HAVING clause explained, SQL: HAVING clause)
I don't think my understanding of the HAVING
clause is correct because I'm not sure why it's not working. Would someone be able to help and explain?
Note: [Fiscal Year]
is NVARCHAR
so I figured converting it to INT
might work.
SELECT DISTINCT
D.[FISCAL YEAR]
FROM [Dates] AS D
GROUP BY D.[Fiscal Year]
HAVING CONVERT(INT,D.[Fiscal Year]) >= MAX(CONVERT(INT,D.[FISCAL YEAR])) -2
These are my results:
(No column name)
2015
2014
2013
2012
2016
These are the results I should get:
(No column name)
2015
2014
2016