I have a table with some columns, amongst which there are two - number
, joining_date
.
What I want to select is the newest joining date and matching number. I created the following script:
SELECT ac.number, ac.joining_date
FROM accounts ac
INNER JOIN (
SELECT number, MAX(joining_date) as maxDate FROM accounts GROUP BY number
) iac ON ac.number = iac.number AND ac.joining_date = iac.maxDate;
It seems fine, however, I have noticed that when the joining_date
is equal i.e. 2020-04-02 10:17:00.000000
for more than one record, the number
appears twice in the result, even if the MAX
should return only one row.
Question: how to retrieve only one number
by the newest joining_date
? Is DISTINCT
guarantees that?