diff --git a/lib/pages/practice/PracticeDatabase.dart b/lib/pages/practice/PracticeDatabase.dart index 164912d..a2d6d06 100644 --- a/lib/pages/practice/PracticeDatabase.dart +++ b/lib/pages/practice/PracticeDatabase.dart @@ -40,6 +40,29 @@ class PracticeDatabase { FROM PracticeSession INNER JOIN PracticeAnswer on PracticeSession.id = PracticeAnswer.sessionId; '''); + await database.execute(''' + CREATE VIEW AnswersCorrectByTypeView AS + SELECT + type, + SUM(correct = 1)*1.0 AS correct, + SUM(correct = 0)*1.0 AS incorrect, + COUNT(*) AS total + FROM + AnswersView + GROUP BY + type; + '''); + await database.execute(''' + CREATE VIEW AnswersRatioByTypeView AS + SELECT + type, + correct, + incorrect, + total, + correct / total AS ratio + FROM + AnswersCorrectByTypeView + '''); } static Future getDatabase() async { @@ -74,16 +97,12 @@ class PracticeDatabase { List> answers = await db.rawQuery(''' SELECT type, - SUM(correct = 1) AS correct, - SUM(correct = 0) AS incorrect, - ( - (SUM(correct = 1)*1.0) / - (SUM(correct = 0) + SUM(correct = 1)) - ) as ratio + correct, + incorrect, + total, + ratio FROM - AnswersView - GROUP BY - type; + AnswersRatioByTypeView '''); print(answers); var first = answers.first;