diff --git a/src/model/stat.rs b/src/model/stat.rs index d60816a..c0fc5e0 100644 --- a/src/model/stat.rs +++ b/src/model/stat.rs @@ -13,21 +13,11 @@ impl Stat { //TODO: switch to query! macro again (once upgraded to sqlite 3.42 on server) sqlx::query( " -SELECT u.name, CAST(SUM(sub.distance_in_km) AS INTEGER) AS rowed_km +SELECT u.name, CAST(SUM(l.distance_in_km) AS INTEGER) AS rowed_km FROM user u -INNER JOIN ( - SELECT r.rower_id AS user_id, l.distance_in_km - FROM logbook l - INNER JOIN rower r ON l.id = r.logbook_id - WHERE l.distance_in_km IS NOT NULL - - UNION ALL - - SELECT l.shipmaster AS user_id, l.distance_in_km - FROM logbook l - WHERE l.distance_in_km IS NOT NULL -) sub ON u.id = sub.user_id -WHERE u.is_guest = false +INNER JOIN rower r ON u.id = r.rower_id +INNER JOIN logbook l ON r.logbook_id = l.id +WHERE u.is_guest = 0 AND l.distance_in_km IS NOT NULL GROUP BY u.name ORDER BY rowed_km DESC; ",