Skip to content

Commit 0aca746

Browse files
authored
Auto-format .sql files using github.com/sql-formatter-org/sql-formatter - high-scores (#155)
[no important files changed]
1 parent 41e635a commit 0aca746

File tree

6 files changed

+253
-124
lines changed

6 files changed

+253
-124
lines changed
Lines changed: 106 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,119 @@
11
-- store comma-separated list of scores of a game
22
UPDATE results
3-
SET result = score_groups.scores
4-
FROM (
5-
SELECT game_id, GROUP_CONCAT(score) "scores"
6-
FROM (
7-
SELECT scores.game_id, score
8-
FROM scores
9-
INNER JOIN results USING (game_id)
10-
WHERE results.property = 'scores'
11-
ORDER BY scores.rowid
12-
) scores_by_game_id
13-
GROUP BY game_id
14-
) score_groups
15-
WHERE results.game_id = score_groups.game_id;
3+
SET
4+
result = score_groups.scores
5+
FROM
6+
(
7+
SELECT
8+
game_id,
9+
GROUP_CONCAT(score) "scores"
10+
FROM
11+
(
12+
SELECT
13+
scores.game_id,
14+
score
15+
FROM
16+
scores
17+
INNER JOIN results USING (game_id)
18+
WHERE
19+
results.property = 'scores'
20+
ORDER BY
21+
scores.rowid
22+
) scores_by_game_id
23+
GROUP BY
24+
game_id
25+
) score_groups
26+
WHERE
27+
results.game_id = score_groups.game_id;
1628

1729
-- store the latest score by game
1830
UPDATE results
19-
SET result = latest_score.score
20-
FROM (
21-
SELECT scores.game_id, score
22-
FROM scores
23-
INNER JOIN (
24-
SELECT scores.game_id, MAX(scores.rowid) "last_row"
25-
FROM scores
26-
INNER JOIN results USING (game_id)
27-
WHERE results.property = 'latest'
28-
GROUP BY scores.game_id
29-
) latest USING (game_id)
30-
WHERE scores.rowid = latest.last_row
31-
) latest_score
32-
WHERE results.game_id = latest_score.game_id;
31+
SET
32+
result = latest_score.score
33+
FROM
34+
(
35+
SELECT
36+
scores.game_id,
37+
score
38+
FROM
39+
scores
40+
INNER JOIN (
41+
SELECT
42+
scores.game_id,
43+
MAX(scores.rowid) "last_row"
44+
FROM
45+
scores
46+
INNER JOIN results USING (game_id)
47+
WHERE
48+
results.property = 'latest'
49+
GROUP BY
50+
scores.game_id
51+
) latest USING (game_id)
52+
WHERE
53+
scores.rowid = latest.last_row
54+
) latest_score
55+
WHERE
56+
results.game_id = latest_score.game_id;
3357

3458
-- the largest score by game
3559
UPDATE results
36-
SET result = best_score.best
37-
FROM (
38-
SELECT scores.game_id, max(score) "best"
39-
FROM scores
40-
INNER JOIN results USING (game_id)
41-
WHERE results.property = 'personalBest'
42-
GROUP BY scores.game_id
43-
) best_score
44-
WHERE results.game_id = best_score.game_id;
60+
SET
61+
result = best_score.best
62+
FROM
63+
(
64+
SELECT
65+
scores.game_id,
66+
max(score) "best"
67+
FROM
68+
scores
69+
INNER JOIN results USING (game_id)
70+
WHERE
71+
results.property = 'personalBest'
72+
GROUP BY
73+
scores.game_id
74+
) best_score
75+
WHERE
76+
results.game_id = best_score.game_id;
4577

4678
-- for personalTopThree, sort scores descending by game
4779
DROP TABLE IF EXISTS reversed;
48-
CREATE TEMP TABLE reversed AS
49-
SELECT scores.game_id, score
50-
FROM scores
51-
INNER JOIN results USING (game_id)
52-
WHERE results.property = 'personalTopThree'
53-
ORDER BY scores.game_id ASC, score DESC
54-
;
80+
81+
CREATE TEMP TABLE reversed AS
82+
SELECT
83+
scores.game_id,
84+
score
85+
FROM
86+
scores
87+
INNER JOIN results USING (game_id)
88+
WHERE
89+
results.property = 'personalTopThree'
90+
ORDER BY
91+
scores.game_id ASC,
92+
score DESC;
5593

5694
UPDATE results
57-
SET result = top_three_scores.top_three
58-
FROM (
59-
SELECT reversed.game_id, GROUP_CONCAT(reversed.score) "top_three"
60-
FROM reversed
61-
INNER JOIN (
62-
SELECT game_id, MIN(rowid) "first_row"
63-
FROM reversed
64-
GROUP BY game_id
65-
) min_row USING (game_id)
66-
WHERE reversed.rowid < min_row.first_row + 3
67-
GROUP BY reversed.game_id
68-
) top_three_scores
69-
WHERE results.game_id = top_three_scores.game_id;
95+
SET
96+
result = top_three_scores.top_three
97+
FROM
98+
(
99+
SELECT
100+
reversed.game_id,
101+
GROUP_CONCAT(reversed.score) "top_three"
102+
FROM
103+
reversed
104+
INNER JOIN (
105+
SELECT
106+
game_id,
107+
MIN(rowid) "first_row"
108+
FROM
109+
reversed
110+
GROUP BY
111+
game_id
112+
) min_row USING (game_id)
113+
WHERE
114+
reversed.rowid < min_row.first_row + 3
115+
GROUP BY
116+
reversed.game_id
117+
) top_three_scores
118+
WHERE
119+
results.game_id = top_three_scores.game_id;

exercises/practice/high-scores/create_fixture.sql

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,10 @@
11
DROP TABLE IF EXISTS "scores";
2-
CREATE TABLE "scores" (
3-
"game_id" TEXT,
4-
"score" INT
5-
);
2+
3+
CREATE TABLE "scores" ("game_id" TEXT, "score" INT);
64

75
DROP TABLE IF EXISTS "results";
8-
CREATE TABLE "results" (
9-
"game_id" TEXT,
10-
"property" TEXT,
11-
"result" TEXT
12-
);
6+
7+
CREATE TABLE "results" ("game_id" TEXT, "property" TEXT, "result" TEXT);
138

149
.mode csv
1510
.import ./data_scores.csv scores
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
-- Schema:
22
-- CREATE TABLE "scores" ("game_id" TEXT, "score" INT);
33
-- CREATE TABLE "results" ("game_id" TEXT, "property" TEXT, "result" TEXT);
4-
54
-- Task: Given the data in the "scores" table, update the "result" field in the "results" table.
Lines changed: 99 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,109 @@
11
-- Setup test table and read in student solution:
22
.read ./test_setup.sql
3-
43
-- Test cases:
54
-- Note: the strings below may contain literal tab, newline, carriage returns.
6-
7-
INSERT INTO tests (name, uuid,
8-
function, input, expected)
9-
VALUES
10-
('List of scores', '1035eb93-2208-4c22-bab8-fef06769a73c',
11-
'scores', '30,50,20,70', '30,50,20,70'),
12-
('Latest score', '6aa5dbf5-78fa-4375-b22c-ffaa989732d2',
13-
'latest', '100,0,90,30',30),
14-
('Personal best', 'b661a2e1-aebf-4f50-9139-0fb817dd12c6',
15-
'personalBest', '40,100,70',100),
16-
('Personal top three from a list of scores', '3d996a97-c81c-4642-9afc-80b80dc14015',
17-
'personalTopThree', '10,30,90,30,100,20,10,0,30,40,40,70,70', '100,90,70'),
18-
('Personal top highest to lowest', '1084ecb5-3eb4-46fe-a816-e40331a4e83a',
19-
'personalTopThree', '20,10,30', '30,20,10'),
20-
('Personal top when there is a tie', 'e6465b6b-5a11-4936-bfe3-35241c4f4f16',
21-
'personalTopThree', '40,20,40,30', '40,40,30'),
22-
('Personal top when there are less than 3', 'f73b02af-c8fd-41c9-91b9-c86eaa86bce2',
23-
'personalTopThree', '30,70', '70,30'),
24-
('Personal top when there is only one', '16608eae-f60f-4a88-800e-aabce5df2865',
25-
'personalTopThree', '40', '40'),
26-
('Latest score after personal top scores', '2df075f9-fec9-4756-8f40-98c52a11504f',
27-
'latest', '70,50,20,30', '30'),
28-
('Scores after personal top scores', '809c4058-7eb1-4206-b01e-79238b9b71bc',
29-
'scores', '30,50,20,70', '30,50,20,70'),
30-
('Latest score after personal best', 'ddb0efc0-9a86-4f82-bc30-21ae0bdc6418',
31-
'latest', '20,70,15,25,30', '30'),
32-
('Scores after personal best', '6a0fd2d1-4cc4-46b9-a5bb-2fb667ca2364',
33-
'scores', '20,70,15,25,30', '20,70,15,25,30');
5+
INSERT INTO
6+
tests (name, uuid, function, input, expected)
7+
VALUES
8+
(
9+
'List of scores',
10+
'1035eb93-2208-4c22-bab8-fef06769a73c',
11+
'scores',
12+
'30,50,20,70',
13+
'30,50,20,70'
14+
),
15+
(
16+
'Latest score',
17+
'6aa5dbf5-78fa-4375-b22c-ffaa989732d2',
18+
'latest',
19+
'100,0,90,30',
20+
30
21+
),
22+
(
23+
'Personal best',
24+
'b661a2e1-aebf-4f50-9139-0fb817dd12c6',
25+
'personalBest',
26+
'40,100,70',
27+
100
28+
),
29+
(
30+
'Personal top three from a list of scores',
31+
'3d996a97-c81c-4642-9afc-80b80dc14015',
32+
'personalTopThree',
33+
'10,30,90,30,100,20,10,0,30,40,40,70,70',
34+
'100,90,70'
35+
),
36+
(
37+
'Personal top highest to lowest',
38+
'1084ecb5-3eb4-46fe-a816-e40331a4e83a',
39+
'personalTopThree',
40+
'20,10,30',
41+
'30,20,10'
42+
),
43+
(
44+
'Personal top when there is a tie',
45+
'e6465b6b-5a11-4936-bfe3-35241c4f4f16',
46+
'personalTopThree',
47+
'40,20,40,30',
48+
'40,40,30'
49+
),
50+
(
51+
'Personal top when there are less than 3',
52+
'f73b02af-c8fd-41c9-91b9-c86eaa86bce2',
53+
'personalTopThree',
54+
'30,70',
55+
'70,30'
56+
),
57+
(
58+
'Personal top when there is only one',
59+
'16608eae-f60f-4a88-800e-aabce5df2865',
60+
'personalTopThree',
61+
'40',
62+
'40'
63+
),
64+
(
65+
'Latest score after personal top scores',
66+
'2df075f9-fec9-4756-8f40-98c52a11504f',
67+
'latest',
68+
'70,50,20,30',
69+
'30'
70+
),
71+
(
72+
'Scores after personal top scores',
73+
'809c4058-7eb1-4206-b01e-79238b9b71bc',
74+
'scores',
75+
'30,50,20,70',
76+
'30,50,20,70'
77+
),
78+
(
79+
'Latest score after personal best',
80+
'ddb0efc0-9a86-4f82-bc30-21ae0bdc6418',
81+
'latest',
82+
'20,70,15,25,30',
83+
'30'
84+
),
85+
(
86+
'Scores after personal best',
87+
'6a0fd2d1-4cc4-46b9-a5bb-2fb667ca2364',
88+
'scores',
89+
'20,70,15,25,30',
90+
'20,70,15,25,30'
91+
);
3492

3593
-- Comparison of user input and the tests updates the status for each test:
3694
UPDATE tests
37-
SET status = 'pass'
38-
FROM (SELECT game_id, result FROM results) AS actual
39-
WHERE (actual.game_id, actual.result) = (tests.uuid, tests.expected);
95+
SET
96+
status = 'pass'
97+
FROM
98+
(
99+
SELECT
100+
game_id,
101+
result
102+
FROM
103+
results
104+
) AS actual
105+
WHERE
106+
(actual.game_id, actual.result) = (tests.uuid, tests.expected);
40107

41108
-- Write results and debug info:
42109
.read ./test_reporter.sql
Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,39 @@
11
-- Update message for failed tests to give helpful information:
22
UPDATE tests
3-
SET message = (
4-
'Result for "' || tests.name || '"'
5-
|| ' is <' || COALESCE(actual.result, 'NULL')
6-
|| '> but should be <' || tests.expected || '>'
7-
)
8-
FROM (SELECT game_id, result FROM results) AS actual
9-
WHERE actual.game_id = tests.uuid AND tests.status = 'fail';
3+
SET
4+
message = (
5+
'Result for "' || tests.name || '"' || ' is <' || COALESCE(actual.result, 'NULL') || '> but should be <' || tests.expected || '>'
6+
)
7+
FROM
8+
(
9+
SELECT
10+
game_id,
11+
result
12+
FROM
13+
results
14+
) AS actual
15+
WHERE
16+
actual.game_id = tests.uuid
17+
AND tests.status = 'fail';
1018

1119
-- Save results to ./output.json (needed by the online test-runner)
1220
.mode json
1321
.once './output.json'
14-
SELECT name, status, message, output, test_code, task_id
15-
FROM tests;
22+
SELECT
23+
name,
24+
status,
25+
message,
26+
output,
27+
test_code,
28+
task_id
29+
FROM
30+
tests;
1631

1732
-- Display test results in readable form for the student:
1833
.mode table
19-
SELECT name, status, message
20-
FROM tests;
34+
SELECT
35+
name,
36+
status,
37+
message
38+
FROM
39+
tests;

0 commit comments

Comments
 (0)