Access SQL query: find the row with the most recent date for each distinct entry in a table

后端 未结 5 1850
盖世英雄少女心
盖世英雄少女心 2021-02-06 08:50

All,

I\'m sure this is a pretty simple SQL query question, but I\'m sure there\'s a good way, and a very BAD way, to do this. Left to my own devices, I\'m liable to end

5条回答
  •  孤独总比滥情好
    2021-02-06 09:16

    Not sure what platform you're looking to do this on, but in T-SQL you can do the following:

    SELECT t.*
    FROM (
        SELECT ID, MAX(As_Of) as r1
        FROM myTable
        GROUP BY ID
        ) as dt
    INNER JOIN myTable t ON dt.ID = t.ID and dt.r1 = t.As_Of
    

    Good luck!

    EDIT: Well my poor answer was bothering me so i've fixed it, even though this answer already exists elsewhere on the page now.

提交回复
热议问题