I have a table with the following columns.
- ID (auto-inc)
- When (datetime)
- id1 (number)
- id2 (number)
The combination of id1 and id2 can be unique or duplicated many times.
I need a query that returns the earliest record (by When) for each unique combination of id1+id2.
Example data:
ID | When | id1 | id2 |
---|---|---|---|
1 | 1-Jan-2020 | 4 | 5 |
2 | 1-Jan-2019 | 4 | 5 |
3 | 1-Jan-2021 | 4 | 5 |
4 | 1-Jan-2020 | 4 | 4 |
5 | 1-Jan-2019 | 4 | 4 |
6 | 1-Jan-2021 | 4 | 6 |
I need this to return rows 2, 5 and 6
I cannot figure out how to do this with an SQL query.
I have tried Group By on the concatenation of id1 & id2, and I have tried "Distinct id1, id2", but neither return the entire row of the record with the earliest When value.
If the result set can just return the ID that is fine also, I just need to know the rows that match these two requirements.