I need help with a SQL query in PHP ( WordPress ). I have this data:
| id | kw_id | date | position
| 1 | 1 | 2020.08.23 | 16 |
| 2 | 1 | 2020.10.02 | 17 |
| 3 | 2 | 2020.10.08 | 5 |
| 4 | 2 | 2020.10.08 | 6 |
and I need get two rows ( id=2, id=3 ). Query criteries is two:
- get row with max date
- group by kw_id
when date have duplicate ( id=3 and id=4 ) then get row with min position.
The result must be as follows:
id | kw_id | date | position
2 | 1 | 2020.10.02 | 17
3 | 2 | 2020.10.08 | 5
Thanks in advance.