fix stat query

This commit is contained in:
philipp 2023-08-05 15:58:17 +02:00
parent 7309b1f2b2
commit 64aefde8bc
2 changed files with 20 additions and 13 deletions

View File

@ -94,8 +94,8 @@ CREATE TABLE IF NOT EXISTS "logbook" (
"boat_id" INTEGER NOT NULL REFERENCES boat(id), "boat_id" INTEGER NOT NULL REFERENCES boat(id),
"shipmaster" INTEGER NOT NULL REFERENCES user(id), -- null: club is owner "shipmaster" INTEGER NOT NULL REFERENCES user(id), -- null: club is owner
"shipmaster_only_steering" boolean not null, "shipmaster_only_steering" boolean not null,
"departure" text not null, "departure" datetime not null,
"arrival" text, -- None -> ship is on water "arrival" datetime, -- None -> ship is on water
"destination" text, "destination" text,
"distance_in_km" integer, "distance_in_km" integer,
"comments" text, "comments" text,

View File

@ -11,17 +11,24 @@ impl Stat {
pub async fn get_rowed_km(db: &SqlitePool) -> Vec<Stat> { pub async fn get_rowed_km(db: &SqlitePool) -> Vec<Stat> {
//TODO: switch to query! macro again (once upgraded to sqlite 3.42 on server) //TODO: switch to query! macro again (once upgraded to sqlite 3.42 on server)
sqlx::query( sqlx::query(
"SELECT u.name AS name, COALESCE(SUM(distance_in_km), 0) as rowed_km "
FROM user u SELECT u.name, SUM(sub.distance_in_km) AS rowed_km
INNER JOIN ( FROM user u
SELECT shipmaster AS user_id, distance_in_km INNER JOIN (
FROM logbook SELECT r.rower_id AS user_id, l.distance_in_km
UNION FROM logbook l
SELECT r.rower_id AS user_id, l.distance_in_km INNER JOIN rower r ON l.id = r.logbook_id
FROM logbook l WHERE l.distance_in_km IS NOT NULL
INNER JOIN rower r ON r.logbook_id = l.id
) AS subquery ON u.id = subquery.user_id UNION ALL
GROUP BY u.id ORDER BY rowed_km DESC;",
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) .fetch_all(db)
.await .await