diff --git a/migration.sql b/migration.sql index c1214f9..b7d1d6e 100644 --- a/migration.sql +++ b/migration.sql @@ -94,8 +94,8 @@ CREATE TABLE IF NOT EXISTS "logbook" ( "boat_id" INTEGER NOT NULL REFERENCES boat(id), "shipmaster" INTEGER NOT NULL REFERENCES user(id), -- null: club is owner "shipmaster_only_steering" boolean not null, - "departure" text not null, - "arrival" text, -- None -> ship is on water + "departure" datetime not null, + "arrival" datetime, -- None -> ship is on water "destination" text, "distance_in_km" integer, "comments" text, diff --git a/src/model/stat.rs b/src/model/stat.rs index 488b602..4f6ced9 100644 --- a/src/model/stat.rs +++ b/src/model/stat.rs @@ -11,17 +11,24 @@ impl Stat { pub async fn get_rowed_km(db: &SqlitePool) -> Vec { //TODO: switch to query! macro again (once upgraded to sqlite 3.42 on server) sqlx::query( - "SELECT u.name AS name, COALESCE(SUM(distance_in_km), 0) as rowed_km - FROM user u - INNER JOIN ( - SELECT shipmaster AS user_id, distance_in_km - FROM logbook - UNION - SELECT r.rower_id AS user_id, l.distance_in_km - FROM logbook l - INNER JOIN rower r ON r.logbook_id = l.id - ) AS subquery ON u.id = subquery.user_id - GROUP BY u.id ORDER BY rowed_km DESC;", + " +SELECT u.name, SUM(sub.distance_in_km) 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 +GROUP BY u.name +ORDER BY rowed_km DESC; +", ) .fetch_all(db) .await