'Two layered SQL Filter on single column

I am trying to find a better alternative to the common (probably why there is a typical answer) approach of checking IN (subquery ordered by column A) when looking for a latest version. Sample as follows.

SELECT v.id
FROM   tblitemversion v,
       tblitem i
WHERE  v.id = i.id
AND    v.versionid IN
       ( --latest revision
                SELECT   v2.versionid
                FROM     tblversionextended ve,
                         tblitemversion v2
                WHERE    ve.id = v2.id
                AND      v2.secondaryid = ir. secondaryid
                ORDER BY ve.datecreated DESC,
                         ve.lastupdated DESC,
                         v2.versionid DESC offset 0 ROWS
                FETCH NEXT 1 ROWS ONLY)
ID VersionID
A1 00
A1 01
A1 01V1
A1 01Z1
B1 00
C1 00
C1 01
C1 02
C1 03

What I would like to do is write it so that I can have two filters, for IDs like B1 and C1 i only go by max value since its numeric. For A1 I know that 01V1 and 01Z1 are only going to be able to be determined based on the created date from the joined table, i have to include that in the filter. I was curious if there is a conditional or better approach than the existing query in order to increase performance (hitting against millions of records).



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source