Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to deduplicate in Presto

Tags:

sql

presto

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?

like image 779
Archon Avatar asked Aug 01 '18 09:08

Archon


3 Answers

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;
like image 63
Gordon Linoff Avatar answered Sep 28 '22 16:09

Gordon Linoff


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);
like image 21
Yogesh Sharma Avatar answered Sep 28 '22 15:09

Yogesh Sharma


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
like image 31
Dwamian Mcleish Avatar answered Sep 28 '22 14:09

Dwamian Mcleish