From 5c056fa1018862870937dce83b45b72d0041bff9 Mon Sep 17 00:00:00 2001 From: BroodjeAap Date: Mon, 9 Nov 2020 21:49:29 +0100 Subject: [PATCH] have working correct/incorrect/total/ratio values for every type from a 'single' query in getStats() --- lib/pages/practice/PracticeDatabase.dart | 72 +++++++++++++++++++----- 1 file changed, 59 insertions(+), 13 deletions(-) diff --git a/lib/pages/practice/PracticeDatabase.dart b/lib/pages/practice/PracticeDatabase.dart index a2d6d06..7cd4789 100644 --- a/lib/pages/practice/PracticeDatabase.dart +++ b/lib/pages/practice/PracticeDatabase.dart @@ -41,7 +41,7 @@ class PracticeDatabase { INNER JOIN PracticeAnswer on PracticeSession.id = PracticeAnswer.sessionId; '''); await database.execute(''' - CREATE VIEW AnswersCorrectByTypeView AS + CREATE VIEW AnswersCorrectByTypeViewAll AS SELECT type, SUM(correct = 1)*1.0 AS correct, @@ -53,15 +53,48 @@ class PracticeDatabase { type; '''); await database.execute(''' - CREATE VIEW AnswersRatioByTypeView AS + CREATE VIEW AnswersCorrectByTypeView7d AS SELECT type, - correct, - incorrect, - total, - correct / total AS ratio + SUM(correct = 1)*1.0 AS correct, + SUM(correct = 0)*1.0 AS incorrect, + COUNT(*) AS total, + CAST( + strftime('%s', time) + AS INTEGER + ) as time, + CAST( + strftime("%s", DATETIME('now', '-7 day')) + AS INTEGER + ) as before FROM - AnswersCorrectByTypeView + AnswersView + WHERE + time < before + GROUP BY + type; + '''); + await database.execute(''' + CREATE VIEW AnswersCorrectByTypeView30d AS + SELECT + type, + SUM(correct = 1)*1.0 AS correct, + SUM(correct = 0)*1.0 AS incorrect, + COUNT(*) AS total, + CAST( + strftime('%s', time) + AS INTEGER + ) as time, + CAST( + strftime("%s", DATETIME('now', '-1 month ')) + AS INTEGER + ) as before + FROM + AnswersView + WHERE + time < before + GROUP BY + type; '''); } @@ -95,18 +128,31 @@ class PracticeDatabase { static Future> getStats() async { var db = await getDatabase(); List> answers = await db.rawQuery(''' - SELECT + SELECT type, correct, incorrect, - total, - ratio + total FROM - AnswersRatioByTypeView + AnswersCorrectByTypeViewAll + UNION ALL + SELECT + type, + correct, + incorrect, + total + FROM + AnswersCorrectByTypeView7d + UNION ALL + SELECT + type, + correct, + incorrect, + total + FROM + AnswersCorrectByTypeView30d; '''); print(answers); - var first = answers.first; - print(first["practice_id"]); print("something else"); } } \ No newline at end of file