diff --git a/migrations/0002_fixTrendsFromCacheAndCurrent.sql b/migrations/0002_fixTrendsFromCacheAndCurrent.sql new file mode 100644 index 0000000..1e49218 --- /dev/null +++ b/migrations/0002_fixTrendsFromCacheAndCurrent.sql @@ -0,0 +1,33 @@ +CREATE OR REPLACE VIEW mm_rating_trends (mob, rating, "date", id) AS +WITH complete_history (ratings, last_update) AS ( + SELECT * FROM mm_history_cache + UNION SELECT * FROM mm_rating_history +) +SELECT + cast(key AS numeric) AS mob, + cast(value AS numeric) rating, + "date", + id +FROM ( + SELECT + id, + ratings, + "date" + FROM ( + SELECT + max(id) AS id, + "date" + FROM ( + SELECT + last_update AS id, + date(matches.created) AS "date" + FROM complete_history AS history + INNER JOIN mm_matches AS matches + ON history.last_update = matches.id + ) AS dates + GROUP BY "date" + ) AS key_dates + INNER JOIN complete_history AS history + ON key_dates.id = history.last_update +) AS ratings_at_key_date, +jsonb_each(ratings_at_key_date.ratings) AS ratings(key, value); \ No newline at end of file