Skip to content

Instantly share code, notes, and snippets.

@NikolayS
Last active June 8, 2017 22:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NikolayS/549de706a641d1d045f6dc97ca8345d0 to your computer and use it in GitHub Desktop.
Save NikolayS/549de706a641d1d045f6dc97ca8345d0 to your computer and use it in GitHub Desktop.
Delete duplicates with using most recent "last created" timestamps for remaining records
drop table if exists moscow_weather;
create table moscow_weather (
id bigserial not null primary key,
year int2 not null,
month int2 not null,
created timestamptz not null default clock_timestamp(),
weather_is_fine boolean
);
insert into moscow_weather (year, month, weather_is_fine)
select gen_y, gen_m, true
from
(select generate_series as gen_y from generate_series(2000, 2017)) as y,
(select generate_series as gen_m from generate_series(1, 12)) as m
where gen_y < 2017 or gen_m <= 6
;
insert into moscow_weather (year, month, weather_is_fine)
values (2017, 5, false);
insert into moscow_weather (year, month, weather_is_fine)
values (2017, 5, false);
insert into moscow_weather (year, month, weather_is_fine)
values (2017, 6, false);
select * from moscow_weather order by year desc, month desc;
with candidates as (
select year,
month,
max(id) as keep_id,
array_agg(id) as all_ids,
array_remove( -- Postgres 9.3+
array_agg(id), max(id)
) as discard_ids,
min(created) as first_created
from moscow_weather
group by 1, 2
having count(*) > 1
), patch_rows as (
update moscow_weather
set created = c.first_created
from candidates as c
where moscow_weather.id = c.keep_id
returning *
), delete_rows as (
delete from moscow_weather
where id in (select unnest(discard_ids) from candidates)
returning *
)
select
(select count(*) from patch_rows) as "patched / left",
(select count(*) from delete_rows) as deleted
;
create unique index u_moscow_weather_year_month on moscow_weather using btree(year, month);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment