There is a table with data in the postgresql db Table

What kind of request you need to write, so that only records with minimal (early) dates would be left? Conclusion

  • min (date) and first_value () over (partition by uid order by date asc) - Akina

1 answer 1

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