I am trying to get the latest date among the column values mentioned but also fetching relative column values for the respective Max(date)
My Query:
select gd.desk_id, gd.created_by, max(gd.created_at) from dodem gd where gd.desk_id in ('abc123', 'xyz123')
and gd.pot_id='123S2' group by gd.desk_id;
Now I have the following data column - desk_id, created_by, created_at, pot_id
abc123, sam, 2020-10-12 14:53:27, 123S2
xyz123, max, 2020-10-12 14:53:27, 123S2
xyz123, max, 2020-10-12 15:53:27, 123S2
With above query I should be getting two rows
abc123, sam, 2020-10-12 14:53:27, 123S2
xyz123, max, 2020-10-12 15:53:27, 123S2
which is absolutely working as it should be but the query is getting wrong results when I have similar data but different created_by then it does not fetches created_by of max(created_at) rather which comes first.
Example:
abc123, sam, 2020-10-12 14:53:27, 123S2
xyz123, max, 2020-10-12 14:53:27, 123S2
xyz123, jim, 2020-10-12 15:53:27, 123S2
Query Gives:
abc123, sam, 2020-10-12 14:53:27, 123S2
xyz123, max, 2020-10-12 15:53:27, 123S2
If you see I am getting max rather I should be getting jim for latest date corresponding column value.
Please suggest improvement in my query.