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),
"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,

View File

@ -11,17 +11,24 @@ impl 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)
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