简体   繁体   中英

Run latest date records

I'm trying to run latest/recent year records from a set of data in a table. Ex:

10903          09C2D381-59B2-E411-BD0F-1CC1DE2561D4       2014-12-31
10903          D15F5801-5AB2-E411-BD0F-1CC1DE2561D4       2014-12-31
10903          4861F3DB-7DC9-E511-80E2-8CDCD4AF21E4       2014-12-31
10903          C1D6898E-D6ED-E611-80EA-8CDCD4AF21E4       2016-12-31
10903          00D7898E-D6ED-E611-80EA-8CDCD4AF21E4       2016-12-31
10903          A1B214AC-150C-E811-80F1-8CDCD4AF21E4       2017-12-31
10903          08E30EB2-150C-E811-80F1-8CDCD4AF21E4       2017-12-31
10903          889BEB0D-DD29-E911-8105-8CDCD4AF21E4       2018-12-31
10903          8F9BEB0D-DD29-E911-8105-8CDCD4AF21E4       2018-12-31

from the above data, for the identifier 10903, we have 2 records for recent year 2018. I want to run a query to get all records from recent year.

I've 300 identifiers to run. Could you help me with a query?

You can use sub-query :

select t.*
from table t
where year(t.date) = (select max(year(t1.date)) from table t1 where t1.identifier = t.identifier);

You can also ranking function rank() if your DBMS support :

select t.*
from (select t.*, rank() over (partition by t.identifier order by year(t.date) desc) as seq
      from table t
     ) t
where seq = 1;

Function year() is product specific.

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