diff --git a/db/manifest b/db/manifest index dc3f468de..8114e06ee 100755 --- a/db/manifest +++ b/db/manifest @@ -214,3 +214,4 @@ fix_find_session_sorting_2216a.sql fix_find_session_sorting_2216b.sql fix_find_session_sorting_2216c.sql entabulate_current_network_scores.sql +discard_scores_changed.sql \ No newline at end of file diff --git a/db/up/discard_scores_changed.sql b/db/up/discard_scores_changed.sql new file mode 100644 index 000000000..abc8a4d1c --- /dev/null +++ b/db/up/discard_scores_changed.sql @@ -0,0 +1,14 @@ +DROP FUNCTION IF EXISTS discard_scores(); +CREATE FUNCTION discard_scores () RETURNS VOID AS $$ +BEGIN +WITH scores_to_delete AS ( + SELECT alocidispid, blocidispid, scorer, created_at FROM (SELECT *, row_number() OVER (PARTITION BY alocidispid, blocidispid, scorer ORDER BY scores.created_at DESC) AS rownum FROM scores) tmp WHERE rownum > 6 +) + DELETE FROM scores USING scores_to_delete WHERE + scores.alocidispid = scores_to_delete.alocidispid AND + scores.blocidispid = scores_to_delete.blocidispid AND + scores.scorer = scores_to_delete.scorer AND + scores.created_at = scores_to_delete.created_at; + RETURN; +END; +$$ LANGUAGE plpgsql; \ No newline at end of file