Skip to content

Commit

Permalink
Fix 00-lotto-db.sql
Browse files Browse the repository at this point in the history
This was accidentally committed as symbolic link when it should have been a file.
  • Loading branch information
kahgoh committed Mar 23, 2024
1 parent 4778bb6 commit 61e72b5
Showing 1 changed file with 35 additions and 1 deletion.
1 change: 0 additions & 1 deletion sql/00-lotto-db.sql

This file was deleted.

35 changes: 35 additions & 0 deletions sql/00-lotto-db.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
CREATE EXTENSION intarray;

CREATE TYPE game_type AS ENUM (
'SATURDAY',
'MONDAY',
'WEDNESDAY',
'OZ',
'POWERBALL',
'SET_FOR_LIFE'
);

CREATE TABLE games (
type game_type,
game int,
numbers int[],
supplementaries int[],
CONSTRAINT games_pk PRIMARY KEY (type, game)
);

CREATE VIEW sorted_games (type, game, numbers, supplementaries)
AS
SELECT t.type, t.game, sort(t.numbers)::int[] AS numbers, sort(t.supplementaries)::int[] as supplementaries
FROM games t;

CREATE FUNCTION summarise_matches(game_type game_type, numbers int[], min_numbers int)
RETURNS TABLE(numbers_count int, supp_count int, games_count int, latest_game int)
AS $$
SELECT numbers_count, supp_count, count(*) as games_count, max(game) as latest_game
FROM
(SELECT icount(numbers & $2) AS numbers_count, icount(supplementaries & $2) AS supp_count, game
FROM sorted_games WHERE type = $1) c
WHERE numbers_count >= $3
GROUP BY (numbers_count, supp_count)
ORDER BY numbers_count DESC, supp_count DESC
$$ LANGUAGE SQL;

0 comments on commit 61e72b5

Please sign in to comment.