have working correct/incorrect/total/ratio values for every type from a 'single' query in getStats()

This commit is contained in:
BroodjeAap 2020-11-09 21:49:29 +01:00
parent f9ecd40a10
commit 5c056fa101

View file

@ -41,7 +41,7 @@ class PracticeDatabase {
INNER JOIN PracticeAnswer on PracticeSession.id = PracticeAnswer.sessionId; INNER JOIN PracticeAnswer on PracticeSession.id = PracticeAnswer.sessionId;
'''); ''');
await database.execute(''' await database.execute('''
CREATE VIEW AnswersCorrectByTypeView AS CREATE VIEW AnswersCorrectByTypeViewAll AS
SELECT SELECT
type, type,
SUM(correct = 1)*1.0 AS correct, SUM(correct = 1)*1.0 AS correct,
@ -53,15 +53,48 @@ class PracticeDatabase {
type; type;
'''); ''');
await database.execute(''' await database.execute('''
CREATE VIEW AnswersRatioByTypeView AS CREATE VIEW AnswersCorrectByTypeView7d AS
SELECT SELECT
type, type,
correct, SUM(correct = 1)*1.0 AS correct,
incorrect, SUM(correct = 0)*1.0 AS incorrect,
total, COUNT(*) AS total,
correct / total AS ratio CAST(
strftime('%s', time)
AS INTEGER
) as time,
CAST(
strftime("%s", DATETIME('now', '-7 day'))
AS INTEGER
) as before
FROM 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<Map<String, dynamic>> getStats() async { static Future<Map<String, dynamic>> getStats() async {
var db = await getDatabase(); var db = await getDatabase();
List<Map<String, dynamic>> answers = await db.rawQuery(''' List<Map<String, dynamic>> answers = await db.rawQuery('''
SELECT SELECT
type, type,
correct, correct,
incorrect, incorrect,
total, total
ratio
FROM FROM
AnswersRatioByTypeView AnswersCorrectByTypeViewAll
UNION ALL
SELECT
type,
correct,
incorrect,
total
FROM
AnswersCorrectByTypeView7d
UNION ALL
SELECT
type,
correct,
incorrect,
total
FROM
AnswersCorrectByTypeView30d;
'''); ''');
print(answers); print(answers);
var first = answers.first;
print(first["practice_id"]);
print("something else"); print("something else");
} }
} }