I have a table like this:
ID | OtherID | Date |
---|---|---|
1 | z | 2022-09-19 |
1 | b | 2021-04-05 |
2 | e | 2022-04-05 |
3 | t | 2022-07-08 |
3 | z | 2021-03-02 |
I want a table like this:
ID | OtherID | Date |
---|---|---|
1 | z | 2022-09-19 |
2 | e | 2022-04-05 |
3 | t | 2022-07-08 |
That have distinct pairs consisted of ID-OtherID based on the Date values which are the most recent.
The problem I have now is the relationship between ID and OtherID is 1:M
I’ve looked at SELECT DISTINCT
, GROUP BY
, LAG
but I couldn’t figure it out. I’m sorry if this is a duplicate question. I couldn’t find the right keywords to search for the answer.
Update: I use Postgres but would like to know other SQL as well.
2
Answers
This works for many dbms (versions of postgres, mysql and others) but you may need to adapt if something else. You could use a CTE, or a join, or a subquery such as this:
You can use a Common Table Expression (
CTE
) withROW_NUMBER()
to assign a row number based on theID
column (then return the first row for each ID in theWHERE
clausern = 1
):Result:
Fiddle here.