Quantcast
Channel: SQL Query to get latest records for that user - Stack Overflow
Viewing all articles
Browse latest Browse all 4

Answer by GMB for SQL Query to get latest records for that user

$
0
0

You can filter with a subquery:

select t.*from mytable twhere     t.id = 101    t.completiondate = (        select max(t1.completiondate)         from mytable t1         where t1.id = t.id and t1.id = t.id and t1.column1 = t.column1    )

Alternatively, in MySQL 8.0, you can use window function rank():

select *from (     select t.*, rank() over(partition by id, column1 order by completiondate desc) rn     from mytable t     where id = 101) twhere rn = 1

Note that, for this dataset, you could also use simple aggregation:

select id, column1, max(completiondate) completiondatefrom mytablewhere id = 101group by id, column1

Viewing all articles
Browse latest Browse all 4

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>