简体   繁体   中英

How to get Fiscal Year to only show the last 3 years

This question is probably a duplicate but I couldn't find it after 10 mins of research. Here is the question:

I am trying to get the last 3 Fiscal years to show up in my query: so for me it would be 2018, 2019, and 2020.

        SELECT distinct E.FISCALYEAR
        FROM EMPLOYEES as E 
            INNER JOIN HR_PERIODS as H ON E.PERIOD = H.PERIOD 
        WHERE 
        E.FISCALYEAR <= year(getdate())  + 1
        and E.RECORDSTATUS = 1

        ORDER BY E.FISCALYEAR

The query I currently have does:

2015
2016
2017
2018
2019
2020

Also our HR Fiscal Year populates from 2015 to Present fiscal year.

You can use top (3) :

SELECT distinct TOP (3) E.FISCALYEAR
FROM RPT_EMPLOYEECENSUS_ASOF E INNER JOIN
     HR_PERIODS H
     ON E.PERIODNUMBER = H.PERIODNUMBER 
WHERE E.FISCALYEAR <= year(getdate())  + 1 AND
      E.EOM_RECORDSTATUS_EFF = 1
ORDER BY E.FISCALYEAR DESC

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM