Skip to content

Commit

Permalink
refactor demographics schema due to broken endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
p3t3r67x0 committed Dec 21, 2024
1 parent c893328 commit f2ce398
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 61 deletions.
96 changes: 36 additions & 60 deletions data/flensburg_sozialatlas.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@
DROP TABLE IF EXISTS fl_household_type CASCADE;

CREATE TABLE IF NOT EXISTS fl_household_type (
id INT,
label VARCHAR,
PRIMARY KEY(id)
id INT PRIMARY KEY NOT NULL,
label VARCHAR NOT NULL
);

CREATE UNIQUE INDEX IF NOT EXISTS fl_household_type_id_idx ON fl_household_type (id);
Expand All @@ -24,11 +23,10 @@ INSERT INTO fl_household_type VALUES
DROP TABLE IF EXISTS fl_residents_by_districts;

CREATE TABLE IF NOT EXISTS fl_residents_by_districts (
id SERIAL,
id SERIAL PRIMARY KEY,
year INT NOT NULL,
district_id INT,
residents INT,
PRIMARY KEY(id),
FOREIGN KEY(district_id) REFERENCES fl_districts(id)
);

Expand Down Expand Up @@ -145,12 +143,11 @@ INSERT INTO fl_residents_by_districts (year, district_id, residents) VALUES
DROP TABLE IF EXISTS fl_births_by_districts;

CREATE TABLE IF NOT EXISTS fl_births_by_districts (
id SERIAL,
id SERIAL PRIMARY KEY,
year INT NOT NULL,
district_id INT,
births INT,
birth_rate NUMERIC,
PRIMARY KEY(id),
FOREIGN KEY(district_id) REFERENCES fl_districts(id)
);

Expand Down Expand Up @@ -280,15 +277,14 @@ INSERT INTO fl_births_by_districts (year, district_id, births, birth_rate) VALUE
DROP TABLE IF EXISTS fl_age_groups_of_residents;

CREATE TABLE IF NOT EXISTS fl_age_groups_of_residents (
id SERIAL,
id SERIAL PRIMARY KEY,
year INT NOT NULL,
age_under_18 INT,
age_18_to_under_30 INT,
age_30_to_under_45 INT,
age_45_to_under_65 INT,
age_65_to_under_80 INT,
age_80_and_above INT,
PRIMARY KEY(id)
age_80_and_above INT
);

CREATE INDEX IF NOT EXISTS fl_age_groups_of_residents_year_idx ON fl_age_groups_of_residents (year);
Expand All @@ -309,11 +305,10 @@ INSERT INTO fl_age_groups_of_residents (year, age_under_18, age_18_to_under_30,
DROP TABLE IF EXISTS fl_age_ratio_by_districts;

CREATE TABLE IF NOT EXISTS fl_age_ratio_by_districts (
id SERIAL,
id SERIAL PRIMARY KEY,
year INT NOT NULL,
district_id INT,
quotient NUMERIC,
PRIMARY KEY(id),
FOREIGN KEY(district_id) REFERENCES fl_districts(id)
);

Expand Down Expand Up @@ -440,10 +435,10 @@ INSERT INTO fl_age_ratio_by_districts (year, district_id, quotient) VALUES


/* TABELLE 5 EINWOHNER IN STADTTEILEN NACH ALTERSGRUPPEN 2021,2022 */
DROP TABLE IF EXISTS fl_age_groups_of_residents_by_districts;
DROP TABLE IF EXISTS fl_age_groups_of_residents_by_district;

CREATE TABLE IF NOT EXISTS fl_age_groups_of_residents_by_districts (
id SERIAL,
CREATE TABLE IF NOT EXISTS fl_age_groups_of_residents_by_district (
id SERIAL PRIMARY KEY,
year INT NOT NULL,
district_id INT,
age_under_18 INT,
Expand All @@ -454,13 +449,12 @@ CREATE TABLE IF NOT EXISTS fl_age_groups_of_residents_by_districts (
age_80_and_above INT,
age_0_to_under_7 INT,
age_60_and_above INT,
PRIMARY KEY(id),
FOREIGN KEY(district_id) REFERENCES fl_districts(id)
);

CREATE INDEX IF NOT EXISTS fl_age_groups_of_residents_by_districts_year_district_id_idx ON fl_age_groups_of_residents_by_districts (year, district_id);
CREATE INDEX IF NOT EXISTS fl_age_groups_of_residents_by_district_year_district_id_idx ON fl_age_groups_of_residents_by_district (year, district_id);

INSERT INTO fl_age_groups_of_residents_by_districts (year, district_id, age_under_18, age_18_to_under_30, age_30_to_under_45, age_45_to_under_65, age_65_to_under_80, age_80_and_above, age_0_to_under_7, age_60_and_above) VALUES
INSERT INTO fl_age_groups_of_residents_by_district (year, district_id, age_under_18, age_18_to_under_30, age_30_to_under_45, age_45_to_under_65, age_65_to_under_80, age_80_and_above, age_0_to_under_7, age_60_and_above) VALUES
(2021, 1, 360, 1338, 951, 804, 265, 148, 174, 565),
(2022, 1, 403, 1345, 941, 822, 263, 133, 176, 558),
(2021, 2, 745, 1597, 1162, 977, 277, 92, 347, 551),
Expand Down Expand Up @@ -494,11 +488,10 @@ INSERT INTO fl_age_groups_of_residents_by_districts (year, district_id, age_unde
DROP TABLE IF EXISTS fl_children_age_under_18_by_districts;

CREATE TABLE IF NOT EXISTS fl_children_age_under_18_by_districts (
id SERIAL,
id SERIAL PRIMARY KEY,
year INT NOT NULL,
district_id INT,
residents INT,
PRIMARY KEY(id),
FOREIGN KEY(district_id) REFERENCES fl_districts(id)
);

Expand Down Expand Up @@ -628,11 +621,10 @@ INSERT INTO fl_children_age_under_18_by_districts (year, district_id, residents)
DROP TABLE IF EXISTS fl_residents_age_18_to_under_65_by_districts;

CREATE TABLE IF NOT EXISTS fl_residents_age_18_to_under_65_by_districts (
id SERIAL,
id SERIAL PRIMARY KEY,
year INT NOT NULL,
district_id INT,
residents INT,
PRIMARY KEY(id),
FOREIGN KEY(district_id) REFERENCES fl_districts(id)
);

Expand Down Expand Up @@ -762,11 +754,10 @@ INSERT INTO fl_residents_age_18_to_under_65_by_districts (year, district_id, res
DROP TABLE IF EXISTS fl_residents_age_65_and_above_by_districts;

CREATE TABLE IF NOT EXISTS fl_residents_age_65_and_above_by_districts (
id SERIAL,
id SERIAL PRIMARY KEY,
year INT NOT NULL,
district_id INT,
residents INT,
PRIMARY KEY(id),
FOREIGN KEY(district_id) REFERENCES fl_districts(id)
);

Expand Down Expand Up @@ -896,12 +887,11 @@ INSERT INTO fl_residents_age_65_and_above_by_districts (year, district_id, resid
DROP TABLE IF EXISTS fl_migration_background_by_districts;

CREATE TABLE IF NOT EXISTS fl_migration_background_by_districts (
id SERIAL,
id SERIAL PRIMARY KEY,
year INT NOT NULL,
district_id INT,
foreign_citizenship INT,
german_citizenship INT,
PRIMARY KEY(id),
FOREIGN KEY(district_id) REFERENCES fl_districts(id)
);

Expand Down Expand Up @@ -953,15 +943,14 @@ INSERT INTO fl_migration_background_by_districts (year, district_id, foreign_cit
DROP TABLE IF EXISTS fl_non_german_nationals_residence_status;

CREATE TABLE IF NOT EXISTS fl_non_german_nationals_residence_status (
id SERIAL,
id SERIAL PRIMARY KEY,
year INT NOT NULL,
permanent_residency INT,
permanent_residency_according_eu_freedom_movement_act INT,
permanent_residency_third_country_nationality INT,
without_permanent_residency INT,
asylum_seeker INT,
suspension_of_deportation INT,
PRIMARY KEY(id)
suspension_of_deportation INT
);

CREATE INDEX IF NOT EXISTS fl_non_german_nationals_residence_status_year_idx ON fl_non_german_nationals_residence_status (year);
Expand All @@ -980,12 +969,11 @@ INSERT INTO fl_non_german_nationals_residence_status (year, permanent_residency,
DROP TABLE IF EXISTS fl_employed_with_pension_insurance_by_districts;

CREATE TABLE IF NOT EXISTS fl_employed_with_pension_insurance_by_districts (
id SERIAL,
id SERIAL PRIMARY KEY,
year INT NOT NULL,
district_id INT,
residents INT,
employment_rate NUMERIC,
PRIMARY KEY(id),
FOREIGN KEY(district_id) REFERENCES fl_districts(id)
);

Expand Down Expand Up @@ -1089,11 +1077,10 @@ INSERT INTO fl_employed_with_pension_insurance_by_districts (year, district_id,
DROP TABLE IF EXISTS fl_unemployed_residents_by_districts;

CREATE TABLE IF NOT EXISTS fl_unemployed_residents_by_districts (
id SERIAL,
id SERIAL PRIMARY KEY,
year INT NOT NULL,
district_id INT,
residents INT,
PRIMARY KEY(id),
FOREIGN KEY(district_id) REFERENCES fl_districts(id)
);

Expand Down Expand Up @@ -1197,7 +1184,7 @@ INSERT INTO fl_unemployed_residents_by_districts (year, district_id, residents)
DROP TABLE IF EXISTS fl_unemployed_residents_by_districts_categorized;

CREATE TABLE IF NOT EXISTS fl_unemployed_residents_by_districts_categorized (
id SERIAL,
id SERIAL PRIMARY KEY,
year INT NOT NULL,
district_id INT,
unemployed_total INT,
Expand All @@ -1207,7 +1194,6 @@ CREATE TABLE IF NOT EXISTS fl_unemployed_residents_by_districts_categorized (
percentage_foreign_citizenship NUMERIC,
percentage_female NUMERIC,
percentage_age_under_25 NUMERIC,
PRIMARY KEY(id),
FOREIGN KEY(district_id) REFERENCES fl_districts(id)
);

Expand Down Expand Up @@ -1262,11 +1248,10 @@ INSERT INTO fl_unemployed_residents_by_districts_categorized (year, district_id,
DROP TABLE IF EXISTS fl_housing_benefit_by_districts;

CREATE TABLE IF NOT EXISTS fl_housing_benefit_by_districts (
id SERIAL,
id SERIAL PRIMARY KEY,
year INT NOT NULL,
district_id INT,
residents INT,
PRIMARY KEY(id),
FOREIGN KEY(district_id) REFERENCES fl_districts(id)
);

Expand Down Expand Up @@ -1396,7 +1381,7 @@ INSERT INTO fl_housing_benefit_by_districts (year, district_id, residents) VALUE
DROP TABLE IF EXISTS fl_housing_assistance_cases_by_districts;

CREATE TABLE IF NOT EXISTS fl_housing_assistance_cases_by_districts (
id SERIAL,
id SERIAL PRIMARY KEY,
year INT NOT NULL,
district_id INT,
general_consulting INT,
Expand All @@ -1406,7 +1391,6 @@ CREATE TABLE IF NOT EXISTS fl_housing_assistance_cases_by_districts (
action_for_eviction INT,
eviction_notice INT,
eviction_carried INT,
PRIMARY KEY(id),
FOREIGN KEY(district_id) REFERENCES fl_districts(id)
);

Expand Down Expand Up @@ -1455,20 +1439,19 @@ INSERT INTO fl_housing_assistance_cases_by_districts (year, district_id, general


/* TABELLE 16 WOHNUNGSLOSIGKEIT BEDROHTE HAUSHALTE IN STADTTEILEN 2017-2022 */
DROP TABLE IF EXISTS fl_households_at_risk_of_homelessness_by_districts;
DROP TABLE IF EXISTS fl_risk_homelessness_by_districts;

CREATE TABLE IF NOT EXISTS fl_households_at_risk_of_homelessness_by_districts (
id SERIAL,
CREATE TABLE IF NOT EXISTS fl_risk_homelessness_by_districts (
id SERIAL PRIMARY KEY,
year INT NOT NULL,
district_id INT,
residents INT,
PRIMARY KEY(id),
FOREIGN KEY(district_id) REFERENCES fl_districts(id)
);

CREATE INDEX IF NOT EXISTS fl_households_at_risk_of_homelessness_by_districts_year_district_id_idx ON fl_households_at_risk_of_homelessness_by_districts (year, district_id);
CREATE INDEX IF NOT EXISTS fl_risk_homelessness_by_districts_year_district_id_idx ON fl_risk_homelessness_by_districts (year, district_id);

INSERT INTO fl_households_at_risk_of_homelessness_by_districts (year, district_id, residents) VALUES
INSERT INTO fl_risk_homelessness_by_districts (year, district_id, residents) VALUES
(2017, 1, 14),
(2018, 1, 5),
(2019, 1, 13),
Expand Down Expand Up @@ -1566,7 +1549,7 @@ INSERT INTO fl_households_at_risk_of_homelessness_by_districts (year, district_i
DROP TABLE IF EXISTS fl_beneficiaries_age_15_to_under_65_by_districts;

CREATE TABLE IF NOT EXISTS fl_beneficiaries_age_15_to_under_65_by_districts (
id SERIAL,
id SERIAL PRIMARY KEY,
year INT NOT NULL,
district_id INT,
total INT,
Expand All @@ -1575,7 +1558,6 @@ CREATE TABLE IF NOT EXISTS fl_beneficiaries_age_15_to_under_65_by_districts (
unemployment_benefits INT,
basic_income INT,
assisting_benefits INT,
PRIMARY KEY(id),
FOREIGN KEY(district_id) REFERENCES fl_districts(id)
);

Expand Down Expand Up @@ -1630,11 +1612,10 @@ INSERT INTO fl_beneficiaries_age_15_to_under_65_by_districts (year,district_id,t
DROP TABLE IF EXISTS fl_beneficiaries_by_districts;

CREATE TABLE IF NOT EXISTS fl_beneficiaries_by_districts (
id SERIAL,
id SERIAL PRIMARY KEY,
year INT NOT NULL,
district_id INT,
residents INT,
PRIMARY KEY(id),
FOREIGN KEY(district_id) REFERENCES fl_districts(id)
);

Expand Down Expand Up @@ -1725,15 +1706,14 @@ INSERT INTO fl_beneficiaries_by_districts (year, district_id, residents) VALUES
DROP TABLE IF EXISTS fl_beneficiaries_characteristics_by_districts;

CREATE TABLE IF NOT EXISTS fl_beneficiaries_characteristics_by_districts (
id SERIAL,
id SERIAL PRIMARY KEY,
year INT,
district_id INT,
unemployability INT,
employability INT,
percentage_females NUMERIC,
percentage_single_parents NUMERIC,
percentage_foreign_citizenship NUMERIC,
PRIMARY KEY(id),
FOREIGN KEY(district_id) REFERENCES fl_districts(id)
);

Expand Down Expand Up @@ -1789,11 +1769,10 @@ INSERT INTO fl_beneficiaries_characteristics_by_districts (year, district_id, un
DROP TABLE IF EXISTS fl_inactive_beneficiaries_in_households_by_districts;

CREATE TABLE IF NOT EXISTS fl_inactive_beneficiaries_in_households_by_districts (
id SERIAL,
id SERIAL PRIMARY KEY,
year INT NOT NULL,
district_id INT,
residents INT,
PRIMARY KEY(id),
FOREIGN KEY(district_id) REFERENCES fl_districts(id)
);

Expand Down Expand Up @@ -1884,14 +1863,13 @@ INSERT INTO fl_inactive_beneficiaries_in_households_by_districts (year, district
DROP TABLE IF EXISTS fl_basic_benefits_income_by_districts;

CREATE TABLE IF NOT EXISTS fl_basic_benefits_income_by_districts (
id SERIAL,
id SERIAL PRIMARY KEY,
year INT NOT NULL,
district_id INT,
male INT,
female INT,
age_18_to_under_65 INT,
age_65_and_above INT,
PRIMARY KEY(id),
FOREIGN KEY(district_id) REFERENCES fl_districts(id)
);

Expand Down Expand Up @@ -1948,11 +1926,10 @@ INSERT INTO fl_basic_benefits_income_by_districts (year, district_id, male, fema
DROP TABLE IF EXISTS fl_debt_counseling_residents;

CREATE TABLE IF NOT EXISTS fl_debt_counseling_residents (
id SERIAL,
id SERIAL PRIMARY KEY,
year INT NOT NULL,
household_type_id INT,
residents INT,
PRIMARY KEY(id),
FOREIGN KEY(household_type_id) REFERENCES fl_household_type(id)
);

Expand Down Expand Up @@ -1986,7 +1963,7 @@ INSERT INTO fl_debt_counseling_residents (year, household_type_id, residents) VA
DROP TABLE IF EXISTS fl_child_education_support CASCADE;

CREATE TABLE IF NOT EXISTS fl_child_education_support (
id SERIAL,
id SERIAL PRIMARY KEY,
year INT NOT NULL,
educational_assistance INT,
parenting_counselor INT,
Expand All @@ -1995,8 +1972,7 @@ CREATE TABLE IF NOT EXISTS fl_child_education_support (
full_time_care INT,
residential_education INT,
integration_assistance INT,
additional_support INT,
PRIMARY KEY(id)
additional_support INT
);

CREATE INDEX IF NOT EXISTS fl_child_education_support_year_idx ON fl_child_education_support (year);
Expand Down
2 changes: 1 addition & 1 deletion data/flensburg_stadtteile.sql
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* HILFSTABELLE STADTTEILEILE MIT NAMEN */
/* HILFSTABELLE STADTTEILEILE FLENSBURG */
DROP TABLE IF EXISTS fl_districts CASCADE;

CREATE TABLE IF NOT EXISTS fl_districts (
Expand Down

0 comments on commit f2ce398

Please sign in to comment.