I have a Presto table assume it has [id, name, update_time] columns and data
(1, Amy, 2018-08-01),
(1, Amy, 2018-08-02),
(1, Amyyyyyyy, 2018-08-03),
(2, Bob, 2018-08-01)
Now, I want to execute a sql and the result will be
(1, Amyyyyyyy, 2018-08-03),
(2, Bob, 2018-08-01)
Currently, my best way to deduplicate in Presto is below.
select
t1.id,
t1.name,
t1.update_time
from table_name t1
join (select id, max(update_time) as update_time from table_name group by id) t2
on t1.id = t2.id and t1.update_time = t2.update_time
More information, clike deduplication in sql
Is there a better way to deduplicate in Presto?
In PrestoDB, I would be inclined to use row_number()
:
select id, name, date
from (select t.*,
row_number() over (partition by name order by date desc) as seqnum
from table_name t
) t
where seqnum = 1;
You seems want subquery
:
select t.*
from table t
where update_time = (select MAX(t1.update_time) from table t1 where t1.id = t.id);
Here is another way
WITH latestDate AS (SELECT id,max(date) as latestDate FROM table_name GROUP BY id)
SELECT id,name,date FROM table_name t INNER JOIN latestDate l ON t.id = l.id AND t.date = l.latestDate
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With