There is a table with data in the postgresql db 
What kind of request you need to write, so that only records with minimal (early) dates would be left? 
create table "test_over" ( "uid" int, "sum" int, "date" date );β
insert into "test_over" ("uid","sum","date") values (1, 50,'2018-01-01'), (1, 30,'2018-01-05'), (1, 60,'2018-01-20'), (2, 0,'2018-01-05'), (2, 30,'2018-01-03'), (3, 80,'2018-01-15'), (8,100,'2018-01-22'), (8, 50,'2018-01-23');8 rows affected
select distinct "uid", first_value ("sum") over (partition by "uid" order by "date" asc), first_value ("date") over (partition by "uid" order by "date" asc) from "test_over" order by "uid" ;uid | first_value | first_value -: | ----------: | : ---------- 1 | 50 | 2018-01-01 2 | 30 | 2018-01-03 3 | 80 | 2018-01-15 8 | 100 | 2018-01-22
db <> fiddle here
Source: https://ru.stackoverflow.com/questions/852725/
All Articles