С SQLite, учитывая таблицы ниже
_id Name
1 StudentA
2 StudentB
3 StudentC
и
id StudentId Test Score
1 1 A 5
2 1 B 5
3 1 A 6
4 1 B 6
5 2 A 3
6 2 B 3
7 2 A 4
или в SQL форме
BEGIN TRANSACTION;
DROP TABLE IF EXISTS "Results";
CREATE TABLE IF NOT EXISTS "Results" (
"id" INTEGER PRIMARY KEY AUTOINCREMENT,
"StudentId" INTEGER,
"Test" TEXT,
"Score" INTEGER,
FOREIGN KEY("StudentId") REFERENCES "Students"("_id")
);
DROP TABLE IF EXISTS "Students";
CREATE TABLE IF NOT EXISTS "Students" (
"_id" INTEGER PRIMARY KEY AUTOINCREMENT,
"Name" TEXT NOT NULL
);
INSERT INTO "Results" ("id","StudentId","Test","Score") VALUES (1,1,'A',5);
INSERT INTO "Results" ("id","StudentId","Test","Score") VALUES (2,1,'B',5);
INSERT INTO "Results" ("id","StudentId","Test","Score") VALUES (3,1,'A',6);
INSERT INTO "Results" ("id","StudentId","Test","Score") VALUES (4,1,'B',6);
INSERT INTO "Results" ("id","StudentId","Test","Score") VALUES (5,2,'A',3);
INSERT INTO "Results" ("id","StudentId","Test","Score") VALUES (6,2,'B',3);
INSERT INTO "Results" ("id","StudentId","Test","Score") VALUES (7,2,'A',4);
INSERT INTO "Students" ("_id","Name") VALUES (1,'StudentA');
INSERT INTO "Students" ("_id","Name") VALUES (2,'StudentB');
INSERT INTO "Students" ("_id","Name") VALUES (3,'StudentC');
COMMIT;
Я хотел бы показать, какие студенты пропустили какие тесты и сколько раз
Приведенный ниже запрос SQL приближает меня, но не совсем то, что мне нужно
SELECT s.Name, r.Test, COUNT(r.Test) AS Count
FROM Students s
LEFT OUTER JOIN Results r ON s._id = r.StudentId
GROUP BY s._id, r.Test
Дает мне таблицу результатов: -
Name Test Count
StudentA A 2
StudentA B 2
StudentB A 2
StudentB B 1
StudentC NULL 0
НО мне нужна таблица, показанная ниже: -
Name Test Count
StudentA A 2
StudentA B 2
StudentB A 2
StudentB B 1
StudentC A 0
StudentC B 0
Есть ли способ сделать это с SQLite?