Suppose I have a single table actions like so:
+----+------------+--------+-------+
| id | rdate | clicks | epoch |
+----+------------+--------+-------+
| 1 | 2020-01-01 | 100 | 1200 |
| 1 | 2020-01-01 | 95 | 1100 |
| 1 | 2020-10-12 | 42 | 1000 |
| 1 | 2020-10-12 | 66 | 900 |
+----+------------+--------+-------+
I am trying to write a query that gives me the number of clicks based on the MAX(epoch) for records with identical values in the id and rdate columns - the end result should be: (note, I do not need the epoch column in the result)
+----+------------+--------+
| id | date | clicks |
+----+------------+--------+
| 1 | 2020-01-01 | 100 |
| 1 | 2020-10-12 | 42 |
+----+------------+--------+
I have tried the following query but the duplicates are still present in the result. The group by query does remove duplicates when run by itself, but the inner join to get the clicks does not work as intended.
SELECT
id,
rdate,
clicks
FROM actions a
INNER JOIN (
SELECT
id,
rdate,
MAX(epoch)
from actions
group by
id,
rdate
) b
on a.id = b.id
and a.rdate = b.rdate;