-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path03_create_duckdb.sql
More file actions
63 lines (61 loc) · 1.96 KB
/
03_create_duckdb.sql
File metadata and controls
63 lines (61 loc) · 1.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# First open the votes database
.open processed_data/votes.duckdb
# First, create all these tables
CREATE TABLE kieskring AS SELECT * FROM read_parquet("processed_data/votes_data/kieskring.parquet");
CREATE TABLE party AS SELECT * FROM read_parquet("processed_data/votes_data/party.parquet");
CREATE TABLE candidate AS SELECT * FROM read_parquet("processed_data/votes_data/candidate.parquet");
CREATE TABLE gemeente AS SELECT * FROM read_parquet("processed_data/votes_data/gemeente.parquet");
CREATE TABLE bureau AS SELECT * FROM read_parquet("processed_data/votes_data/bureau.parquet");
CREATE TABLE postcode AS SELECT * FROM read_parquet("processed_data/votes_data/postcode.parquet");
CREATE TABLE vote AS SELECT * FROM read_parquet("processed_data/votes_data/vote.parquet");
# then, create a view with the full table that people will want to use
CREATE VIEW main AS
SELECT
votetab.kieskring_id AS kieskring_id,
kieskring."name" AS kieskring,
gemeente_id,
gemeente."name" AS gemeente,
bureau_id,
"location",
bureau.postcode AS postcode,
votetab.party_id AS party_id,
party."name" AS party,
votetab.candidate_nr AS candidate_nr,
candidate_id,
first_name,
infix,
last_name,
gender,
"language",
votes,
party_votes,
bureau_votes,
x_centroid,
y_centroid,
population,
area_m2
FROM (
SELECT q01.*, SUM(votes) OVER (PARTITION BY bureau_id) AS bureau_votes
FROM (
SELECT
vote.*,
SUM(votes) OVER (PARTITION BY bureau_id, party_id) AS party_votes
FROM vote
) q01
) AS votetab
LEFT JOIN kieskring
ON (votetab.kieskring_id = kieskring.id)
LEFT JOIN gemeente
ON (votetab.gemeente_id = gemeente.id)
LEFT JOIN bureau
ON (votetab.bureau_id = bureau.id)
LEFT JOIN party
ON (votetab.party_id = party.id)
LEFT JOIN candidate
ON (
votetab.kieskring_id = candidate.kieskring_id AND
votetab.party_id = candidate.party_id AND
votetab.candidate_nr = candidate.candidate_nr
)
LEFT JOIN postcode
ON (bureau.postcode = postcode.postcode);