Wpis z mikrobloga

#bazydanych #sql #postgresql
Jako certyfikowany Janusz Postgresa™ mam problem ze zrobieniem skumulowanej sumy. Gdy wartości między wierszami się powtarzają, suma się nie aktualizuje - dopiero gdy wskoczy nowa wartość. Co robię nie tak? ¯\(ツ)_/¯

SELECT a.name,
a.nb_notes,
(a.nb_notes-b.nb_notes) AS diff,
sum(a.nb_notes) OVER (
ORDER BY a.nb_notes DESC) AS cumulative,
a.avg_age,
a.link
FROM pl_note_stats_after a
JOIN pl_note_stats_before b ON a.osm_id = b.osm_id
ORDER BY a.nb_notes DESC;

Tu przykład jak to wygląda

name | nb_notes | diff | cumulative | avg_age | link
-------------------------------+----------+------+------------+-----------+---------------------------------
Kraków | 347 | 1 | 347 | 283 days | [http://osm.org/relation/2768921](http://osm.org/relation/2768921)
Warszawa | 269 | -15 | 616 | 306 days | [http://osm.org/relation/2907540](http://osm.org/relation/2907540)
------- wycięte kilka wierszy -------
Jastarnia | 30 | 0 | 2157 | 439 days | [http://osm.org/relation/1555426](http://osm.org/relation/1555426)
Jelenia Góra | 29 | 0 | 2186 | 445 days | [http://osm.org/relation/3045883](http://osm.org/relation/3045883)
Świebodzice | 28 | 0 | 2298 | 788 days | [http://osm.org/relation/3027709](http://osm.org/relation/3027709)
gmina Pisz | 28 | 0 | 2298 | 309 days | [http://osm.org/relation/2992058](http://osm.org/relation/2992058)
gmina Wałcz | 28 | 0 | 2298 | 57 days | [http://osm.org/relation/2887596](http://osm.org/relation/2887596)
gmina Zabierzów | 28 | -2 | 2298 | 527 days | [http://osm.org/relation/1435909](http://osm.org/relation/1435909)
Włocławek | 27 | 0 | 2379 | 610 days | [http://osm.org/relation/2727653](http://osm.org/relation/2727653)
Częstochowa | 27 | 0 | 2379 | 354 days | [http://osm.org/relation/2831099](http://osm.org/relation/2831099)
Jaworzno | 27 | -1 | 2379 | 316 days | [http://osm.org/relation/2816596](http://osm.org/relation/2816596)
Tarnobrzeg | 26 | 0 | 2457 | 604 days | [http://osm.org/relation/3027887](http://osm.org/relation/3027887)
gmina Krościenko nad Dunajcem | 26 | 0 | 2457 | 752 days | [http://osm.org/relation/2834798](http://osm.org/relation/2834798)
gmina Władysławowo | 26 | 0 | 2457 | 447 days | [http://osm.org/relation/1555428](http://osm.org/relation/1555428)
gmina Kępno | 25 | 0 | 2557 | 378 days | [http://osm.org/relation/2713291](http://osm.org/relation/2713291)
Elbląg | 25 | 0 | 2557 | 537 days | [http://osm.org/relation/2723258](http://osm.org/relation/2723258)
gmina Olkusz | 25 | -1 | 2557 | 743 days | [http://osm.org/relation/2747693](http://osm.org/relation/2747693)
  • 2
@madry_i_mieciutki: Dzięki, też sam to znalazłem właśnie przed chwilą w dokumentacji :)
Wygląda na to, że cały ten mechanizm "peers" był robiony pod funkcje które podają ranking, a nieintuicyjne zachowanie funkcji agregujących to taki efekt uboczny ( ͡~ ͜ʖ ͡°)