diff --git a/db/up/scores_report.sql b/db/up/scores_report.sql new file mode 100644 index 000000000..9d796cdb3 --- /dev/null +++ b/db/up/scores_report.sql @@ -0,0 +1,22 @@ +-- TODO median is implemented as average until a suitable median function can be found. + +create or replace function scores_report(begin_at timestamp, end_at timestamp) + returns table (from_city text, from_region text, from_country text, from_isp text, to_city text, to_region text, to_country text, to_isp text, min_score integer, max_score integer, mean_score numeric, median_score numeric) +as +$body$ + select a.city as from_city, e.regionname as from_region, c.countryname as from_country, x.company as from_isp, b.city as to_city, f.regionname as to_region, d.countryname as to_country, y.company as to_isp, min(s.score) as min_score, max(s.score) as max_score, avg(s.score) as mean_score, avg(s.score) as median_score + from scores s + join geoiplocations a on a.locid = (s.alocidispid / 1000000) + join geoiplocations b on b.locid = (s.blocidispid / 1000000) + join countries c on c.countrycode = a.countrycode + join countries d on d.countrycode = b.countrycode + join regions e on e.countrycode = a.countrycode and e.region = a.region + join regions f on f.countrycode = b.countrycode and f.region = b.region + join jamcompany x on x.coid = (s.alocidispid % 1000000) + join jamcompany y on y.coid = (s.blocidispid % 1000000) + where s.scorer = 0 and s.score_dt between begin_at and end_at + group by a.city, e.regionname, c.countryname, x.company, b.city, f.regionname, d.countryname, y.company; +$body$ +language sql; + +-- select * from scores_report(current_timestamp::timestamp, current_timestamp::timestamp);