From 3cbd9c1d306c7c4dbdcecaf9dff0830ee6d22f6b Mon Sep 17 00:00:00 2001 From: hhssb Date: Mon, 21 Oct 2024 10:58:25 +0200 Subject: [PATCH 1/2] [test] Add tests for statistical_history --- ...al-units-sector-change-middle-of-month.csv | 3 + ...ts-sector-change-start-of-second-month.csv | 3 + ...egal-units-sector-change-start-of-year.csv | 4 + .../41_legal-units-births-middle-of-month.csv | 5 + ...gal-units-births-start-of-second-month.csv | 5 + .../41_legal-units-births-start-of-year.csv | 5 + ..._legal-units-deaths-end-of-first-month.csv | 4 + .../42_legal-units-deaths-end-of-year.csv | 4 + .../42_legal-units-deaths-start-of-month.csv | 4 + ..._history_legal_units_changes_over_time.out | 0 .../41_history_legal_units_births.out | 0 .../42_history_legal_units_deaths.out | 0 ..._history_legal_units_changes_over_time.sql | 174 ++++++++++++++++++ test/sql/41_history_legal_units_births.sql | 174 ++++++++++++++++++ test/sql/42_history_legal_units_deaths.sql | 173 +++++++++++++++++ 15 files changed, 558 insertions(+) create mode 100644 test/data/40_legal-units-sector-change-middle-of-month.csv create mode 100644 test/data/40_legal-units-sector-change-start-of-second-month.csv create mode 100644 test/data/40_legal-units-sector-change-start-of-year.csv create mode 100644 test/data/41_legal-units-births-middle-of-month.csv create mode 100644 test/data/41_legal-units-births-start-of-second-month.csv create mode 100644 test/data/41_legal-units-births-start-of-year.csv create mode 100644 test/data/42_legal-units-deaths-end-of-first-month.csv create mode 100644 test/data/42_legal-units-deaths-end-of-year.csv create mode 100644 test/data/42_legal-units-deaths-start-of-month.csv create mode 100644 test/expected/40_history_legal_units_changes_over_time.out create mode 100644 test/expected/41_history_legal_units_births.out create mode 100644 test/expected/42_history_legal_units_deaths.out create mode 100644 test/sql/40_history_legal_units_changes_over_time.sql create mode 100644 test/sql/41_history_legal_units_births.sql create mode 100644 test/sql/42_history_legal_units_deaths.sql diff --git a/test/data/40_legal-units-sector-change-middle-of-month.csv b/test/data/40_legal-units-sector-change-middle-of-month.csv new file mode 100644 index 000000000..fa5e0435a --- /dev/null +++ b/test/data/40_legal-units-sector-change-middle-of-month.csv @@ -0,0 +1,3 @@ +valid_from,valid_to,tax_ident,name,birth_date,death_date,physical_address_part1,physical_postal_code,physical_postal_place,physical_region_code,physical_country_iso_2,postal_address_part1,postal_postal_code,postal_postal_place,postal_region_code,postal_country_iso_2,primary_activity_category_code,secondary_activity_category_code,sector_code,legal_form_code +2010-01-01,2011-01-14,823573673,Kranløft Vestland,2010-01-01,,Vestlyvegen 219,4347,LYE,1121,NO,,,,,,49.410,,2100,AS +2011-01-15,infinity,823573673,Kranløft Vestland,2010-01-01,,Vestlyvegen 219,4347,LYE,1121,NO,,,,,,49.410,,8200,AS diff --git a/test/data/40_legal-units-sector-change-start-of-second-month.csv b/test/data/40_legal-units-sector-change-start-of-second-month.csv new file mode 100644 index 000000000..b7c8caeb1 --- /dev/null +++ b/test/data/40_legal-units-sector-change-start-of-second-month.csv @@ -0,0 +1,3 @@ +valid_from,valid_to,tax_ident,name,birth_date,death_date,physical_address_part1,physical_postal_code,physical_postal_place,physical_region_code,physical_country_iso_2,postal_address_part1,postal_postal_code,postal_postal_place,postal_region_code,postal_country_iso_2,primary_activity_category_code,secondary_activity_category_code,sector_code,legal_form_code +2010-01-01,2011-01-31,823573673,Kranløft Vestland,2010-01-01,,Vestlyvegen 219,4347,LYE,1121,NO,,,,,,49.410,,2100,AS +2011-02-01,infinity,823573673,Kranløft Vestland,2010-01-01,,Vestlyvegen 219,4347,LYE,1121,NO,,,,,,49.410,,8200,AS diff --git a/test/data/40_legal-units-sector-change-start-of-year.csv b/test/data/40_legal-units-sector-change-start-of-year.csv new file mode 100644 index 000000000..6d8d44a21 --- /dev/null +++ b/test/data/40_legal-units-sector-change-start-of-year.csv @@ -0,0 +1,4 @@ +valid_from,valid_to,tax_ident,name,birth_date,death_date,physical_address_part1,physical_postal_code,physical_postal_place,physical_region_code,physical_country_iso_2,postal_address_part1,postal_postal_code,postal_postal_place,postal_region_code,postal_country_iso_2,primary_activity_category_code,secondary_activity_category_code,sector_code,legal_form_code +2010-01-01,2010-12-31,823573673,Kranløft Vestland,2010-01-01,,Vestlyvegen 219,4347,LYE,1121,NO,,,,,,49.410,,2100,AS +2011-01-01,2011-12-31,823573673,Kranløft Vestland,2010-01-01,,Vestlyvegen 219,4347,LYE,1121,NO,,,,,,49.410,,7000,AS +2012-01-01,infinity,823573673,Kranløft Vestland,2010-01-01,,Vestlyvegen 219,4347,LYE,1121,NO,,,,,,49.410,,8200,AS diff --git a/test/data/41_legal-units-births-middle-of-month.csv b/test/data/41_legal-units-births-middle-of-month.csv new file mode 100644 index 000000000..1b0301969 --- /dev/null +++ b/test/data/41_legal-units-births-middle-of-month.csv @@ -0,0 +1,5 @@ +valid_from,valid_to,tax_ident,name,birth_date,death_date,physical_address_part1,physical_postal_code,physical_postal_place,physical_region_code,physical_country_iso_2,postal_address_part1,postal_postal_code,postal_postal_place,postal_region_code,postal_country_iso_2,primary_activity_category_code,secondary_activity_category_code,sector_code,legal_form_code +2010-01-15,2010-12-31,823573673,Kranløft Vestland,2010-01-15,,Vestlyvegen 219,4347,LYE,1121,NO,,,,,,49.410,,2100,AS +2011-01-01,2012-01-14,823573673,Kranløft Vestland,2010-01-15,,Vestlyvegen 219,4347,LYE,1121,NO,,,,,,49.410,,2100,AS +2012-01-15,infinity,823573673,Kranløft Vestland,2010-01-15,,Vestlyvegen 219,4347,LYE,1121,NO,,,,,,49.410,,2100,AS +2011-01-15,infinity,921835809,Kranløft Østland,2011-01-15,,Vestlyvegen 219,4347,LYE,1121,NO,,,,,,49.410,,2100,AS diff --git a/test/data/41_legal-units-births-start-of-second-month.csv b/test/data/41_legal-units-births-start-of-second-month.csv new file mode 100644 index 000000000..02bd0193d --- /dev/null +++ b/test/data/41_legal-units-births-start-of-second-month.csv @@ -0,0 +1,5 @@ +valid_from,valid_to,tax_ident,name,birth_date,death_date,physical_address_part1,physical_postal_code,physical_postal_place,physical_region_code,physical_country_iso_2,postal_address_part1,postal_postal_code,postal_postal_place,postal_region_code,postal_country_iso_2,primary_activity_category_code,secondary_activity_category_code,sector_code,legal_form_code +2010-02-01,2010-12-31,823573673,Kranløft Vestland,2010-02-01,,Vestlyvegen 219,4347,LYE,1121,NO,,,,,,49.410,,2100,AS +2011-01-01,2012-01-14,823573673,Kranløft Vestland,2010-02-01,,Vestlyvegen 219,4347,LYE,1121,NO,,,,,,49.410,,2100,AS +2012-01-15,infinity,823573673,Kranløft Vestland,2010-02-01,,Vestlyvegen 219,4347,LYE,1121,NO,,,,,,49.410,,2100,AS +2011-02-01,infinity,921835809,Kranløft Østland,2011-02-01,,Vestlyvegen 219,4347,LYE,1121,NO,,,,,,49.410,,2100,AS diff --git a/test/data/41_legal-units-births-start-of-year.csv b/test/data/41_legal-units-births-start-of-year.csv new file mode 100644 index 000000000..12523ec0f --- /dev/null +++ b/test/data/41_legal-units-births-start-of-year.csv @@ -0,0 +1,5 @@ +valid_from,valid_to,tax_ident,name,birth_date,death_date,physical_address_part1,physical_postal_code,physical_postal_place,physical_region_code,physical_country_iso_2,postal_address_part1,postal_postal_code,postal_postal_place,postal_region_code,postal_country_iso_2,primary_activity_category_code,secondary_activity_category_code,sector_code,legal_form_code +2010-01-01,2010-12-31,823573673,Kranløft Vestland,2010-01-01,,Vestlyvegen 219,4347,LYE,1121,NO,,,,,,49.410,,2100,AS +2011-01-01,2012-01-14,823573673,Kranløft Vestland,2010-01-01,,Vestlyvegen 219,4347,LYE,1121,NO,,,,,,49.410,,2100,AS +2012-01-15,infinity,823573673,Kranløft Vestland,2010-01-01,,Vestlyvegen 219,4347,LYE,1121,NO,,,,,,49.410,,2100,AS +2011-01-01,infinity,921835809,Kranløft Østland,2011-01-01,,Vestlyvegen 219,4347,LYE,1121,NO,,,,,,49.410,,2100,AS diff --git a/test/data/42_legal-units-deaths-end-of-first-month.csv b/test/data/42_legal-units-deaths-end-of-first-month.csv new file mode 100644 index 000000000..939293748 --- /dev/null +++ b/test/data/42_legal-units-deaths-end-of-first-month.csv @@ -0,0 +1,4 @@ +valid_from,valid_to,tax_ident,name,birth_date,death_date,physical_address_part1,physical_postal_code,physical_postal_place,physical_region_code,physical_country_iso_2,postal_address_part1,postal_postal_code,postal_postal_place,postal_region_code,postal_country_iso_2,primary_activity_category_code,secondary_activity_category_code,sector_code,legal_form_code +2010-01-01,2011-12-31,823573673,Kranløft Vestland,2010-01-01,,Vestlyvegen 219,4347,LYE,1121,NO,,,,,,49.410,,2100,AS +2012-01-01,2012-12-31,823573673,Kranløft Vestland,2010-01-01,2012-12-31,Vestlyvegen 219,4347,LYE,1121,NO,,,,,,49.410,,2100,AS +2010-01-01,2011-01-31,921835809,Kranløft Østland,2010-01-01,2011-01-31,Vestlyvegen 219,4347,LYE,1121,NO,,,,,,49.410,,2100,AS diff --git a/test/data/42_legal-units-deaths-end-of-year.csv b/test/data/42_legal-units-deaths-end-of-year.csv new file mode 100644 index 000000000..20da52831 --- /dev/null +++ b/test/data/42_legal-units-deaths-end-of-year.csv @@ -0,0 +1,4 @@ +valid_from,valid_to,tax_ident,name,birth_date,death_date,physical_address_part1,physical_postal_code,physical_postal_place,physical_region_code,physical_country_iso_2,postal_address_part1,postal_postal_code,postal_postal_place,postal_region_code,postal_country_iso_2,primary_activity_category_code,secondary_activity_category_code,sector_code,legal_form_code +2010-01-01,2010-12-31,823573673,Kranløft Vestland,2010-01-01,,Vestlyvegen 219,4347,LYE,1121,NO,,,,,,49.410,,2100,AS +2011-01-01,2012-12-31,823573673,Kranløft Vestland,2010-01-01,2012-12-31,Vestlyvegen 219,4347,LYE,1121,NO,,,,,,49.410,,2100,AS +2010-01-01,2011-12-31,921835809,Kranløft Østland,2010-01-01,2011-12-31,Vestlyvegen 219,4347,LYE,1121,NO,,,,,,49.410,,2100,AS diff --git a/test/data/42_legal-units-deaths-start-of-month.csv b/test/data/42_legal-units-deaths-start-of-month.csv new file mode 100644 index 000000000..bbe84fafa --- /dev/null +++ b/test/data/42_legal-units-deaths-start-of-month.csv @@ -0,0 +1,4 @@ +valid_from,valid_to,tax_ident,name,birth_date,death_date,physical_address_part1,physical_postal_code,physical_postal_place,physical_region_code,physical_country_iso_2,postal_address_part1,postal_postal_code,postal_postal_place,postal_region_code,postal_country_iso_2,primary_activity_category_code,secondary_activity_category_code,sector_code,legal_form_code +2010-01-01,2010-12-31,823573673,Kranløft Vestland,2010-01-01,,Vestlyvegen 219,4347,LYE,1121,NO,,,,,,49.410,,2100,AS +2011-01-01,2012-01-01,823573673,Kranløft Vestland,2010-01-01,2012-01-01,Vestlyvegen 219,4347,LYE,1121,NO,,,,,,49.410,,2100,AS +2010-01-01,2011-01-01,921835809,Kranløft Østland,2010-01-01,2011-01-01,Vestlyvegen 219,4347,LYE,1121,NO,,,,,,49.410,,2100,AS diff --git a/test/expected/40_history_legal_units_changes_over_time.out b/test/expected/40_history_legal_units_changes_over_time.out new file mode 100644 index 000000000..e69de29bb diff --git a/test/expected/41_history_legal_units_births.out b/test/expected/41_history_legal_units_births.out new file mode 100644 index 000000000..e69de29bb diff --git a/test/expected/42_history_legal_units_deaths.out b/test/expected/42_history_legal_units_deaths.out new file mode 100644 index 000000000..e69de29bb diff --git a/test/sql/40_history_legal_units_changes_over_time.sql b/test/sql/40_history_legal_units_changes_over_time.sql new file mode 100644 index 000000000..a0cd51882 --- /dev/null +++ b/test/sql/40_history_legal_units_changes_over_time.sql @@ -0,0 +1,174 @@ +SET datestyle TO 'ISO, DMY'; + +BEGIN; + +\echo "Setting up Statbus to test enterprise grouping and primary" + +\echo "User selected the Activity Category Standard" +INSERT INTO settings(activity_category_standard_id,only_one_setting) +SELECT id, true FROM activity_category_standard WHERE code = 'nace_v2.1' +ON CONFLICT (only_one_setting) +DO UPDATE SET + activity_category_standard_id =(SELECT id FROM activity_category_standard WHERE code = 'nace_v2.1') + WHERE settings.id = EXCLUDED.id; +; +SELECT acs.code + FROM public.settings AS s + JOIN activity_category_standard AS acs + ON s.activity_category_standard_id = acs.id; + +\echo "User uploads the sample activity categories" +\copy public.activity_category_available_custom(path,name,description) FROM 'app/public/activity_category_norway.csv' WITH (FORMAT csv, DELIMITER ',', QUOTE '"', HEADER true); +SELECT count(*) FROM public.activity_category_available; + +\echo "User uploads the sample regions" +\copy public.region_upload(path, name) FROM 'app/public/norway-regions-2024.csv' WITH (FORMAT csv, DELIMITER ',', QUOTE '"', HEADER true); +SELECT count(*) FROM public.region; + +\echo "User uploads the sample legal forms" +\copy public.legal_form_custom_only(code,name) FROM 'app/public/legal_form_norway.csv' WITH (FORMAT csv, DELIMITER ',', QUOTE '"', HEADER true); +SELECT count(*) FROM public.legal_form_available; + +\echo "User uploads the sample sectors" +\copy public.sector_custom_only(path,name,description) FROM 'app/public/sector_norway.csv' WITH (FORMAT csv, DELIMITER ',', QUOTE '"', HEADER true); +SELECT count(*) FROM public.sector_available; + +SAVEPOINT before_loading_units; + +\echo "Test sector changes in the middle of a month" + +SELECT + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.establishment) AS establishment_count, + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.legal_unit) AS legal_unit_count, + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.enterprise) AS enterprise_count; + +\echo "User uploads the legal units" +\copy public.import_legal_unit_era(valid_from,valid_to,tax_ident,name,birth_date,death_date,physical_address_part1,physical_postal_code,physical_postal_place,physical_region_code,physical_country_iso_2,postal_address_part1,postal_postal_code,postal_postal_place,postal_region_code,postal_country_iso_2,primary_activity_category_code,secondary_activity_category_code,sector_code,legal_form_code) FROM 'test/data/40_legal-units-sector-change-middle-of-month.csv' WITH (FORMAT csv, DELIMITER ',', QUOTE '"', HEADER true); + +SELECT + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.establishment) AS establishment_count, + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.legal_unit) AS legal_unit_count, + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.enterprise) AS enterprise_count; + +\echo "Refreshing materialized views" +-- Exclude the refresh_time_ms as it will vary. +SELECT view_name FROM statistical_unit_refresh_now(); + +\echo "Check sector for legal units over time" +SELECT external_idents ->> 'tax_ident' as tax_ident, name, valid_after, valid_from, valid_to, sector_code +FROM public.statistical_unit +WHERE unit_type = 'legal_unit'; + +\echo "Test statistical unit history by year - sector_change_count should be 1 for year 2011" +SELECT resolution, year, unit_type, count, births, deaths, sector_change_count +FROM public.statistical_history +WHERE resolution = 'year' +AND year < 2013 +AND unit_type = 'legal_unit'; + + +\echo "Test statistical unit history by year-month - sector_change_count should be 1 for year-month 2011-1" +SELECT resolution, year, month, unit_type, count, births, deaths, sector_change_count +FROM public.statistical_history +WHERE resolution = 'year-month' +AND year < 2013 +AND unit_type = 'legal_unit'; + +\x + + +ROLLBACK TO before_loading_units; + +\echo "Test sector changes at the start of the second month" + + +\x +SELECT + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.establishment) AS establishment_count, + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.legal_unit) AS legal_unit_count, + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.enterprise) AS enterprise_count; + +\echo "User uploads the legal units" +\copy public.import_legal_unit_era(valid_from,valid_to,tax_ident,name,birth_date,death_date,physical_address_part1,physical_postal_code,physical_postal_place,physical_region_code,physical_country_iso_2,postal_address_part1,postal_postal_code,postal_postal_place,postal_region_code,postal_country_iso_2,primary_activity_category_code,secondary_activity_category_code,sector_code,legal_form_code) FROM 'test/data/40_legal-units-sector-change-start-of-second-month.csv' WITH (FORMAT csv, DELIMITER ',', QUOTE '"', HEADER true); + +SELECT + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.establishment) AS establishment_count, + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.legal_unit) AS legal_unit_count, + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.enterprise) AS enterprise_count; + +\echo "Refreshing materialized views" +-- Exclude the refresh_time_ms as it will vary. +SELECT view_name FROM statistical_unit_refresh_now(); + +\echo "Check sector for legal units over time" +SELECT external_idents ->> 'tax_ident' as tax_ident, name, valid_after, valid_from, valid_to, sector_code +FROM public.statistical_unit +WHERE unit_type = 'legal_unit'; + + +\echo "Check statistical unit history by year - sector_change_count should be 1 for year 2011" +SELECT resolution, year,month, unit_type, count, births, deaths, sector_change_count +FROM public.statistical_history +WHERE resolution = 'year' +AND year < 2012 +AND unit_type = 'legal_unit'; + + + +\echo "Check statistical unit history by year-month - sector_change_count should be 1 for year-month 2011-2" +SELECT resolution, year, month, unit_type, count, births, deaths, sector_change_count +FROM public.statistical_history +WHERE resolution = 'year-month' +AND year < 2012 +AND unit_type = 'legal_unit'; + +\x + +ROLLBACK TO before_loading_units; + +\echo "Test sector changes at the start of the year" + +\x +SELECT + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.establishment) AS establishment_count, + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.legal_unit) AS legal_unit_count, + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.enterprise) AS enterprise_count; + +\echo "User uploads the legal units" +\copy public.import_legal_unit_era(valid_from,valid_to,tax_ident,name,birth_date,death_date,physical_address_part1,physical_postal_code,physical_postal_place,physical_region_code,physical_country_iso_2,postal_address_part1,postal_postal_code,postal_postal_place,postal_region_code,postal_country_iso_2,primary_activity_category_code,secondary_activity_category_code,sector_code,legal_form_code) FROM 'test/data/40_legal-units-sector-change-start-of-year.csv' WITH (FORMAT csv, DELIMITER ',', QUOTE '"', HEADER true); + + +SELECT + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.establishment) AS establishment_count, + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.legal_unit) AS legal_unit_count, + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.enterprise) AS enterprise_count; + +\echo "Refreshing materialized views" +-- Exclude the refresh_time_ms as it will vary. +SELECT view_name FROM statistical_unit_refresh_now(); + + +\echo "Check sector for legal units over time" +SELECT external_idents ->> 'tax_ident' as tax_ident, name, valid_after, valid_from, valid_to, sector_code +FROM public.statistical_unit +WHERE unit_type = 'legal_unit'; + + +\echo "Check statistical unit history by year - sector_change_count should be 1 for year 2011 and 2012" +SELECT resolution, year,month, unit_type, count, births, deaths, sector_change_count +FROM public.statistical_history +WHERE resolution = 'year' +AND year < 2014 +AND unit_type = 'legal_unit'; + + +\echo "Check statistical unit history by year-month - sector_change_count should be 1 for year-month 2011-1 and 2012-1" +SELECT resolution, year, month, unit_type, count, births, deaths, sector_change_count +FROM public.statistical_history +WHERE resolution = 'year-month' +AND year < 2013 +AND unit_type = 'legal_unit'; + +\x + +ROLLBACK; \ No newline at end of file diff --git a/test/sql/41_history_legal_units_births.sql b/test/sql/41_history_legal_units_births.sql new file mode 100644 index 000000000..ed24b5880 --- /dev/null +++ b/test/sql/41_history_legal_units_births.sql @@ -0,0 +1,174 @@ +SET datestyle TO 'ISO, DMY'; + +BEGIN; + +\echo "Setting up Statbus to test enterprise grouping and primary" + +\echo "User selected the Activity Category Standard" +INSERT INTO settings(activity_category_standard_id,only_one_setting) +SELECT id, true FROM activity_category_standard WHERE code = 'nace_v2.1' +ON CONFLICT (only_one_setting) +DO UPDATE SET + activity_category_standard_id =(SELECT id FROM activity_category_standard WHERE code = 'nace_v2.1') + WHERE settings.id = EXCLUDED.id; +; +SELECT acs.code + FROM public.settings AS s + JOIN activity_category_standard AS acs + ON s.activity_category_standard_id = acs.id; + +\echo "User uploads the sample activity categories" +\copy public.activity_category_available_custom(path,name,description) FROM 'app/public/activity_category_norway.csv' WITH (FORMAT csv, DELIMITER ',', QUOTE '"', HEADER true); +SELECT count(*) FROM public.activity_category_available; + +\echo "User uploads the sample regions" +\copy public.region_upload(path, name) FROM 'app/public/norway-regions-2024.csv' WITH (FORMAT csv, DELIMITER ',', QUOTE '"', HEADER true); +SELECT count(*) FROM public.region; + +\echo "User uploads the sample legal forms" +\copy public.legal_form_custom_only(code,name) FROM 'app/public/legal_form_norway.csv' WITH (FORMAT csv, DELIMITER ',', QUOTE '"', HEADER true); +SELECT count(*) FROM public.legal_form_available; + +\echo "User uploads the sample sectors" +\copy public.sector_custom_only(path,name,description) FROM 'app/public/sector_norway.csv' WITH (FORMAT csv, DELIMITER ',', QUOTE '"', HEADER true); +SELECT count(*) FROM public.sector_available; + +SAVEPOINT before_loading_units; + +\echo "Test births at the start of the year" + +SELECT + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.establishment) AS establishment_count, + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.legal_unit) AS legal_unit_count, + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.enterprise) AS enterprise_count; + +\echo "User uploads the legal units" +\copy public.import_legal_unit_era(valid_from,valid_to,tax_ident,name,birth_date,death_date,physical_address_part1,physical_postal_code,physical_postal_place,physical_region_code,physical_country_iso_2,postal_address_part1,postal_postal_code,postal_postal_place,postal_region_code,postal_country_iso_2,primary_activity_category_code,secondary_activity_category_code,sector_code,legal_form_code) FROM 'test/data/41_legal-units-births-start-of-year.csv' WITH (FORMAT csv, DELIMITER ',', QUOTE '"', HEADER true); + + +SELECT + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.establishment) AS establishment_count, + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.legal_unit) AS legal_unit_count, + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.enterprise) AS enterprise_count; + +\echo "Refreshing materialized views" +-- Exclude the refresh_time_ms as it will vary. +SELECT view_name FROM statistical_unit_refresh_now(); + + +\echo "Check legal units over time" +SELECT external_idents ->> 'tax_ident' as tax_ident, name, valid_after, valid_from, valid_to, birth_date, death_date +FROM public.statistical_unit +WHERE unit_type = 'legal_unit'; + +\echo "Check statistical unit history by year - births should be 1 for year 2010 and 2011" +SELECT resolution, year, month, unit_type, count, births, deaths +FROM public.statistical_history +WHERE resolution = 'year' +AND year < 2013 +AND unit_type = 'legal_unit'; + + +\echo "Check statistical unit history by year-month - births should be 1 for year-month 2010-1 and 2011-1" +SELECT resolution, year, month, unit_type, count, births, deaths +FROM public.statistical_history +WHERE resolution = 'year-month' +AND year < 2013 +AND unit_type = 'legal_unit'; + + +\x + + +ROLLBACK TO before_loading_units; + +\echo "Test births at the start of the second month" + +\x +SELECT + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.establishment) AS establishment_count, + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.legal_unit) AS legal_unit_count, + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.enterprise) AS enterprise_count; + +\echo "User uploads the legal units" +\copy public.import_legal_unit_era(valid_from,valid_to,tax_ident,name,birth_date,death_date,physical_address_part1,physical_postal_code,physical_postal_place,physical_region_code,physical_country_iso_2,postal_address_part1,postal_postal_code,postal_postal_place,postal_region_code,postal_country_iso_2,primary_activity_category_code,secondary_activity_category_code,sector_code,legal_form_code) FROM 'test/data/41_legal-units-births-start-of-second-month.csv' WITH (FORMAT csv, DELIMITER ',', QUOTE '"', HEADER true); + +SELECT + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.establishment) AS establishment_count, + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.legal_unit) AS legal_unit_count, + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.enterprise) AS enterprise_count; + +\echo "Refreshing materialized views" +-- Exclude the refresh_time_ms as it will vary. +SELECT view_name FROM statistical_unit_refresh_now(); + +\echo "Check legal units over time" +SELECT external_idents ->> 'tax_ident' as tax_ident, name, valid_after, valid_from, valid_to, birth_date, death_date +FROM public.statistical_unit +WHERE unit_type = 'legal_unit'; + +\echo "Check statistical unit history by year - births should be 1 for year 2010 and 2011" +SELECT resolution, year, month, unit_type, count, births, deaths +FROM public.statistical_history +WHERE resolution = 'year' +AND year < 2013 +AND unit_type = 'legal_unit'; + + +\echo "Check statistical unit history by year-month - births should be 1 for year-month 2010-2 and 2011-2" +SELECT resolution, year, month, unit_type, count, births, deaths +FROM public.statistical_history +WHERE resolution = 'year-month' +AND year < 2013 +AND unit_type = 'legal_unit'; + + +\x + +ROLLBACK TO before_loading_units; + +\echo "Test births in the middle of a month" + +\x +SELECT + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.establishment) AS establishment_count, + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.legal_unit) AS legal_unit_count, + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.enterprise) AS enterprise_count; + +\echo "User uploads the legal units" +\copy public.import_legal_unit_era(valid_from,valid_to,tax_ident,name,birth_date,death_date,physical_address_part1,physical_postal_code,physical_postal_place,physical_region_code,physical_country_iso_2,postal_address_part1,postal_postal_code,postal_postal_place,postal_region_code,postal_country_iso_2,primary_activity_category_code,secondary_activity_category_code,sector_code,legal_form_code) FROM 'test/data/41_legal-units-births-middle-of-month.csv' WITH (FORMAT csv, DELIMITER ',', QUOTE '"', HEADER true); + + +SELECT + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.establishment) AS establishment_count, + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.legal_unit) AS legal_unit_count, + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.enterprise) AS enterprise_count; + +\echo "Refreshing materialized views" +-- Exclude the refresh_time_ms as it will vary. +SELECT view_name FROM statistical_unit_refresh_now(); + +\echo "Check legal units over time" +SELECT external_idents ->> 'tax_ident' as tax_ident, name, valid_after, valid_from, valid_to, birth_date, death_date +FROM public.statistical_unit +WHERE unit_type = 'legal_unit'; + + +\echo "Check statistical unit history by year - births should be 1 for year 2010 and 2011" +SELECT resolution, year, month, unit_type, count, births, deaths +FROM public.statistical_history +WHERE resolution = 'year' +AND year < 2013 +AND unit_type = 'legal_unit'; + + +\echo "Check statistical unit history by year-month - births should be 1 for year-month 2010-1 and 2011-1" +SELECT resolution, year, month, unit_type, count, births, deaths +FROM public.statistical_history +WHERE resolution = 'year-month' +AND year < 2013 +AND unit_type = 'legal_unit'; + +\x + +ROLLBACK; \ No newline at end of file diff --git a/test/sql/42_history_legal_units_deaths.sql b/test/sql/42_history_legal_units_deaths.sql new file mode 100644 index 000000000..5486a314d --- /dev/null +++ b/test/sql/42_history_legal_units_deaths.sql @@ -0,0 +1,173 @@ +SET datestyle TO 'ISO, DMY'; + +BEGIN; + +\echo "Setting up Statbus to test enterprise grouping and primary" + +\echo "User selected the Activity Category Standard" +INSERT INTO settings(activity_category_standard_id,only_one_setting) +SELECT id, true FROM activity_category_standard WHERE code = 'nace_v2.1' +ON CONFLICT (only_one_setting) +DO UPDATE SET + activity_category_standard_id =(SELECT id FROM activity_category_standard WHERE code = 'nace_v2.1') + WHERE settings.id = EXCLUDED.id; +; +SELECT acs.code + FROM public.settings AS s + JOIN activity_category_standard AS acs + ON s.activity_category_standard_id = acs.id; + +\echo "User uploads the sample activity categories" +\copy public.activity_category_available_custom(path,name,description) FROM 'app/public/activity_category_norway.csv' WITH (FORMAT csv, DELIMITER ',', QUOTE '"', HEADER true); +SELECT count(*) FROM public.activity_category_available; + +\echo "User uploads the sample regions" +\copy public.region_upload(path, name) FROM 'app/public/norway-regions-2024.csv' WITH (FORMAT csv, DELIMITER ',', QUOTE '"', HEADER true); +SELECT count(*) FROM public.region; + +\echo "User uploads the sample legal forms" +\copy public.legal_form_custom_only(code,name) FROM 'app/public/legal_form_norway.csv' WITH (FORMAT csv, DELIMITER ',', QUOTE '"', HEADER true); +SELECT count(*) FROM public.legal_form_available; + +\echo "User uploads the sample sectors" +\copy public.sector_custom_only(path,name,description) FROM 'app/public/sector_norway.csv' WITH (FORMAT csv, DELIMITER ',', QUOTE '"', HEADER true); +SELECT count(*) FROM public.sector_available; + +SAVEPOINT before_loading_units; + +\echo "Test deaths at the end of the year" + + +SELECT + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.establishment) AS establishment_count, + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.legal_unit) AS legal_unit_count, + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.enterprise) AS enterprise_count; + +\echo "User uploads the legal units" +\copy public.import_legal_unit_era(valid_from,valid_to,tax_ident,name,birth_date,death_date,physical_address_part1,physical_postal_code,physical_postal_place,physical_region_code,physical_country_iso_2,postal_address_part1,postal_postal_code,postal_postal_place,postal_region_code,postal_country_iso_2,primary_activity_category_code,secondary_activity_category_code,sector_code,legal_form_code) FROM 'test/data/42_legal-units-deaths-end-of-year.csv' WITH (FORMAT csv, DELIMITER ',', QUOTE '"', HEADER true); + + +SELECT + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.establishment) AS establishment_count, + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.legal_unit) AS legal_unit_count, + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.enterprise) AS enterprise_count; + +\echo "Refreshing materialized views" +-- Exclude the refresh_time_ms as it will vary. +SELECT view_name FROM statistical_unit_refresh_now(); + +\echo "Check statistical units" +SELECT external_idents ->> 'tax_ident' as tax_ident, name, valid_after, valid_from, valid_to, birth_date, death_date +FROM public.statistical_unit +WHERE unit_type = 'legal_unit'; + +\echo "Check statistical unit history by year - deaths should be 1 for year 2011 and 2012" +SELECT resolution, year, month, unit_type, count, births, deaths +FROM public.statistical_history +WHERE resolution = 'year' +AND year < 2013 +AND unit_type = 'legal_unit'; + + +\echo "Check statistical unit history by year-month - deaths should be 1 for year-month 2011-12 and 2012-12" +SELECT resolution, year, month, unit_type, count, births, deaths +FROM public.statistical_history +WHERE resolution = 'year-month' +AND year < 2013 +AND unit_type = 'legal_unit'; + + +\x + + +ROLLBACK TO before_loading_units; + +\echo "Test deaths at the end of the first month" + +\x +SELECT + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.establishment) AS establishment_count, + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.legal_unit) AS legal_unit_count, + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.enterprise) AS enterprise_count; + +\echo "User uploads the legal units" +\copy public.import_legal_unit_era(valid_from,valid_to,tax_ident,name,birth_date,death_date,physical_address_part1,physical_postal_code,physical_postal_place,physical_region_code,physical_country_iso_2,postal_address_part1,postal_postal_code,postal_postal_place,postal_region_code,postal_country_iso_2,primary_activity_category_code,secondary_activity_category_code,sector_code,legal_form_code) FROM 'test/data/42_legal-units-deaths-end-of-first-month.csv' WITH (FORMAT csv, DELIMITER ',', QUOTE '"', HEADER true); + +SELECT + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.establishment) AS establishment_count, + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.legal_unit) AS legal_unit_count, + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.enterprise) AS enterprise_count; + +\echo "Refreshing materialized views" +-- Exclude the refresh_time_ms as it will vary. +SELECT view_name FROM statistical_unit_refresh_now(); + +\echo "Check statistical units" +SELECT external_idents ->> 'tax_ident' as tax_ident, name, valid_after, valid_from, valid_to, birth_date, death_date +FROM public.statistical_unit +WHERE unit_type = 'legal_unit'; + +\echo "Check statistical unit history by year - deaths should be 1 for year 2011 and 2012" +SELECT resolution, year, month, unit_type, count, births, deaths +FROM public.statistical_history +WHERE resolution = 'year' +AND year < 2013 +AND unit_type = 'legal_unit'; + +\echo "Check statistical unit history by year-month - deaths should be 1 for year-month 2011-1 and 2012-12" +SELECT resolution, year, month, unit_type, count, births, deaths +FROM public.statistical_history +WHERE resolution = 'year-month' +AND year < 2013 +AND unit_type = 'legal_unit'; + + +\x + + +ROLLBACK TO before_loading_units; + +\echo "Test deaths in the start of a month" + +\x +SELECT + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.establishment) AS establishment_count, + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.legal_unit) AS legal_unit_count, + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.enterprise) AS enterprise_count; + +\echo "User uploads the legal units" +\copy public.import_legal_unit_era(valid_from,valid_to,tax_ident,name,birth_date,death_date,physical_address_part1,physical_postal_code,physical_postal_place,physical_region_code,physical_country_iso_2,postal_address_part1,postal_postal_code,postal_postal_place,postal_region_code,postal_country_iso_2,primary_activity_category_code,secondary_activity_category_code,sector_code,legal_form_code) FROM 'test/data/42_legal-units-deaths-start-of-month.csv' WITH (FORMAT csv, DELIMITER ',', QUOTE '"', HEADER true); + +SELECT + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.establishment) AS establishment_count, + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.legal_unit) AS legal_unit_count, + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.enterprise) AS enterprise_count; + +\echo "Refreshing materialized views" +-- Exclude the refresh_time_ms as it will vary. +SELECT view_name FROM statistical_unit_refresh_now(); + +\echo "Check statistical units" +SELECT external_idents ->> 'tax_ident' as tax_ident, name, valid_after, valid_from, valid_to, birth_date, death_date +FROM public.statistical_unit +WHERE unit_type = 'legal_unit'; + +\echo "Check statistical unit history by year - deaths should be 1 for year 2011 and 2012" +SELECT resolution, year, month, unit_type, count, births, deaths +FROM public.statistical_history +WHERE resolution = 'year' +AND year < 2013 +AND unit_type = 'legal_unit'; + +\echo "Check statistical unit history by year-month - deaths should be 1 for year-month 2011-1 and 2012-1" +SELECT resolution, year, month, unit_type, count, births, deaths +FROM public.statistical_history +WHERE resolution = 'year-month' +AND year < 2013 +AND unit_type = 'legal_unit'; + + + +\x + +ROLLBACK; From 8b33909779f16996b04abfef3474aec6ead1986c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B8rgen=20H=2E=20Fjeld?= Date: Wed, 23 Oct 2024 15:18:02 +0200 Subject: [PATCH 2/2] db: Fix history death/birth/count and track name/addresss changes There was a logical bug in the history counting, remedied by analysing the timeline within each range, and picking the *last* relevant entry and using that as a starting point for analysis. Furthermore any change must be analysed with respect to the previous range, so the historical periods are extended with prev_stop as the last day of the preceeding range. Also track changes to name/address across ranges. Tests adjusted as required. The example data was changed to add the death date the same day it is valid from, as it was not known in advance about the death. --- dbseed/create-db-structure.sql | 494 ++++++----- ..._legal-units-deaths-end-of-first-month.csv | 4 +- .../42_legal-units-deaths-end-of-year.csv | 7 +- .../42_legal-units-deaths-start-of-month.csv | 4 +- .../03_load_and_verify_history_functions.out | 780 +++++++++--------- .../05_modify_enterprise_connections.out | 24 +- ..._history_legal_units_changes_over_time.out | 392 +++++++++ .../41_history_legal_units_births.out | 402 +++++++++ .../42_history_legal_units_deaths.out | 396 +++++++++ test/sql/41_history_legal_units_births.sql | 14 +- 10 files changed, 1896 insertions(+), 621 deletions(-) diff --git a/dbseed/create-db-structure.sql b/dbseed/create-db-structure.sql index 15edca9d8..94d638fa9 100644 --- a/dbseed/create-db-structure.sql +++ b/dbseed/create-db-structure.sql @@ -5261,28 +5261,30 @@ WITH year_range AS ( FROM public.statistical_unit ) SELECT 'year'::public.history_resolution AS resolution - , EXTRACT(YEAR FROM time_start)::INT AS year + , EXTRACT(YEAR FROM curr_start)::INT AS year , NULL::INTEGER AS month - , series.time_start::DATE - , (series.time_start + interval '1 year' - interval '1 day')::DATE AS time_stop + , (series.curr_start - interval '1 day')::DATE AS prev_stop + , series.curr_start::DATE + , (series.curr_start + interval '1 year' - interval '1 day')::DATE AS curr_stop FROM year_range, LATERAL generate_series( date_trunc('year', year_range.start_year)::DATE, date_trunc('year', year_range.stop_year)::DATE, interval '1 year' -) AS series(time_start) +) AS series(curr_start) UNION ALL SELECT 'year-month'::public.history_resolution AS resolution - , EXTRACT(YEAR FROM time_start)::INT AS year - , EXTRACT(MONTH FROM time_start)::INT AS month - , series.time_start::DATE - , (series.time_start + interval '1 month' - interval '1 day')::DATE AS time_stop + , EXTRACT(YEAR FROM curr_start)::INT AS year + , EXTRACT(MONTH FROM curr_start)::INT AS month + , (series.curr_start - interval '1 day')::DATE AS prev_stop + , series.curr_start::DATE + , (series.curr_start + interval '1 month' - interval '1 day')::DATE AS curr_stop FROM year_range, LATERAL generate_series( date_trunc('month', year_range.start_year)::DATE, date_trunc('month', year_range.stop_year)::DATE, interval '1 month' -) AS series(time_start) +) AS series(curr_start) ; @@ -5292,37 +5294,38 @@ CREATE VIEW public.statistical_history_def AS WITH year_with_unit_basis AS ( SELECT range.resolution AS resolution , range.year AS year - , COALESCE(su_start.unit_type,su_stop.unit_type) AS unit_type + , su_curr.unit_type AS unit_type -- - , COALESCE(su_start.unit_id, su_stop.unit_id) AS unit_id - , su_start.unit_id IS NOT NULL AND su_stop.unit_id IS NOT NULL AS track_changes + , su_curr.unit_id AS unit_id + , su_prev.unit_id IS NOT NULL AND su_curr.unit_id IS NOT NULL AS track_changes -- - , COALESCE(su_stop.birth_date, su_start.birth_date) AS birth_date - , COALESCE(su_stop.death_date, su_start.death_date) AS death_date + , su_curr.birth_date AS birth_date + , su_curr.death_date AS death_date -- - , COALESCE(range.time_start <= COALESCE(su_stop.birth_date, su_start.birth_date),false) AS born - , COALESCE(COALESCE(su_stop.death_date, su_start.death_date) <= range.time_stop ,false) AS died + , COALESCE(range.curr_start <= su_curr.birth_date AND su_curr.birth_date <= range.curr_stop,false) AS born + , COALESCE(range.curr_start <= su_curr.death_date AND su_curr.death_date <= range.curr_stop,false) AS died -- - , su_start.primary_activity_category_path AS start_primary_activity_category_path - , su_start.secondary_activity_category_path AS start_secondary_activity_category_path - , su_start.sector_path AS start_sector_path - , su_start.legal_form_id AS start_legal_form_id - , su_start.physical_region_path AS start_physical_region_path - , su_start.physical_country_id AS start_physical_country_id + , su_prev.name AS prev_name + , su_prev.primary_activity_category_path AS prev_primary_activity_category_path + , su_prev.secondary_activity_category_path AS prev_secondary_activity_category_path + , su_prev.sector_path AS prev_sector_path + , su_prev.legal_form_id AS prev_legal_form_id + , su_prev.physical_region_path AS prev_physical_region_path + , su_prev.physical_country_id AS prev_physical_country_id + , su_prev.physical_address_part1 AS prev_physical_address_part1 + , su_prev.physical_address_part2 AS prev_physical_address_part2 + , su_prev.physical_address_part3 AS prev_physical_address_part3 -- - , su_stop.primary_activity_category_path AS stop_primary_activity_category_path - , su_stop.secondary_activity_category_path AS stop_secondary_activity_category_path - , su_stop.sector_path AS stop_sector_path - , su_stop.legal_form_id AS stop_legal_form_id - , su_stop.physical_region_path AS stop_physical_region_path - , su_stop.physical_country_id AS stop_physical_country_id - -- - , COALESCE(su_stop.primary_activity_category_path , su_start.primary_activity_category_path) AS primary_activity_category_path - , COALESCE(su_stop.secondary_activity_category_path, su_start.secondary_activity_category_path) AS secondary_activity_category_path - , COALESCE(su_stop.sector_path , su_start.sector_path) AS sector_path - , COALESCE(su_stop.legal_form_id , su_start.legal_form_id) AS legal_form_id - , COALESCE(su_stop.physical_region_path , su_start.physical_region_path) AS physical_region_path - , COALESCE(su_stop.physical_country_id , su_start.physical_country_id) AS physical_country_id + , su_curr.name AS curr_name + , su_curr.primary_activity_category_path AS curr_primary_activity_category_path + , su_curr.secondary_activity_category_path AS curr_secondary_activity_category_path + , su_curr.sector_path AS curr_sector_path + , su_curr.legal_form_id AS curr_legal_form_id + , su_curr.physical_region_path AS curr_physical_region_path + , su_curr.physical_country_id AS curr_physical_country_id + , su_curr.physical_address_part1 AS curr_physical_address_part1 + , su_curr.physical_address_part2 AS curr_physical_address_part2 + , su_curr.physical_address_part3 AS curr_physical_address_part3 -- -- Notice that `stats` is the stats of this particular unit as recorded, -- while stats_summary is the aggregated stats of multiple contained units. @@ -5330,31 +5333,48 @@ WITH year_with_unit_basis AS ( -- changes, and not at the summaries, as I don't see how it makes sense -- to track changes in statistical summaries, but rather in the reported -- statistical variables, and then possibly summarise the changes. - , su_start.stats AS start_stats - , su_stop.stats AS stop_stats + , su_prev.stats AS prev_stats + , su_curr.stats AS curr_stats -- - , COALESCE(su_stop.stats , su_start.stats) AS stats - , COALESCE(su_stop.stats_summary , su_start.stats_summary) AS stats_summary + , su_curr.stats AS stats + , su_curr.stats_summary AS stats_summary -- FROM public.statistical_history_periods AS range - LEFT JOIN public.statistical_unit AS su_start - ON su_start.valid_from <= range.time_start AND range.time_start <= su_start.valid_to - LEFT JOIN public.statistical_unit AS su_stop - ON su_stop.valid_from <= range.time_stop AND range.time_stop <= su_stop.valid_to - WHERE range.resolution = 'year' AND - ( su_start.unit_type IS NULL - OR su_stop.unit_type IS NULL - OR su_start.unit_type = su_stop.unit_type AND su_start.unit_id = su_stop.unit_id - ) + JOIN LATERAL ( + -- Within a range find the last row of each timeline + SELECT * + FROM ( + SELECT su_range.* + , ROW_NUMBER() OVER (PARTITION BY su_range.unit_type, su_range.unit_id ORDER BY su_range.valid_from DESC) = 1 AS last_in_range + FROM public.statistical_unit AS su_range + WHERE daterange(su_range.valid_from, su_range.valid_to, '[]') && daterange(range.curr_start,range.curr_stop,'[]') + -- Entries already dead entries are not relevant. + AND (su_range.death_date IS NULL OR range.curr_start <= su_range.death_date) + -- Entries not yet born are not relevant. + AND (su_range.birth_date IS NULL OR su_range.birth_date <= range.curr_stop) + ) AS range_units + WHERE last_in_range + ) AS su_curr ON true + LEFT JOIN public.statistical_unit AS su_prev + -- There may be a previous entry to compare with. + ON su_prev.valid_from <= range.prev_stop AND range.prev_stop <= su_prev.valid_to + AND su_prev.unit_type = su_curr.unit_type AND su_prev.unit_id = su_curr.unit_id + WHERE range.resolution = 'year' ), year_with_unit_derived AS ( SELECT basis.* -- - , track_changes AND NOT born AND not died AND start_primary_activity_category_path IS DISTINCT FROM stop_primary_activity_category_path AS primary_activity_category_changed - , track_changes AND NOT born AND not died AND start_secondary_activity_category_path IS DISTINCT FROM stop_secondary_activity_category_path AS secondary_activity_category_changed - , track_changes AND NOT born AND not died AND start_sector_path IS DISTINCT FROM stop_sector_path AS sector_changed - , track_changes AND NOT born AND not died AND start_legal_form_id IS DISTINCT FROM stop_legal_form_id AS legal_form_changed - , track_changes AND NOT born AND not died AND start_physical_region_path IS DISTINCT FROM stop_physical_region_path AS physical_region_changed - , track_changes AND NOT born AND not died AND start_physical_country_id IS DISTINCT FROM stop_physical_country_id AS physical_country_changed + , track_changes AND NOT born AND not died AND prev_name IS DISTINCT FROM curr_name AS name_changed + , track_changes AND NOT born AND not died AND prev_primary_activity_category_path IS DISTINCT FROM curr_primary_activity_category_path AS primary_activity_category_changed + , track_changes AND NOT born AND not died AND prev_secondary_activity_category_path IS DISTINCT FROM curr_secondary_activity_category_path AS secondary_activity_category_changed + , track_changes AND NOT born AND not died AND prev_sector_path IS DISTINCT FROM curr_sector_path AS sector_changed + , track_changes AND NOT born AND not died AND prev_legal_form_id IS DISTINCT FROM curr_legal_form_id AS legal_form_changed + , track_changes AND NOT born AND not died AND prev_physical_region_path IS DISTINCT FROM curr_physical_region_path AS physical_region_changed + , track_changes AND NOT born AND not died AND prev_physical_country_id IS DISTINCT FROM curr_physical_country_id AS physical_country_changed + , track_changes AND NOT born AND not died AND ( + prev_physical_address_part1 IS DISTINCT FROM curr_physical_address_part1 + OR prev_physical_address_part2 IS DISTINCT FROM curr_physical_address_part2 + OR prev_physical_address_part3 IS DISTINCT FROM curr_physical_address_part3 + ) AS physical_address_changed -- -- TODO: Track the change in `stats` and put that into `stats_change` using `public.stats_change`. --, CASE WHEN track_changes THEN public.stats_change(start_stats,stop_stats) ELSE NULL END AS stats_change @@ -5364,63 +5384,81 @@ WITH year_with_unit_basis AS ( SELECT range.resolution AS resolution , range.year AS year , range.month AS month - , COALESCE(su_start.unit_type,su_stop.unit_type) AS unit_type - -- - , COALESCE(su_start.unit_id, su_stop.unit_id) AS unit_id - , su_start.unit_id IS NOT NULL AND su_stop.unit_id IS NOT NULL AS track_changes + , su_curr.unit_type AS unit_type -- - , COALESCE(su_stop.birth_date, su_start.birth_date) AS birth_date - , COALESCE(su_stop.death_date, su_start.death_date) AS death_date + , su_curr.unit_id AS unit_id + , su_prev.unit_id IS NOT NULL AND su_curr.unit_id IS NOT NULL AS track_changes -- - , COALESCE(range.time_start <= COALESCE(su_stop.birth_date, su_start.birth_date),false) AS born - , COALESCE(COALESCE(su_stop.death_date, su_start.death_date) <= range.time_stop ,false) AS died + , su_curr.birth_date AS birth_date + , su_curr.death_date AS death_date -- - , su_start.primary_activity_category_path AS start_primary_activity_category_path - , su_start.secondary_activity_category_path AS start_secondary_activity_category_path - , su_start.sector_path AS start_sector_path - , su_start.legal_form_id AS start_legal_form_id - , su_start.physical_region_path AS start_physical_region_path - , su_start.physical_country_id AS start_physical_country_id + , COALESCE(range.curr_start <= su_curr.birth_date AND su_curr.birth_date <= range.curr_stop,false) AS born + , COALESCE(range.curr_start <= su_curr.death_date AND su_curr.death_date <= range.curr_stop,false) AS died -- - , su_stop.primary_activity_category_path AS stop_primary_activity_category_path - , su_stop.secondary_activity_category_path AS stop_secondary_activity_category_path - , su_stop.sector_path AS stop_sector_path - , su_stop.legal_form_id AS stop_legal_form_id - , su_stop.physical_region_path AS stop_physical_region_path - , su_stop.physical_country_id AS stop_physical_country_id + , su_prev.name AS prev_name + , su_prev.primary_activity_category_path AS prev_primary_activity_category_path + , su_prev.secondary_activity_category_path AS prev_secondary_activity_category_path + , su_prev.sector_path AS prev_sector_path + , su_prev.legal_form_id AS prev_legal_form_id + , su_prev.physical_region_path AS prev_physical_region_path + , su_prev.physical_country_id AS prev_physical_country_id + , su_prev.physical_address_part1 AS prev_physical_address_part1 + , su_prev.physical_address_part2 AS prev_physical_address_part2 + , su_prev.physical_address_part3 AS prev_physical_address_part3 -- - , COALESCE(su_stop.primary_activity_category_path , su_start.primary_activity_category_path) AS primary_activity_category_path - , COALESCE(su_stop.secondary_activity_category_path, su_start.secondary_activity_category_path) AS secondary_activity_category_path - , COALESCE(su_stop.sector_path , su_start.sector_path) AS sector_path - , COALESCE(su_stop.legal_form_id , su_start.legal_form_id) AS legal_form_id - , COALESCE(su_stop.physical_region_path , su_start.physical_region_path) AS physical_region_path - , COALESCE(su_stop.physical_country_id , su_start.physical_country_id) AS physical_country_id + , su_curr.name AS curr_name + , su_curr.primary_activity_category_path AS curr_primary_activity_category_path + , su_curr.secondary_activity_category_path AS curr_secondary_activity_category_path + , su_curr.sector_path AS curr_sector_path + , su_curr.legal_form_id AS curr_legal_form_id + , su_curr.physical_region_path AS curr_physical_region_path + , su_curr.physical_country_id AS curr_physical_country_id + , su_curr.physical_address_part1 AS curr_physical_address_part1 + , su_curr.physical_address_part2 AS curr_physical_address_part2 + , su_curr.physical_address_part3 AS curr_physical_address_part3 -- - , su_start.stats AS start_stats - , su_stop.stats AS stop_stats + , su_prev.stats AS start_stats + , su_curr.stats AS stop_stats -- - , COALESCE(su_stop.stats , su_start.stats) AS stats - , COALESCE(su_stop.stats_summary , su_start.stats_summary) AS stats_summary + , su_curr.stats AS stats + , su_curr.stats_summary AS stats_summary -- FROM public.statistical_history_periods AS range - LEFT JOIN public.statistical_unit AS su_start - ON su_start.valid_from <= range.time_start AND range.time_start <= su_start.valid_to - LEFT JOIN public.statistical_unit AS su_stop - ON su_stop.valid_from <= range.time_stop AND range.time_stop <= su_stop.valid_to - WHERE range.resolution = 'year-month' AND - ( su_start.unit_type IS NULL - OR su_stop.unit_type IS NULL - OR su_start.unit_type = su_stop.unit_type AND su_start.unit_id = su_stop.unit_id - ) + JOIN LATERAL ( + -- Within a range find the last row of each timeline + SELECT * + FROM ( + SELECT su_range.* + , ROW_NUMBER() OVER (PARTITION BY su_range.unit_type, su_range.unit_id ORDER BY su_range.valid_from DESC) = 1 AS last_in_range + FROM public.statistical_unit AS su_range + WHERE daterange(su_range.valid_from, su_range.valid_to, '[]') && daterange(range.curr_start,range.curr_stop,'[]') + -- Entries already dead entries are not relevant. + AND (su_range.death_date IS NULL OR range.curr_start <= su_range.death_date) + -- Entries not yet born are not relevant. + AND (su_range.birth_date IS NULL OR su_range.birth_date <= range.curr_stop) + ) AS range_units + WHERE last_in_range + ) AS su_curr ON true + LEFT JOIN public.statistical_unit AS su_prev + -- There may be a previous entry to compare with. + ON su_prev.valid_from <= range.prev_stop AND range.prev_stop <= su_prev.valid_to + AND su_prev.unit_type = su_curr.unit_type AND su_prev.unit_id = su_curr.unit_id + WHERE range.resolution = 'year-month' ), year_and_month_with_unit_derived AS ( SELECT basis.* -- - , track_changes AND NOT born AND not died AND start_primary_activity_category_path IS DISTINCT FROM stop_primary_activity_category_path AS primary_activity_category_changed - , track_changes AND NOT born AND not died AND start_secondary_activity_category_path IS DISTINCT FROM stop_secondary_activity_category_path AS secondary_activity_category_changed - , track_changes AND NOT born AND not died AND start_sector_path IS DISTINCT FROM stop_sector_path AS sector_changed - , track_changes AND NOT born AND not died AND start_legal_form_id IS DISTINCT FROM stop_legal_form_id AS legal_form_changed - , track_changes AND NOT born AND not died AND start_physical_region_path IS DISTINCT FROM stop_physical_region_path AS physical_region_changed - , track_changes AND NOT born AND not died AND start_physical_country_id IS DISTINCT FROM stop_physical_country_id AS physical_country_changed + , track_changes AND NOT born AND not died AND prev_name IS DISTINCT FROM curr_name AS name_changed + , track_changes AND NOT born AND not died AND prev_primary_activity_category_path IS DISTINCT FROM curr_primary_activity_category_path AS primary_activity_category_changed + , track_changes AND NOT born AND not died AND prev_secondary_activity_category_path IS DISTINCT FROM curr_secondary_activity_category_path AS secondary_activity_category_changed + , track_changes AND NOT born AND not died AND prev_sector_path IS DISTINCT FROM curr_sector_path AS sector_changed + , track_changes AND NOT born AND not died AND prev_legal_form_id IS DISTINCT FROM curr_legal_form_id AS legal_form_changed + , track_changes AND NOT born AND not died AND prev_physical_region_path IS DISTINCT FROM curr_physical_region_path AS physical_region_changed + , track_changes AND NOT born AND not died AND prev_physical_country_id IS DISTINCT FROM curr_physical_country_id AS physical_country_changed + , track_changes AND NOT born AND not died AND ( + prev_physical_address_part1 IS DISTINCT FROM curr_physical_address_part1 + OR prev_physical_address_part2 IS DISTINCT FROM curr_physical_address_part2 + OR prev_physical_address_part3 IS DISTINCT FROM curr_physical_address_part3 + ) AS physical_address_changed -- -- TODO: Track the change in `stats` and put that into `stats_change` using `public.stats_change`. --, CASE WHEN track_changes THEN public.stats_change(start_stats,stop_stats) ELSE NULL END AS stats_change @@ -5432,19 +5470,22 @@ WITH year_with_unit_basis AS ( , NULL::INTEGER AS month , source.unit_type AS unit_type -- - , COUNT(source.*) AS count + , COUNT(source.*) FILTER (WHERE NOT source.died) AS count -- , COUNT(source.*) FILTER (WHERE source.born) AS births , COUNT(source.*) FILTER (WHERE source.died) AS deaths -- + , COUNT(source.*) FILTER (WHERE source.name_changed) AS name_change_count , COUNT(source.*) FILTER (WHERE source.primary_activity_category_changed) AS primary_activity_category_change_count , COUNT(source.*) FILTER (WHERE source.secondary_activity_category_changed) AS secondary_activity_category_change_count , COUNT(source.*) FILTER (WHERE source.sector_changed) AS sector_change_count , COUNT(source.*) FILTER (WHERE source.legal_form_changed) AS legal_form_change_count , COUNT(source.*) FILTER (WHERE source.physical_region_changed) AS physical_region_change_count , COUNT(source.*) FILTER (WHERE source.physical_country_changed) AS physical_country_change_count + , COUNT(source.*) FILTER (WHERE source.physical_address_changed) AS physical_address_change_count -- , public.jsonb_stats_summary_merge_agg(source.stats_summary) AS stats_summary + -- FROM year_with_unit_derived AS source GROUP BY resolution, year, unit_type ), year_and_month_with_unit AS ( @@ -5453,19 +5494,22 @@ WITH year_with_unit_basis AS ( , source.month AS month , source.unit_type AS unit_type -- - , COUNT(source.*) AS count + , COUNT(source.*) FILTER (WHERE NOT source.died) AS count -- , COUNT(source.*) FILTER (WHERE source.born) AS births , COUNT(source.*) FILTER (WHERE source.died) AS deaths -- + , COUNT(source.*) FILTER (WHERE source.name_changed) AS name_change_count , COUNT(source.*) FILTER (WHERE source.primary_activity_category_changed) AS primary_activity_category_change_count , COUNT(source.*) FILTER (WHERE source.secondary_activity_category_changed) AS secondary_activity_category_change_count , COUNT(source.*) FILTER (WHERE source.sector_changed) AS sector_change_count , COUNT(source.*) FILTER (WHERE source.legal_form_changed) AS legal_form_change_count , COUNT(source.*) FILTER (WHERE source.physical_region_changed) AS physical_region_change_count , COUNT(source.*) FILTER (WHERE source.physical_country_changed) AS physical_country_change_count + , COUNT(source.*) FILTER (WHERE source.physical_address_changed) AS physical_address_change_count -- , public.jsonb_stats_summary_merge_agg(source.stats_summary) AS stats_summary + -- FROM year_and_month_with_unit_derived AS source GROUP BY resolution, year, month, unit_type ) @@ -5522,63 +5566,81 @@ WITH year_with_unit_basis AS ( SELECT range.resolution AS resolution , range.year AS year , NULL::INTEGER AS month - , COALESCE(su_start.unit_type, su_stop.unit_type) AS unit_type - -- - , COALESCE(su_start.unit_id, su_stop.unit_id) AS unit_id - , su_start.unit_id IS NOT NULL AND su_stop.unit_id IS NOT NULL AS track_changes + , su_curr.unit_type AS unit_type -- - , COALESCE(su_stop.birth_date, su_start.birth_date) AS birth_date - , COALESCE(su_stop.death_date, su_start.death_date) AS death_date + , su_curr.unit_id AS unit_id + , su_prev.unit_id IS NOT NULL AND su_curr.unit_id IS NOT NULL AS track_changes -- - , COALESCE(range.time_start <= COALESCE(su_stop.birth_date, su_start.birth_date), false) AS born - , COALESCE(COALESCE(su_stop.death_date, su_start.death_date) <= range.time_stop, false) AS died + , su_curr.birth_date AS birth_date + , su_curr.death_date AS death_date -- - , su_start.primary_activity_category_path AS start_primary_activity_category_path - , su_start.secondary_activity_category_path AS start_secondary_activity_category_path - , su_start.sector_path AS start_sector_path - , su_start.legal_form_id AS start_legal_form_id - , su_start.physical_region_path AS start_physical_region_path - , su_start.physical_country_id AS start_physical_country_id + , COALESCE(range.curr_start <= su_curr.birth_date AND su_curr.birth_date <= range.curr_stop,false) AS born + , COALESCE(range.curr_start <= su_curr.death_date AND su_curr.death_date <= range.curr_stop,false) AS died -- - , su_stop.primary_activity_category_path AS stop_primary_activity_category_path - , su_stop.secondary_activity_category_path AS stop_secondary_activity_category_path - , su_stop.sector_path AS stop_sector_path - , su_stop.legal_form_id AS stop_legal_form_id - , su_stop.physical_region_path AS stop_physical_region_path - , su_stop.physical_country_id AS stop_physical_country_id + , su_prev.name AS prev_name + , su_prev.primary_activity_category_path AS prev_primary_activity_category_path + , su_prev.secondary_activity_category_path AS prev_secondary_activity_category_path + , su_prev.sector_path AS prev_sector_path + , su_prev.legal_form_id AS prev_legal_form_id + , su_prev.physical_region_path AS prev_physical_region_path + , su_prev.physical_country_id AS prev_physical_country_id + , su_prev.physical_address_part1 AS prev_physical_address_part1 + , su_prev.physical_address_part2 AS prev_physical_address_part2 + , su_prev.physical_address_part3 AS prev_physical_address_part3 -- - , COALESCE(su_stop.primary_activity_category_path , su_start.primary_activity_category_path) AS primary_activity_category_path - , COALESCE(su_stop.secondary_activity_category_path, su_start.secondary_activity_category_path) AS secondary_activity_category_path - , COALESCE(su_stop.sector_path , su_start.sector_path) AS sector_path - , COALESCE(su_stop.legal_form_id , su_start.legal_form_id) AS legal_form_id - , COALESCE(su_stop.physical_region_path , su_start.physical_region_path) AS physical_region_path - , COALESCE(su_stop.physical_country_id , su_start.physical_country_id) AS physical_country_id + , su_curr.name AS curr_name + , su_curr.primary_activity_category_path AS curr_primary_activity_category_path + , su_curr.secondary_activity_category_path AS curr_secondary_activity_category_path + , su_curr.sector_path AS curr_sector_path + , su_curr.legal_form_id AS curr_legal_form_id + , su_curr.physical_region_path AS curr_physical_region_path + , su_curr.physical_country_id AS curr_physical_country_id + , su_curr.physical_address_part1 AS curr_physical_address_part1 + , su_curr.physical_address_part2 AS curr_physical_address_part2 + , su_curr.physical_address_part3 AS curr_physical_address_part3 -- - , su_start.stats AS start_stats - , su_stop.stats AS stop_stats + , su_prev.stats AS prev_stats + , su_curr.stats AS curr_stats -- - , COALESCE(su_stop.stats , su_start.stats) AS stats - , COALESCE(su_stop.stats_summary , su_start.stats_summary) AS stats_summary + , su_curr.stats AS stats + , su_curr.stats_summary AS stats_summary -- FROM public.statistical_history_periods AS range - LEFT JOIN public.statistical_unit AS su_start - ON su_start.valid_from <= range.time_start AND range.time_start <= su_start.valid_to - LEFT JOIN public.statistical_unit AS su_stop - ON su_stop.valid_from <= range.time_stop AND range.time_stop <= su_stop.valid_to - WHERE range.resolution = 'year' AND - ( su_start.unit_type IS NULL - OR su_stop.unit_type IS NULL - OR su_start.unit_type = su_stop.unit_type AND su_start.unit_id = su_stop.unit_id - ) + JOIN LATERAL ( + -- Within a range find the last row of each timeline + SELECT * + FROM ( + SELECT su_range.* + , ROW_NUMBER() OVER (PARTITION BY su_range.unit_type, su_range.unit_id ORDER BY su_range.valid_from DESC) = 1 AS last_in_range + FROM public.statistical_unit AS su_range + WHERE daterange(su_range.valid_from, su_range.valid_to, '[]') && daterange(range.curr_start,range.curr_stop,'[]') + -- Entries already dead entries are not relevant. + AND (su_range.death_date IS NULL OR range.curr_start <= su_range.death_date) + -- Entries not yet born are not relevant. + AND (su_range.birth_date IS NULL OR su_range.birth_date <= range.curr_stop) + ) AS range_units + WHERE last_in_range + ) AS su_curr ON true + LEFT JOIN public.statistical_unit AS su_prev + -- There may be a previous entry to compare with. + ON su_prev.valid_from <= range.prev_stop AND range.prev_stop <= su_prev.valid_to + AND su_prev.unit_type = su_curr.unit_type AND su_prev.unit_id = su_curr.unit_id + WHERE range.resolution = 'year' ), year_with_unit_derived AS ( SELECT basis.* -- - , track_changes AND NOT born AND not died AND start_primary_activity_category_path IS DISTINCT FROM stop_primary_activity_category_path AS primary_activity_category_changed - , track_changes AND NOT born AND not died AND start_secondary_activity_category_path IS DISTINCT FROM stop_secondary_activity_category_path AS secondary_activity_category_changed - , track_changes AND NOT born AND not died AND start_sector_path IS DISTINCT FROM stop_sector_path AS sector_changed - , track_changes AND NOT born AND not died AND start_legal_form_id IS DISTINCT FROM stop_legal_form_id AS legal_form_changed - , track_changes AND NOT born AND not died AND start_physical_region_path IS DISTINCT FROM stop_physical_region_path AS physical_region_changed - , track_changes AND NOT born AND not died AND start_physical_country_id IS DISTINCT FROM stop_physical_country_id AS physical_country_changed + , track_changes AND NOT born AND not died AND prev_name IS DISTINCT FROM curr_name AS name_changed + , track_changes AND NOT born AND not died AND prev_primary_activity_category_path IS DISTINCT FROM curr_primary_activity_category_path AS primary_activity_category_changed + , track_changes AND NOT born AND not died AND prev_secondary_activity_category_path IS DISTINCT FROM curr_secondary_activity_category_path AS secondary_activity_category_changed + , track_changes AND NOT born AND not died AND prev_sector_path IS DISTINCT FROM curr_sector_path AS sector_changed + , track_changes AND NOT born AND not died AND prev_legal_form_id IS DISTINCT FROM curr_legal_form_id AS legal_form_changed + , track_changes AND NOT born AND not died AND prev_physical_region_path IS DISTINCT FROM curr_physical_region_path AS physical_region_changed + , track_changes AND NOT born AND not died AND prev_physical_country_id IS DISTINCT FROM curr_physical_country_id AS physical_country_changed + , track_changes AND NOT born AND not died AND ( + prev_physical_address_part1 IS DISTINCT FROM curr_physical_address_part1 + OR prev_physical_address_part2 IS DISTINCT FROM curr_physical_address_part2 + OR prev_physical_address_part3 IS DISTINCT FROM curr_physical_address_part3 + ) AS physical_address_changed -- -- TODO: Track the change in `stats` and put that into `stats_change` using `public.stats_change`. --, CASE WHEN track_changes THEN public.stats_change(start_stats,stop_stats) ELSE NULL END AS stats_change @@ -5588,63 +5650,81 @@ WITH year_with_unit_basis AS ( SELECT range.resolution AS resolution , range.year AS year , range.month AS month - , COALESCE(su_start.unit_type, su_stop.unit_type) AS unit_type + , COALESCE(su_prev.unit_type, su_curr.unit_type) AS unit_type -- - , COALESCE(su_start.unit_id, su_stop.unit_id) AS unit_id - , su_start.unit_id IS NOT NULL AND su_stop.unit_id IS NOT NULL AS track_changes + , su_curr.unit_id AS unit_id + , su_prev.unit_id IS NOT NULL AND su_curr.unit_id IS NOT NULL AS track_changes -- - , COALESCE(su_stop.birth_date, su_start.birth_date) AS birth_date - , COALESCE(su_stop.death_date, su_start.death_date) AS death_date + , su_curr.birth_date AS birth_date + , su_curr.death_date AS death_date -- - , COALESCE(range.time_start <= COALESCE(su_stop.birth_date, su_start.birth_date), false) AS born - , COALESCE(COALESCE(su_stop.death_date, su_start.death_date) <= range.time_stop, false) AS died + , COALESCE(range.curr_start <= su_curr.birth_date AND su_curr.birth_date <= range.curr_stop,false) AS born + , COALESCE(range.curr_start <= su_curr.death_date AND su_curr.death_date <= range.curr_stop,false) AS died -- - , su_start.primary_activity_category_path AS start_primary_activity_category_path - , su_start.secondary_activity_category_path AS start_secondary_activity_category_path - , su_start.sector_path AS start_sector_path - , su_start.legal_form_id AS start_legal_form_id - , su_start.physical_region_path AS start_physical_region_path - , su_start.physical_country_id AS start_physical_country_id + , su_prev.name AS prev_name + , su_prev.primary_activity_category_path AS prev_primary_activity_category_path + , su_prev.secondary_activity_category_path AS prev_secondary_activity_category_path + , su_prev.sector_path AS prev_sector_path + , su_prev.legal_form_id AS prev_legal_form_id + , su_prev.physical_region_path AS prev_physical_region_path + , su_prev.physical_country_id AS prev_physical_country_id + , su_prev.physical_address_part1 AS prev_physical_address_part1 + , su_prev.physical_address_part2 AS prev_physical_address_part2 + , su_prev.physical_address_part3 AS prev_physical_address_part3 -- - , su_stop.primary_activity_category_path AS stop_primary_activity_category_path - , su_stop.secondary_activity_category_path AS stop_secondary_activity_category_path - , su_stop.sector_path AS stop_sector_path - , su_stop.legal_form_id AS stop_legal_form_id - , su_stop.physical_region_path AS stop_physical_region_path - , su_stop.physical_country_id AS stop_physical_country_id + , su_curr.name AS curr_name + , su_curr.primary_activity_category_path AS curr_primary_activity_category_path + , su_curr.secondary_activity_category_path AS curr_secondary_activity_category_path + , su_curr.sector_path AS curr_sector_path + , su_curr.legal_form_id AS curr_legal_form_id + , su_curr.physical_region_path AS curr_physical_region_path + , su_curr.physical_country_id AS curr_physical_country_id + , su_curr.physical_address_part1 AS curr_physical_address_part1 + , su_curr.physical_address_part2 AS curr_physical_address_part2 + , su_curr.physical_address_part3 AS curr_physical_address_part3 -- - , COALESCE(su_stop.primary_activity_category_path , su_start.primary_activity_category_path) AS primary_activity_category_path - , COALESCE(su_stop.secondary_activity_category_path, su_start.secondary_activity_category_path) AS secondary_activity_category_path - , COALESCE(su_stop.sector_path , su_start.sector_path) AS sector_path - , COALESCE(su_stop.legal_form_id , su_start.legal_form_id) AS legal_form_id - , COALESCE(su_stop.physical_region_path , su_start.physical_region_path) AS physical_region_path - , COALESCE(su_stop.physical_country_id , su_start.physical_country_id) AS physical_country_id + , su_prev.stats AS prev_stats + , su_curr.stats AS curr_stats -- - , su_start.stats AS start_stats - , su_stop.stats AS stop_stats - -- - , COALESCE(su_stop.stats , su_start.stats) AS stats - , COALESCE(su_stop.stats_summary , su_start.stats_summary) AS stats_summary + , su_curr.stats AS stats + , su_curr.stats_summary AS stats_summary -- FROM public.statistical_history_periods AS range - LEFT JOIN public.statistical_unit AS su_start - ON su_start.valid_from <= range.time_start AND range.time_start <= su_start.valid_to - LEFT JOIN public.statistical_unit AS su_stop - ON su_stop.valid_from <= range.time_stop AND range.time_stop <= su_stop.valid_to - WHERE range.resolution = 'year-month' AND - ( su_start.unit_type IS NULL - OR su_stop.unit_type IS NULL - OR su_start.unit_type = su_stop.unit_type AND su_start.unit_id = su_stop.unit_id - ) + JOIN LATERAL ( + -- Within a range find the last row of each timeline + SELECT * + FROM ( + SELECT su_range.* + , ROW_NUMBER() OVER (PARTITION BY su_range.unit_type, su_range.unit_id ORDER BY su_range.valid_from DESC) = 1 AS last_in_range + FROM public.statistical_unit AS su_range + WHERE daterange(su_range.valid_from, su_range.valid_to, '[]') && daterange(range.curr_start,range.curr_stop,'[]') + -- Entries already dead entries are not relevant. + AND (su_range.death_date IS NULL OR range.curr_start <= su_range.death_date) + -- Entries not yet born are not relevant. + AND (su_range.birth_date IS NULL OR su_range.birth_date <= range.curr_stop) + ) AS range_units + WHERE last_in_range + ) AS su_curr ON true + LEFT JOIN public.statistical_unit AS su_prev + -- There may be a previous entry to compare with. + ON su_prev.valid_from <= range.prev_stop AND range.prev_stop <= su_prev.valid_to + AND su_prev.unit_type = su_curr.unit_type AND su_prev.unit_id = su_curr.unit_id + WHERE range.resolution = 'year-month' ), year_and_month_with_unit_derived AS ( SELECT basis.* -- - , track_changes AND NOT born AND not died AND start_primary_activity_category_path IS DISTINCT FROM stop_primary_activity_category_path AS primary_activity_category_changed - , track_changes AND NOT born AND not died AND start_secondary_activity_category_path IS DISTINCT FROM stop_secondary_activity_category_path AS secondary_activity_category_changed - , track_changes AND NOT born AND not died AND start_sector_path IS DISTINCT FROM stop_sector_path AS sector_changed - , track_changes AND NOT born AND not died AND start_legal_form_id IS DISTINCT FROM stop_legal_form_id AS legal_form_changed - , track_changes AND NOT born AND not died AND start_physical_region_path IS DISTINCT FROM stop_physical_region_path AS physical_region_changed - , track_changes AND NOT born AND not died AND start_physical_country_id IS DISTINCT FROM stop_physical_country_id AS physical_country_changed + , track_changes AND NOT born AND not died AND prev_name IS DISTINCT FROM curr_name AS name_changed + , track_changes AND NOT born AND not died AND prev_primary_activity_category_path IS DISTINCT FROM curr_primary_activity_category_path AS primary_activity_category_changed + , track_changes AND NOT born AND not died AND prev_secondary_activity_category_path IS DISTINCT FROM curr_secondary_activity_category_path AS secondary_activity_category_changed + , track_changes AND NOT born AND not died AND prev_sector_path IS DISTINCT FROM curr_sector_path AS sector_changed + , track_changes AND NOT born AND not died AND prev_legal_form_id IS DISTINCT FROM curr_legal_form_id AS legal_form_changed + , track_changes AND NOT born AND not died AND prev_physical_region_path IS DISTINCT FROM curr_physical_region_path AS physical_region_changed + , track_changes AND NOT born AND not died AND prev_physical_country_id IS DISTINCT FROM curr_physical_country_id AS physical_country_changed + , track_changes AND NOT born AND not died AND ( + prev_physical_address_part1 IS DISTINCT FROM curr_physical_address_part1 + OR prev_physical_address_part2 IS DISTINCT FROM curr_physical_address_part2 + OR prev_physical_address_part3 IS DISTINCT FROM curr_physical_address_part3 + ) AS physical_address_changed -- -- TODO: Track the change in `stats` and put that into `stats_change` using `public.stats_change`. --, CASE WHEN track_changes THEN stop_stats - start_stats ELSE NULL END AS stats_change @@ -5656,24 +5736,26 @@ WITH year_with_unit_basis AS ( , NULL::INTEGER AS month , source.unit_type AS unit_type -- - , source.primary_activity_category_path AS primary_activity_category_path - , source.secondary_activity_category_path AS secondary_activity_category_path - , source.sector_path AS sector_path - , source.legal_form_id AS legal_form_id - , source.physical_region_path AS physical_region_path - , source.physical_country_id AS physical_country_id + , source.curr_primary_activity_category_path AS primary_activity_category_path + , source.curr_secondary_activity_category_path AS secondary_activity_category_path + , source.curr_sector_path AS sector_path + , source.curr_legal_form_id AS legal_form_id + , source.curr_physical_region_path AS physical_region_path + , source.curr_physical_country_id AS physical_country_id -- - , COUNT(source.*) AS count + , COUNT(source.*) FILTER (WHERE NOT source.died) AS count -- , COUNT(source.*) FILTER (WHERE source.born) AS births , COUNT(source.*) FILTER (WHERE source.died) AS deaths -- + , COUNT(source.*) FILTER (WHERE source.name_changed) AS name_change_count , COUNT(source.*) FILTER (WHERE source.primary_activity_category_changed) AS primary_activity_category_change_count , COUNT(source.*) FILTER (WHERE source.secondary_activity_category_changed) AS secondary_activity_category_change_count , COUNT(source.*) FILTER (WHERE source.sector_changed) AS sector_change_count , COUNT(source.*) FILTER (WHERE source.legal_form_changed) AS legal_form_change_count , COUNT(source.*) FILTER (WHERE source.physical_region_changed) AS physical_region_change_count , COUNT(source.*) FILTER (WHERE source.physical_country_changed) AS physical_country_change_count + , COUNT(source.*) FILTER (WHERE source.physical_address_changed) AS physical_address_change_count -- , public.jsonb_stats_summary_merge_agg(source.stats_summary) AS stats_summary FROM year_with_unit_derived AS source @@ -5690,24 +5772,26 @@ WITH year_with_unit_basis AS ( , source.month AS month , source.unit_type AS unit_type -- - , source.primary_activity_category_path AS primary_activity_category_path - , source.secondary_activity_category_path AS secondary_activity_category_path - , source.sector_path AS sector_path - , source.legal_form_id AS legal_form_id - , source.physical_region_path AS physical_region_path - , source.physical_country_id AS physical_country_id + , source.curr_primary_activity_category_path AS primary_activity_category_path + , source.curr_secondary_activity_category_path AS secondary_activity_category_path + , source.curr_sector_path AS sector_path + , source.curr_legal_form_id AS legal_form_id + , source.curr_physical_region_path AS physical_region_path + , source.curr_physical_country_id AS physical_country_id -- - , COUNT(source.*) AS count + , COUNT(source.*) FILTER (WHERE NOT source.died) AS count -- , COUNT(source.*) FILTER (WHERE source.born) AS births , COUNT(source.*) FILTER (WHERE source.died) AS deaths -- + , COUNT(source.*) FILTER (WHERE source.name_changed) AS name_change_count , COUNT(source.*) FILTER (WHERE source.primary_activity_category_changed) AS primary_activity_category_change_count , COUNT(source.*) FILTER (WHERE source.secondary_activity_category_changed) AS secondary_activity_category_change_count , COUNT(source.*) FILTER (WHERE source.sector_changed) AS sector_change_count , COUNT(source.*) FILTER (WHERE source.legal_form_changed) AS legal_form_change_count , COUNT(source.*) FILTER (WHERE source.physical_region_changed) AS physical_region_change_count , COUNT(source.*) FILTER (WHERE source.physical_country_changed) AS physical_country_change_count + , COUNT(source.*) FILTER (WHERE source.physical_address_changed) AS physical_address_change_count -- , public.jsonb_stats_summary_merge_agg(source.stats_summary) AS stats_summary FROM year_and_month_with_unit_derived AS source diff --git a/test/data/42_legal-units-deaths-end-of-first-month.csv b/test/data/42_legal-units-deaths-end-of-first-month.csv index 939293748..091a1d485 100644 --- a/test/data/42_legal-units-deaths-end-of-first-month.csv +++ b/test/data/42_legal-units-deaths-end-of-first-month.csv @@ -1,4 +1,4 @@ valid_from,valid_to,tax_ident,name,birth_date,death_date,physical_address_part1,physical_postal_code,physical_postal_place,physical_region_code,physical_country_iso_2,postal_address_part1,postal_postal_code,postal_postal_place,postal_region_code,postal_country_iso_2,primary_activity_category_code,secondary_activity_category_code,sector_code,legal_form_code -2010-01-01,2011-12-31,823573673,Kranløft Vestland,2010-01-01,,Vestlyvegen 219,4347,LYE,1121,NO,,,,,,49.410,,2100,AS -2012-01-01,2012-12-31,823573673,Kranløft Vestland,2010-01-01,2012-12-31,Vestlyvegen 219,4347,LYE,1121,NO,,,,,,49.410,,2100,AS +2010-01-01,2012-12-31,823573673,Kranløft Vestland,2010-01-01,,Vestlyvegen 219,4347,LYE,1121,NO,,,,,,49.410,,2100,AS +2012-12-31,2012-12-31,823573673,Kranløft Vestland,2010-01-01,2012-12-31,Vestlyvegen 219,4347,LYE,1121,NO,,,,,,49.410,,2100,AS 2010-01-01,2011-01-31,921835809,Kranløft Østland,2010-01-01,2011-01-31,Vestlyvegen 219,4347,LYE,1121,NO,,,,,,49.410,,2100,AS diff --git a/test/data/42_legal-units-deaths-end-of-year.csv b/test/data/42_legal-units-deaths-end-of-year.csv index 20da52831..f066666ec 100644 --- a/test/data/42_legal-units-deaths-end-of-year.csv +++ b/test/data/42_legal-units-deaths-end-of-year.csv @@ -1,4 +1,5 @@ valid_from,valid_to,tax_ident,name,birth_date,death_date,physical_address_part1,physical_postal_code,physical_postal_place,physical_region_code,physical_country_iso_2,postal_address_part1,postal_postal_code,postal_postal_place,postal_region_code,postal_country_iso_2,primary_activity_category_code,secondary_activity_category_code,sector_code,legal_form_code -2010-01-01,2010-12-31,823573673,Kranløft Vestland,2010-01-01,,Vestlyvegen 219,4347,LYE,1121,NO,,,,,,49.410,,2100,AS -2011-01-01,2012-12-31,823573673,Kranløft Vestland,2010-01-01,2012-12-31,Vestlyvegen 219,4347,LYE,1121,NO,,,,,,49.410,,2100,AS -2010-01-01,2011-12-31,921835809,Kranløft Østland,2010-01-01,2011-12-31,Vestlyvegen 219,4347,LYE,1121,NO,,,,,,49.410,,2100,AS +2010-01-01,2012-12-30,823573673,Kranløft Vestland,2010-01-01,,Vestlyvegen 219,4347,LYE,1121,NO,,,,,,49.410,,2100,AS +2012-12-31,2012-12-31,823573673,Kranløft Vestland,2010-01-01,2012-12-31,Vestlyvegen 219,4347,LYE,1121,NO,,,,,,49.410,,2100,AS +2010-01-01,2011-12-30,921835809,Kranløft Østland,2010-01-01,,Vestlyvegen 219,4347,LYE,1121,NO,,,,,,49.410,,2100,AS +2011-12-31,2011-12-31,921835809,Kranløft Østland,2010-01-01,2011-12-31,Vestlyvegen 219,4347,LYE,1121,NO,,,,,,49.410,,2100,AS diff --git a/test/data/42_legal-units-deaths-start-of-month.csv b/test/data/42_legal-units-deaths-start-of-month.csv index bbe84fafa..6fa19aadf 100644 --- a/test/data/42_legal-units-deaths-start-of-month.csv +++ b/test/data/42_legal-units-deaths-start-of-month.csv @@ -1,4 +1,4 @@ valid_from,valid_to,tax_ident,name,birth_date,death_date,physical_address_part1,physical_postal_code,physical_postal_place,physical_region_code,physical_country_iso_2,postal_address_part1,postal_postal_code,postal_postal_place,postal_region_code,postal_country_iso_2,primary_activity_category_code,secondary_activity_category_code,sector_code,legal_form_code -2010-01-01,2010-12-31,823573673,Kranløft Vestland,2010-01-01,,Vestlyvegen 219,4347,LYE,1121,NO,,,,,,49.410,,2100,AS -2011-01-01,2012-01-01,823573673,Kranløft Vestland,2010-01-01,2012-01-01,Vestlyvegen 219,4347,LYE,1121,NO,,,,,,49.410,,2100,AS +2010-01-01,2011-12-31,823573673,Kranløft Vestland,2010-01-01,,Vestlyvegen 219,4347,LYE,1121,NO,,,,,,49.410,,2100,AS +2012-01-01,2012-01-01,823573673,Kranløft Vestland,2010-01-01,2012-01-01,Vestlyvegen 219,4347,LYE,1121,NO,,,,,,49.410,,2100,AS 2010-01-01,2011-01-01,921835809,Kranløft Østland,2010-01-01,2011-01-01,Vestlyvegen 219,4347,LYE,1121,NO,,,,,,49.410,,2100,AS diff --git a/test/expected/03_load_and_verify_history_functions.out b/test/expected/03_load_and_verify_history_functions.out index 0a55386e4..78f8407d4 100644 --- a/test/expected/03_load_and_verify_history_functions.out +++ b/test/expected/03_load_and_verify_history_functions.out @@ -102,190 +102,190 @@ SELECT view_name FROM statistical_unit_refresh_now(); SELECT * FROM public.statistical_history_periods -- Only list previous years, so the test is stable over time. WHERE year <= 2023; - resolution | year | month | time_start | time_stop -------------+------+-------+------------+------------ - year | 2010 | | 2010-01-01 | 2010-12-31 - year | 2011 | | 2011-01-01 | 2011-12-31 - year | 2012 | | 2012-01-01 | 2012-12-31 - year | 2013 | | 2013-01-01 | 2013-12-31 - year | 2014 | | 2014-01-01 | 2014-12-31 - year | 2015 | | 2015-01-01 | 2015-12-31 - year | 2016 | | 2016-01-01 | 2016-12-31 - year | 2017 | | 2017-01-01 | 2017-12-31 - year | 2018 | | 2018-01-01 | 2018-12-31 - year | 2019 | | 2019-01-01 | 2019-12-31 - year | 2020 | | 2020-01-01 | 2020-12-31 - year | 2021 | | 2021-01-01 | 2021-12-31 - year | 2022 | | 2022-01-01 | 2022-12-31 - year | 2023 | | 2023-01-01 | 2023-12-31 - year-month | 2010 | 1 | 2010-01-01 | 2010-01-31 - year-month | 2010 | 2 | 2010-02-01 | 2010-02-28 - year-month | 2010 | 3 | 2010-03-01 | 2010-03-31 - year-month | 2010 | 4 | 2010-04-01 | 2010-04-30 - year-month | 2010 | 5 | 2010-05-01 | 2010-05-31 - year-month | 2010 | 6 | 2010-06-01 | 2010-06-30 - year-month | 2010 | 7 | 2010-07-01 | 2010-07-31 - year-month | 2010 | 8 | 2010-08-01 | 2010-08-31 - year-month | 2010 | 9 | 2010-09-01 | 2010-09-30 - year-month | 2010 | 10 | 2010-10-01 | 2010-10-31 - year-month | 2010 | 11 | 2010-11-01 | 2010-11-30 - year-month | 2010 | 12 | 2010-12-01 | 2010-12-31 - year-month | 2011 | 1 | 2011-01-01 | 2011-01-31 - year-month | 2011 | 2 | 2011-02-01 | 2011-02-28 - year-month | 2011 | 3 | 2011-03-01 | 2011-03-31 - year-month | 2011 | 4 | 2011-04-01 | 2011-04-30 - year-month | 2011 | 5 | 2011-05-01 | 2011-05-31 - year-month | 2011 | 6 | 2011-06-01 | 2011-06-30 - year-month | 2011 | 7 | 2011-07-01 | 2011-07-31 - year-month | 2011 | 8 | 2011-08-01 | 2011-08-31 - year-month | 2011 | 9 | 2011-09-01 | 2011-09-30 - year-month | 2011 | 10 | 2011-10-01 | 2011-10-31 - year-month | 2011 | 11 | 2011-11-01 | 2011-11-30 - year-month | 2011 | 12 | 2011-12-01 | 2011-12-31 - year-month | 2012 | 1 | 2012-01-01 | 2012-01-31 - year-month | 2012 | 2 | 2012-02-01 | 2012-02-29 - year-month | 2012 | 3 | 2012-03-01 | 2012-03-31 - year-month | 2012 | 4 | 2012-04-01 | 2012-04-30 - year-month | 2012 | 5 | 2012-05-01 | 2012-05-31 - year-month | 2012 | 6 | 2012-06-01 | 2012-06-30 - year-month | 2012 | 7 | 2012-07-01 | 2012-07-31 - year-month | 2012 | 8 | 2012-08-01 | 2012-08-31 - year-month | 2012 | 9 | 2012-09-01 | 2012-09-30 - year-month | 2012 | 10 | 2012-10-01 | 2012-10-31 - year-month | 2012 | 11 | 2012-11-01 | 2012-11-30 - year-month | 2012 | 12 | 2012-12-01 | 2012-12-31 - year-month | 2013 | 1 | 2013-01-01 | 2013-01-31 - year-month | 2013 | 2 | 2013-02-01 | 2013-02-28 - year-month | 2013 | 3 | 2013-03-01 | 2013-03-31 - year-month | 2013 | 4 | 2013-04-01 | 2013-04-30 - year-month | 2013 | 5 | 2013-05-01 | 2013-05-31 - year-month | 2013 | 6 | 2013-06-01 | 2013-06-30 - year-month | 2013 | 7 | 2013-07-01 | 2013-07-31 - year-month | 2013 | 8 | 2013-08-01 | 2013-08-31 - year-month | 2013 | 9 | 2013-09-01 | 2013-09-30 - year-month | 2013 | 10 | 2013-10-01 | 2013-10-31 - year-month | 2013 | 11 | 2013-11-01 | 2013-11-30 - year-month | 2013 | 12 | 2013-12-01 | 2013-12-31 - year-month | 2014 | 1 | 2014-01-01 | 2014-01-31 - year-month | 2014 | 2 | 2014-02-01 | 2014-02-28 - year-month | 2014 | 3 | 2014-03-01 | 2014-03-31 - year-month | 2014 | 4 | 2014-04-01 | 2014-04-30 - year-month | 2014 | 5 | 2014-05-01 | 2014-05-31 - year-month | 2014 | 6 | 2014-06-01 | 2014-06-30 - year-month | 2014 | 7 | 2014-07-01 | 2014-07-31 - year-month | 2014 | 8 | 2014-08-01 | 2014-08-31 - year-month | 2014 | 9 | 2014-09-01 | 2014-09-30 - year-month | 2014 | 10 | 2014-10-01 | 2014-10-31 - year-month | 2014 | 11 | 2014-11-01 | 2014-11-30 - year-month | 2014 | 12 | 2014-12-01 | 2014-12-31 - year-month | 2015 | 1 | 2015-01-01 | 2015-01-31 - year-month | 2015 | 2 | 2015-02-01 | 2015-02-28 - year-month | 2015 | 3 | 2015-03-01 | 2015-03-31 - year-month | 2015 | 4 | 2015-04-01 | 2015-04-30 - year-month | 2015 | 5 | 2015-05-01 | 2015-05-31 - year-month | 2015 | 6 | 2015-06-01 | 2015-06-30 - year-month | 2015 | 7 | 2015-07-01 | 2015-07-31 - year-month | 2015 | 8 | 2015-08-01 | 2015-08-31 - year-month | 2015 | 9 | 2015-09-01 | 2015-09-30 - year-month | 2015 | 10 | 2015-10-01 | 2015-10-31 - year-month | 2015 | 11 | 2015-11-01 | 2015-11-30 - year-month | 2015 | 12 | 2015-12-01 | 2015-12-31 - year-month | 2016 | 1 | 2016-01-01 | 2016-01-31 - year-month | 2016 | 2 | 2016-02-01 | 2016-02-29 - year-month | 2016 | 3 | 2016-03-01 | 2016-03-31 - year-month | 2016 | 4 | 2016-04-01 | 2016-04-30 - year-month | 2016 | 5 | 2016-05-01 | 2016-05-31 - year-month | 2016 | 6 | 2016-06-01 | 2016-06-30 - year-month | 2016 | 7 | 2016-07-01 | 2016-07-31 - year-month | 2016 | 8 | 2016-08-01 | 2016-08-31 - year-month | 2016 | 9 | 2016-09-01 | 2016-09-30 - year-month | 2016 | 10 | 2016-10-01 | 2016-10-31 - year-month | 2016 | 11 | 2016-11-01 | 2016-11-30 - year-month | 2016 | 12 | 2016-12-01 | 2016-12-31 - year-month | 2017 | 1 | 2017-01-01 | 2017-01-31 - year-month | 2017 | 2 | 2017-02-01 | 2017-02-28 - year-month | 2017 | 3 | 2017-03-01 | 2017-03-31 - year-month | 2017 | 4 | 2017-04-01 | 2017-04-30 - year-month | 2017 | 5 | 2017-05-01 | 2017-05-31 - year-month | 2017 | 6 | 2017-06-01 | 2017-06-30 - year-month | 2017 | 7 | 2017-07-01 | 2017-07-31 - year-month | 2017 | 8 | 2017-08-01 | 2017-08-31 - year-month | 2017 | 9 | 2017-09-01 | 2017-09-30 - year-month | 2017 | 10 | 2017-10-01 | 2017-10-31 - year-month | 2017 | 11 | 2017-11-01 | 2017-11-30 - year-month | 2017 | 12 | 2017-12-01 | 2017-12-31 - year-month | 2018 | 1 | 2018-01-01 | 2018-01-31 - year-month | 2018 | 2 | 2018-02-01 | 2018-02-28 - year-month | 2018 | 3 | 2018-03-01 | 2018-03-31 - year-month | 2018 | 4 | 2018-04-01 | 2018-04-30 - year-month | 2018 | 5 | 2018-05-01 | 2018-05-31 - year-month | 2018 | 6 | 2018-06-01 | 2018-06-30 - year-month | 2018 | 7 | 2018-07-01 | 2018-07-31 - year-month | 2018 | 8 | 2018-08-01 | 2018-08-31 - year-month | 2018 | 9 | 2018-09-01 | 2018-09-30 - year-month | 2018 | 10 | 2018-10-01 | 2018-10-31 - year-month | 2018 | 11 | 2018-11-01 | 2018-11-30 - year-month | 2018 | 12 | 2018-12-01 | 2018-12-31 - year-month | 2019 | 1 | 2019-01-01 | 2019-01-31 - year-month | 2019 | 2 | 2019-02-01 | 2019-02-28 - year-month | 2019 | 3 | 2019-03-01 | 2019-03-31 - year-month | 2019 | 4 | 2019-04-01 | 2019-04-30 - year-month | 2019 | 5 | 2019-05-01 | 2019-05-31 - year-month | 2019 | 6 | 2019-06-01 | 2019-06-30 - year-month | 2019 | 7 | 2019-07-01 | 2019-07-31 - year-month | 2019 | 8 | 2019-08-01 | 2019-08-31 - year-month | 2019 | 9 | 2019-09-01 | 2019-09-30 - year-month | 2019 | 10 | 2019-10-01 | 2019-10-31 - year-month | 2019 | 11 | 2019-11-01 | 2019-11-30 - year-month | 2019 | 12 | 2019-12-01 | 2019-12-31 - year-month | 2020 | 1 | 2020-01-01 | 2020-01-31 - year-month | 2020 | 2 | 2020-02-01 | 2020-02-29 - year-month | 2020 | 3 | 2020-03-01 | 2020-03-31 - year-month | 2020 | 4 | 2020-04-01 | 2020-04-30 - year-month | 2020 | 5 | 2020-05-01 | 2020-05-31 - year-month | 2020 | 6 | 2020-06-01 | 2020-06-30 - year-month | 2020 | 7 | 2020-07-01 | 2020-07-31 - year-month | 2020 | 8 | 2020-08-01 | 2020-08-31 - year-month | 2020 | 9 | 2020-09-01 | 2020-09-30 - year-month | 2020 | 10 | 2020-10-01 | 2020-10-31 - year-month | 2020 | 11 | 2020-11-01 | 2020-11-30 - year-month | 2020 | 12 | 2020-12-01 | 2020-12-31 - year-month | 2021 | 1 | 2021-01-01 | 2021-01-31 - year-month | 2021 | 2 | 2021-02-01 | 2021-02-28 - year-month | 2021 | 3 | 2021-03-01 | 2021-03-31 - year-month | 2021 | 4 | 2021-04-01 | 2021-04-30 - year-month | 2021 | 5 | 2021-05-01 | 2021-05-31 - year-month | 2021 | 6 | 2021-06-01 | 2021-06-30 - year-month | 2021 | 7 | 2021-07-01 | 2021-07-31 - year-month | 2021 | 8 | 2021-08-01 | 2021-08-31 - year-month | 2021 | 9 | 2021-09-01 | 2021-09-30 - year-month | 2021 | 10 | 2021-10-01 | 2021-10-31 - year-month | 2021 | 11 | 2021-11-01 | 2021-11-30 - year-month | 2021 | 12 | 2021-12-01 | 2021-12-31 - year-month | 2022 | 1 | 2022-01-01 | 2022-01-31 - year-month | 2022 | 2 | 2022-02-01 | 2022-02-28 - year-month | 2022 | 3 | 2022-03-01 | 2022-03-31 - year-month | 2022 | 4 | 2022-04-01 | 2022-04-30 - year-month | 2022 | 5 | 2022-05-01 | 2022-05-31 - year-month | 2022 | 6 | 2022-06-01 | 2022-06-30 - year-month | 2022 | 7 | 2022-07-01 | 2022-07-31 - year-month | 2022 | 8 | 2022-08-01 | 2022-08-31 - year-month | 2022 | 9 | 2022-09-01 | 2022-09-30 - year-month | 2022 | 10 | 2022-10-01 | 2022-10-31 - year-month | 2022 | 11 | 2022-11-01 | 2022-11-30 - year-month | 2022 | 12 | 2022-12-01 | 2022-12-31 - year-month | 2023 | 1 | 2023-01-01 | 2023-01-31 - year-month | 2023 | 2 | 2023-02-01 | 2023-02-28 - year-month | 2023 | 3 | 2023-03-01 | 2023-03-31 - year-month | 2023 | 4 | 2023-04-01 | 2023-04-30 - year-month | 2023 | 5 | 2023-05-01 | 2023-05-31 - year-month | 2023 | 6 | 2023-06-01 | 2023-06-30 - year-month | 2023 | 7 | 2023-07-01 | 2023-07-31 - year-month | 2023 | 8 | 2023-08-01 | 2023-08-31 - year-month | 2023 | 9 | 2023-09-01 | 2023-09-30 - year-month | 2023 | 10 | 2023-10-01 | 2023-10-31 - year-month | 2023 | 11 | 2023-11-01 | 2023-11-30 - year-month | 2023 | 12 | 2023-12-01 | 2023-12-31 + resolution | year | month | prev_stop | curr_start | curr_stop +------------+------+-------+------------+------------+------------ + year | 2010 | | 2009-12-31 | 2010-01-01 | 2010-12-31 + year | 2011 | | 2010-12-31 | 2011-01-01 | 2011-12-31 + year | 2012 | | 2011-12-31 | 2012-01-01 | 2012-12-31 + year | 2013 | | 2012-12-31 | 2013-01-01 | 2013-12-31 + year | 2014 | | 2013-12-31 | 2014-01-01 | 2014-12-31 + year | 2015 | | 2014-12-31 | 2015-01-01 | 2015-12-31 + year | 2016 | | 2015-12-31 | 2016-01-01 | 2016-12-31 + year | 2017 | | 2016-12-31 | 2017-01-01 | 2017-12-31 + year | 2018 | | 2017-12-31 | 2018-01-01 | 2018-12-31 + year | 2019 | | 2018-12-31 | 2019-01-01 | 2019-12-31 + year | 2020 | | 2019-12-31 | 2020-01-01 | 2020-12-31 + year | 2021 | | 2020-12-31 | 2021-01-01 | 2021-12-31 + year | 2022 | | 2021-12-31 | 2022-01-01 | 2022-12-31 + year | 2023 | | 2022-12-31 | 2023-01-01 | 2023-12-31 + year-month | 2010 | 1 | 2009-12-31 | 2010-01-01 | 2010-01-31 + year-month | 2010 | 2 | 2010-01-31 | 2010-02-01 | 2010-02-28 + year-month | 2010 | 3 | 2010-02-28 | 2010-03-01 | 2010-03-31 + year-month | 2010 | 4 | 2010-03-31 | 2010-04-01 | 2010-04-30 + year-month | 2010 | 5 | 2010-04-30 | 2010-05-01 | 2010-05-31 + year-month | 2010 | 6 | 2010-05-31 | 2010-06-01 | 2010-06-30 + year-month | 2010 | 7 | 2010-06-30 | 2010-07-01 | 2010-07-31 + year-month | 2010 | 8 | 2010-07-31 | 2010-08-01 | 2010-08-31 + year-month | 2010 | 9 | 2010-08-31 | 2010-09-01 | 2010-09-30 + year-month | 2010 | 10 | 2010-09-30 | 2010-10-01 | 2010-10-31 + year-month | 2010 | 11 | 2010-10-31 | 2010-11-01 | 2010-11-30 + year-month | 2010 | 12 | 2010-11-30 | 2010-12-01 | 2010-12-31 + year-month | 2011 | 1 | 2010-12-31 | 2011-01-01 | 2011-01-31 + year-month | 2011 | 2 | 2011-01-31 | 2011-02-01 | 2011-02-28 + year-month | 2011 | 3 | 2011-02-28 | 2011-03-01 | 2011-03-31 + year-month | 2011 | 4 | 2011-03-31 | 2011-04-01 | 2011-04-30 + year-month | 2011 | 5 | 2011-04-30 | 2011-05-01 | 2011-05-31 + year-month | 2011 | 6 | 2011-05-31 | 2011-06-01 | 2011-06-30 + year-month | 2011 | 7 | 2011-06-30 | 2011-07-01 | 2011-07-31 + year-month | 2011 | 8 | 2011-07-31 | 2011-08-01 | 2011-08-31 + year-month | 2011 | 9 | 2011-08-31 | 2011-09-01 | 2011-09-30 + year-month | 2011 | 10 | 2011-09-30 | 2011-10-01 | 2011-10-31 + year-month | 2011 | 11 | 2011-10-31 | 2011-11-01 | 2011-11-30 + year-month | 2011 | 12 | 2011-11-30 | 2011-12-01 | 2011-12-31 + year-month | 2012 | 1 | 2011-12-31 | 2012-01-01 | 2012-01-31 + year-month | 2012 | 2 | 2012-01-31 | 2012-02-01 | 2012-02-29 + year-month | 2012 | 3 | 2012-02-29 | 2012-03-01 | 2012-03-31 + year-month | 2012 | 4 | 2012-03-31 | 2012-04-01 | 2012-04-30 + year-month | 2012 | 5 | 2012-04-30 | 2012-05-01 | 2012-05-31 + year-month | 2012 | 6 | 2012-05-31 | 2012-06-01 | 2012-06-30 + year-month | 2012 | 7 | 2012-06-30 | 2012-07-01 | 2012-07-31 + year-month | 2012 | 8 | 2012-07-31 | 2012-08-01 | 2012-08-31 + year-month | 2012 | 9 | 2012-08-31 | 2012-09-01 | 2012-09-30 + year-month | 2012 | 10 | 2012-09-30 | 2012-10-01 | 2012-10-31 + year-month | 2012 | 11 | 2012-10-31 | 2012-11-01 | 2012-11-30 + year-month | 2012 | 12 | 2012-11-30 | 2012-12-01 | 2012-12-31 + year-month | 2013 | 1 | 2012-12-31 | 2013-01-01 | 2013-01-31 + year-month | 2013 | 2 | 2013-01-31 | 2013-02-01 | 2013-02-28 + year-month | 2013 | 3 | 2013-02-28 | 2013-03-01 | 2013-03-31 + year-month | 2013 | 4 | 2013-03-31 | 2013-04-01 | 2013-04-30 + year-month | 2013 | 5 | 2013-04-30 | 2013-05-01 | 2013-05-31 + year-month | 2013 | 6 | 2013-05-31 | 2013-06-01 | 2013-06-30 + year-month | 2013 | 7 | 2013-06-30 | 2013-07-01 | 2013-07-31 + year-month | 2013 | 8 | 2013-07-31 | 2013-08-01 | 2013-08-31 + year-month | 2013 | 9 | 2013-08-31 | 2013-09-01 | 2013-09-30 + year-month | 2013 | 10 | 2013-09-30 | 2013-10-01 | 2013-10-31 + year-month | 2013 | 11 | 2013-10-31 | 2013-11-01 | 2013-11-30 + year-month | 2013 | 12 | 2013-11-30 | 2013-12-01 | 2013-12-31 + year-month | 2014 | 1 | 2013-12-31 | 2014-01-01 | 2014-01-31 + year-month | 2014 | 2 | 2014-01-31 | 2014-02-01 | 2014-02-28 + year-month | 2014 | 3 | 2014-02-28 | 2014-03-01 | 2014-03-31 + year-month | 2014 | 4 | 2014-03-31 | 2014-04-01 | 2014-04-30 + year-month | 2014 | 5 | 2014-04-30 | 2014-05-01 | 2014-05-31 + year-month | 2014 | 6 | 2014-05-31 | 2014-06-01 | 2014-06-30 + year-month | 2014 | 7 | 2014-06-30 | 2014-07-01 | 2014-07-31 + year-month | 2014 | 8 | 2014-07-31 | 2014-08-01 | 2014-08-31 + year-month | 2014 | 9 | 2014-08-31 | 2014-09-01 | 2014-09-30 + year-month | 2014 | 10 | 2014-09-30 | 2014-10-01 | 2014-10-31 + year-month | 2014 | 11 | 2014-10-31 | 2014-11-01 | 2014-11-30 + year-month | 2014 | 12 | 2014-11-30 | 2014-12-01 | 2014-12-31 + year-month | 2015 | 1 | 2014-12-31 | 2015-01-01 | 2015-01-31 + year-month | 2015 | 2 | 2015-01-31 | 2015-02-01 | 2015-02-28 + year-month | 2015 | 3 | 2015-02-28 | 2015-03-01 | 2015-03-31 + year-month | 2015 | 4 | 2015-03-31 | 2015-04-01 | 2015-04-30 + year-month | 2015 | 5 | 2015-04-30 | 2015-05-01 | 2015-05-31 + year-month | 2015 | 6 | 2015-05-31 | 2015-06-01 | 2015-06-30 + year-month | 2015 | 7 | 2015-06-30 | 2015-07-01 | 2015-07-31 + year-month | 2015 | 8 | 2015-07-31 | 2015-08-01 | 2015-08-31 + year-month | 2015 | 9 | 2015-08-31 | 2015-09-01 | 2015-09-30 + year-month | 2015 | 10 | 2015-09-30 | 2015-10-01 | 2015-10-31 + year-month | 2015 | 11 | 2015-10-31 | 2015-11-01 | 2015-11-30 + year-month | 2015 | 12 | 2015-11-30 | 2015-12-01 | 2015-12-31 + year-month | 2016 | 1 | 2015-12-31 | 2016-01-01 | 2016-01-31 + year-month | 2016 | 2 | 2016-01-31 | 2016-02-01 | 2016-02-29 + year-month | 2016 | 3 | 2016-02-29 | 2016-03-01 | 2016-03-31 + year-month | 2016 | 4 | 2016-03-31 | 2016-04-01 | 2016-04-30 + year-month | 2016 | 5 | 2016-04-30 | 2016-05-01 | 2016-05-31 + year-month | 2016 | 6 | 2016-05-31 | 2016-06-01 | 2016-06-30 + year-month | 2016 | 7 | 2016-06-30 | 2016-07-01 | 2016-07-31 + year-month | 2016 | 8 | 2016-07-31 | 2016-08-01 | 2016-08-31 + year-month | 2016 | 9 | 2016-08-31 | 2016-09-01 | 2016-09-30 + year-month | 2016 | 10 | 2016-09-30 | 2016-10-01 | 2016-10-31 + year-month | 2016 | 11 | 2016-10-31 | 2016-11-01 | 2016-11-30 + year-month | 2016 | 12 | 2016-11-30 | 2016-12-01 | 2016-12-31 + year-month | 2017 | 1 | 2016-12-31 | 2017-01-01 | 2017-01-31 + year-month | 2017 | 2 | 2017-01-31 | 2017-02-01 | 2017-02-28 + year-month | 2017 | 3 | 2017-02-28 | 2017-03-01 | 2017-03-31 + year-month | 2017 | 4 | 2017-03-31 | 2017-04-01 | 2017-04-30 + year-month | 2017 | 5 | 2017-04-30 | 2017-05-01 | 2017-05-31 + year-month | 2017 | 6 | 2017-05-31 | 2017-06-01 | 2017-06-30 + year-month | 2017 | 7 | 2017-06-30 | 2017-07-01 | 2017-07-31 + year-month | 2017 | 8 | 2017-07-31 | 2017-08-01 | 2017-08-31 + year-month | 2017 | 9 | 2017-08-31 | 2017-09-01 | 2017-09-30 + year-month | 2017 | 10 | 2017-09-30 | 2017-10-01 | 2017-10-31 + year-month | 2017 | 11 | 2017-10-31 | 2017-11-01 | 2017-11-30 + year-month | 2017 | 12 | 2017-11-30 | 2017-12-01 | 2017-12-31 + year-month | 2018 | 1 | 2017-12-31 | 2018-01-01 | 2018-01-31 + year-month | 2018 | 2 | 2018-01-31 | 2018-02-01 | 2018-02-28 + year-month | 2018 | 3 | 2018-02-28 | 2018-03-01 | 2018-03-31 + year-month | 2018 | 4 | 2018-03-31 | 2018-04-01 | 2018-04-30 + year-month | 2018 | 5 | 2018-04-30 | 2018-05-01 | 2018-05-31 + year-month | 2018 | 6 | 2018-05-31 | 2018-06-01 | 2018-06-30 + year-month | 2018 | 7 | 2018-06-30 | 2018-07-01 | 2018-07-31 + year-month | 2018 | 8 | 2018-07-31 | 2018-08-01 | 2018-08-31 + year-month | 2018 | 9 | 2018-08-31 | 2018-09-01 | 2018-09-30 + year-month | 2018 | 10 | 2018-09-30 | 2018-10-01 | 2018-10-31 + year-month | 2018 | 11 | 2018-10-31 | 2018-11-01 | 2018-11-30 + year-month | 2018 | 12 | 2018-11-30 | 2018-12-01 | 2018-12-31 + year-month | 2019 | 1 | 2018-12-31 | 2019-01-01 | 2019-01-31 + year-month | 2019 | 2 | 2019-01-31 | 2019-02-01 | 2019-02-28 + year-month | 2019 | 3 | 2019-02-28 | 2019-03-01 | 2019-03-31 + year-month | 2019 | 4 | 2019-03-31 | 2019-04-01 | 2019-04-30 + year-month | 2019 | 5 | 2019-04-30 | 2019-05-01 | 2019-05-31 + year-month | 2019 | 6 | 2019-05-31 | 2019-06-01 | 2019-06-30 + year-month | 2019 | 7 | 2019-06-30 | 2019-07-01 | 2019-07-31 + year-month | 2019 | 8 | 2019-07-31 | 2019-08-01 | 2019-08-31 + year-month | 2019 | 9 | 2019-08-31 | 2019-09-01 | 2019-09-30 + year-month | 2019 | 10 | 2019-09-30 | 2019-10-01 | 2019-10-31 + year-month | 2019 | 11 | 2019-10-31 | 2019-11-01 | 2019-11-30 + year-month | 2019 | 12 | 2019-11-30 | 2019-12-01 | 2019-12-31 + year-month | 2020 | 1 | 2019-12-31 | 2020-01-01 | 2020-01-31 + year-month | 2020 | 2 | 2020-01-31 | 2020-02-01 | 2020-02-29 + year-month | 2020 | 3 | 2020-02-29 | 2020-03-01 | 2020-03-31 + year-month | 2020 | 4 | 2020-03-31 | 2020-04-01 | 2020-04-30 + year-month | 2020 | 5 | 2020-04-30 | 2020-05-01 | 2020-05-31 + year-month | 2020 | 6 | 2020-05-31 | 2020-06-01 | 2020-06-30 + year-month | 2020 | 7 | 2020-06-30 | 2020-07-01 | 2020-07-31 + year-month | 2020 | 8 | 2020-07-31 | 2020-08-01 | 2020-08-31 + year-month | 2020 | 9 | 2020-08-31 | 2020-09-01 | 2020-09-30 + year-month | 2020 | 10 | 2020-09-30 | 2020-10-01 | 2020-10-31 + year-month | 2020 | 11 | 2020-10-31 | 2020-11-01 | 2020-11-30 + year-month | 2020 | 12 | 2020-11-30 | 2020-12-01 | 2020-12-31 + year-month | 2021 | 1 | 2020-12-31 | 2021-01-01 | 2021-01-31 + year-month | 2021 | 2 | 2021-01-31 | 2021-02-01 | 2021-02-28 + year-month | 2021 | 3 | 2021-02-28 | 2021-03-01 | 2021-03-31 + year-month | 2021 | 4 | 2021-03-31 | 2021-04-01 | 2021-04-30 + year-month | 2021 | 5 | 2021-04-30 | 2021-05-01 | 2021-05-31 + year-month | 2021 | 6 | 2021-05-31 | 2021-06-01 | 2021-06-30 + year-month | 2021 | 7 | 2021-06-30 | 2021-07-01 | 2021-07-31 + year-month | 2021 | 8 | 2021-07-31 | 2021-08-01 | 2021-08-31 + year-month | 2021 | 9 | 2021-08-31 | 2021-09-01 | 2021-09-30 + year-month | 2021 | 10 | 2021-09-30 | 2021-10-01 | 2021-10-31 + year-month | 2021 | 11 | 2021-10-31 | 2021-11-01 | 2021-11-30 + year-month | 2021 | 12 | 2021-11-30 | 2021-12-01 | 2021-12-31 + year-month | 2022 | 1 | 2021-12-31 | 2022-01-01 | 2022-01-31 + year-month | 2022 | 2 | 2022-01-31 | 2022-02-01 | 2022-02-28 + year-month | 2022 | 3 | 2022-02-28 | 2022-03-01 | 2022-03-31 + year-month | 2022 | 4 | 2022-03-31 | 2022-04-01 | 2022-04-30 + year-month | 2022 | 5 | 2022-04-30 | 2022-05-01 | 2022-05-31 + year-month | 2022 | 6 | 2022-05-31 | 2022-06-01 | 2022-06-30 + year-month | 2022 | 7 | 2022-06-30 | 2022-07-01 | 2022-07-31 + year-month | 2022 | 8 | 2022-07-31 | 2022-08-01 | 2022-08-31 + year-month | 2022 | 9 | 2022-08-31 | 2022-09-01 | 2022-09-30 + year-month | 2022 | 10 | 2022-09-30 | 2022-10-01 | 2022-10-31 + year-month | 2022 | 11 | 2022-10-31 | 2022-11-01 | 2022-11-30 + year-month | 2022 | 12 | 2022-11-30 | 2022-12-01 | 2022-12-31 + year-month | 2023 | 1 | 2022-12-31 | 2023-01-01 | 2023-01-31 + year-month | 2023 | 2 | 2023-01-31 | 2023-02-01 | 2023-02-28 + year-month | 2023 | 3 | 2023-02-28 | 2023-03-01 | 2023-03-31 + year-month | 2023 | 4 | 2023-03-31 | 2023-04-01 | 2023-04-30 + year-month | 2023 | 5 | 2023-04-30 | 2023-05-01 | 2023-05-31 + year-month | 2023 | 6 | 2023-05-31 | 2023-06-01 | 2023-06-30 + year-month | 2023 | 7 | 2023-06-30 | 2023-07-01 | 2023-07-31 + year-month | 2023 | 8 | 2023-07-31 | 2023-08-01 | 2023-08-31 + year-month | 2023 | 9 | 2023-08-31 | 2023-09-01 | 2023-09-30 + year-month | 2023 | 10 | 2023-09-30 | 2023-10-01 | 2023-10-31 + year-month | 2023 | 11 | 2023-10-31 | 2023-11-01 | 2023-11-30 + year-month | 2023 | 12 | 2023-11-30 | 2023-12-01 | 2023-12-31 (182 rows) \echo "Checking timepoints." @@ -3515,190 +3515,190 @@ stats_summary | { + SELECT * FROM public.statistical_history_periods -- Only list previous years, so the test is stable over time. WHERE year <= 2023; - resolution | year | month | time_start | time_stop -------------+------+-------+------------+------------ - year | 2010 | | 2010-01-01 | 2010-12-31 - year | 2011 | | 2011-01-01 | 2011-12-31 - year | 2012 | | 2012-01-01 | 2012-12-31 - year | 2013 | | 2013-01-01 | 2013-12-31 - year | 2014 | | 2014-01-01 | 2014-12-31 - year | 2015 | | 2015-01-01 | 2015-12-31 - year | 2016 | | 2016-01-01 | 2016-12-31 - year | 2017 | | 2017-01-01 | 2017-12-31 - year | 2018 | | 2018-01-01 | 2018-12-31 - year | 2019 | | 2019-01-01 | 2019-12-31 - year | 2020 | | 2020-01-01 | 2020-12-31 - year | 2021 | | 2021-01-01 | 2021-12-31 - year | 2022 | | 2022-01-01 | 2022-12-31 - year | 2023 | | 2023-01-01 | 2023-12-31 - year-month | 2010 | 1 | 2010-01-01 | 2010-01-31 - year-month | 2010 | 2 | 2010-02-01 | 2010-02-28 - year-month | 2010 | 3 | 2010-03-01 | 2010-03-31 - year-month | 2010 | 4 | 2010-04-01 | 2010-04-30 - year-month | 2010 | 5 | 2010-05-01 | 2010-05-31 - year-month | 2010 | 6 | 2010-06-01 | 2010-06-30 - year-month | 2010 | 7 | 2010-07-01 | 2010-07-31 - year-month | 2010 | 8 | 2010-08-01 | 2010-08-31 - year-month | 2010 | 9 | 2010-09-01 | 2010-09-30 - year-month | 2010 | 10 | 2010-10-01 | 2010-10-31 - year-month | 2010 | 11 | 2010-11-01 | 2010-11-30 - year-month | 2010 | 12 | 2010-12-01 | 2010-12-31 - year-month | 2011 | 1 | 2011-01-01 | 2011-01-31 - year-month | 2011 | 2 | 2011-02-01 | 2011-02-28 - year-month | 2011 | 3 | 2011-03-01 | 2011-03-31 - year-month | 2011 | 4 | 2011-04-01 | 2011-04-30 - year-month | 2011 | 5 | 2011-05-01 | 2011-05-31 - year-month | 2011 | 6 | 2011-06-01 | 2011-06-30 - year-month | 2011 | 7 | 2011-07-01 | 2011-07-31 - year-month | 2011 | 8 | 2011-08-01 | 2011-08-31 - year-month | 2011 | 9 | 2011-09-01 | 2011-09-30 - year-month | 2011 | 10 | 2011-10-01 | 2011-10-31 - year-month | 2011 | 11 | 2011-11-01 | 2011-11-30 - year-month | 2011 | 12 | 2011-12-01 | 2011-12-31 - year-month | 2012 | 1 | 2012-01-01 | 2012-01-31 - year-month | 2012 | 2 | 2012-02-01 | 2012-02-29 - year-month | 2012 | 3 | 2012-03-01 | 2012-03-31 - year-month | 2012 | 4 | 2012-04-01 | 2012-04-30 - year-month | 2012 | 5 | 2012-05-01 | 2012-05-31 - year-month | 2012 | 6 | 2012-06-01 | 2012-06-30 - year-month | 2012 | 7 | 2012-07-01 | 2012-07-31 - year-month | 2012 | 8 | 2012-08-01 | 2012-08-31 - year-month | 2012 | 9 | 2012-09-01 | 2012-09-30 - year-month | 2012 | 10 | 2012-10-01 | 2012-10-31 - year-month | 2012 | 11 | 2012-11-01 | 2012-11-30 - year-month | 2012 | 12 | 2012-12-01 | 2012-12-31 - year-month | 2013 | 1 | 2013-01-01 | 2013-01-31 - year-month | 2013 | 2 | 2013-02-01 | 2013-02-28 - year-month | 2013 | 3 | 2013-03-01 | 2013-03-31 - year-month | 2013 | 4 | 2013-04-01 | 2013-04-30 - year-month | 2013 | 5 | 2013-05-01 | 2013-05-31 - year-month | 2013 | 6 | 2013-06-01 | 2013-06-30 - year-month | 2013 | 7 | 2013-07-01 | 2013-07-31 - year-month | 2013 | 8 | 2013-08-01 | 2013-08-31 - year-month | 2013 | 9 | 2013-09-01 | 2013-09-30 - year-month | 2013 | 10 | 2013-10-01 | 2013-10-31 - year-month | 2013 | 11 | 2013-11-01 | 2013-11-30 - year-month | 2013 | 12 | 2013-12-01 | 2013-12-31 - year-month | 2014 | 1 | 2014-01-01 | 2014-01-31 - year-month | 2014 | 2 | 2014-02-01 | 2014-02-28 - year-month | 2014 | 3 | 2014-03-01 | 2014-03-31 - year-month | 2014 | 4 | 2014-04-01 | 2014-04-30 - year-month | 2014 | 5 | 2014-05-01 | 2014-05-31 - year-month | 2014 | 6 | 2014-06-01 | 2014-06-30 - year-month | 2014 | 7 | 2014-07-01 | 2014-07-31 - year-month | 2014 | 8 | 2014-08-01 | 2014-08-31 - year-month | 2014 | 9 | 2014-09-01 | 2014-09-30 - year-month | 2014 | 10 | 2014-10-01 | 2014-10-31 - year-month | 2014 | 11 | 2014-11-01 | 2014-11-30 - year-month | 2014 | 12 | 2014-12-01 | 2014-12-31 - year-month | 2015 | 1 | 2015-01-01 | 2015-01-31 - year-month | 2015 | 2 | 2015-02-01 | 2015-02-28 - year-month | 2015 | 3 | 2015-03-01 | 2015-03-31 - year-month | 2015 | 4 | 2015-04-01 | 2015-04-30 - year-month | 2015 | 5 | 2015-05-01 | 2015-05-31 - year-month | 2015 | 6 | 2015-06-01 | 2015-06-30 - year-month | 2015 | 7 | 2015-07-01 | 2015-07-31 - year-month | 2015 | 8 | 2015-08-01 | 2015-08-31 - year-month | 2015 | 9 | 2015-09-01 | 2015-09-30 - year-month | 2015 | 10 | 2015-10-01 | 2015-10-31 - year-month | 2015 | 11 | 2015-11-01 | 2015-11-30 - year-month | 2015 | 12 | 2015-12-01 | 2015-12-31 - year-month | 2016 | 1 | 2016-01-01 | 2016-01-31 - year-month | 2016 | 2 | 2016-02-01 | 2016-02-29 - year-month | 2016 | 3 | 2016-03-01 | 2016-03-31 - year-month | 2016 | 4 | 2016-04-01 | 2016-04-30 - year-month | 2016 | 5 | 2016-05-01 | 2016-05-31 - year-month | 2016 | 6 | 2016-06-01 | 2016-06-30 - year-month | 2016 | 7 | 2016-07-01 | 2016-07-31 - year-month | 2016 | 8 | 2016-08-01 | 2016-08-31 - year-month | 2016 | 9 | 2016-09-01 | 2016-09-30 - year-month | 2016 | 10 | 2016-10-01 | 2016-10-31 - year-month | 2016 | 11 | 2016-11-01 | 2016-11-30 - year-month | 2016 | 12 | 2016-12-01 | 2016-12-31 - year-month | 2017 | 1 | 2017-01-01 | 2017-01-31 - year-month | 2017 | 2 | 2017-02-01 | 2017-02-28 - year-month | 2017 | 3 | 2017-03-01 | 2017-03-31 - year-month | 2017 | 4 | 2017-04-01 | 2017-04-30 - year-month | 2017 | 5 | 2017-05-01 | 2017-05-31 - year-month | 2017 | 6 | 2017-06-01 | 2017-06-30 - year-month | 2017 | 7 | 2017-07-01 | 2017-07-31 - year-month | 2017 | 8 | 2017-08-01 | 2017-08-31 - year-month | 2017 | 9 | 2017-09-01 | 2017-09-30 - year-month | 2017 | 10 | 2017-10-01 | 2017-10-31 - year-month | 2017 | 11 | 2017-11-01 | 2017-11-30 - year-month | 2017 | 12 | 2017-12-01 | 2017-12-31 - year-month | 2018 | 1 | 2018-01-01 | 2018-01-31 - year-month | 2018 | 2 | 2018-02-01 | 2018-02-28 - year-month | 2018 | 3 | 2018-03-01 | 2018-03-31 - year-month | 2018 | 4 | 2018-04-01 | 2018-04-30 - year-month | 2018 | 5 | 2018-05-01 | 2018-05-31 - year-month | 2018 | 6 | 2018-06-01 | 2018-06-30 - year-month | 2018 | 7 | 2018-07-01 | 2018-07-31 - year-month | 2018 | 8 | 2018-08-01 | 2018-08-31 - year-month | 2018 | 9 | 2018-09-01 | 2018-09-30 - year-month | 2018 | 10 | 2018-10-01 | 2018-10-31 - year-month | 2018 | 11 | 2018-11-01 | 2018-11-30 - year-month | 2018 | 12 | 2018-12-01 | 2018-12-31 - year-month | 2019 | 1 | 2019-01-01 | 2019-01-31 - year-month | 2019 | 2 | 2019-02-01 | 2019-02-28 - year-month | 2019 | 3 | 2019-03-01 | 2019-03-31 - year-month | 2019 | 4 | 2019-04-01 | 2019-04-30 - year-month | 2019 | 5 | 2019-05-01 | 2019-05-31 - year-month | 2019 | 6 | 2019-06-01 | 2019-06-30 - year-month | 2019 | 7 | 2019-07-01 | 2019-07-31 - year-month | 2019 | 8 | 2019-08-01 | 2019-08-31 - year-month | 2019 | 9 | 2019-09-01 | 2019-09-30 - year-month | 2019 | 10 | 2019-10-01 | 2019-10-31 - year-month | 2019 | 11 | 2019-11-01 | 2019-11-30 - year-month | 2019 | 12 | 2019-12-01 | 2019-12-31 - year-month | 2020 | 1 | 2020-01-01 | 2020-01-31 - year-month | 2020 | 2 | 2020-02-01 | 2020-02-29 - year-month | 2020 | 3 | 2020-03-01 | 2020-03-31 - year-month | 2020 | 4 | 2020-04-01 | 2020-04-30 - year-month | 2020 | 5 | 2020-05-01 | 2020-05-31 - year-month | 2020 | 6 | 2020-06-01 | 2020-06-30 - year-month | 2020 | 7 | 2020-07-01 | 2020-07-31 - year-month | 2020 | 8 | 2020-08-01 | 2020-08-31 - year-month | 2020 | 9 | 2020-09-01 | 2020-09-30 - year-month | 2020 | 10 | 2020-10-01 | 2020-10-31 - year-month | 2020 | 11 | 2020-11-01 | 2020-11-30 - year-month | 2020 | 12 | 2020-12-01 | 2020-12-31 - year-month | 2021 | 1 | 2021-01-01 | 2021-01-31 - year-month | 2021 | 2 | 2021-02-01 | 2021-02-28 - year-month | 2021 | 3 | 2021-03-01 | 2021-03-31 - year-month | 2021 | 4 | 2021-04-01 | 2021-04-30 - year-month | 2021 | 5 | 2021-05-01 | 2021-05-31 - year-month | 2021 | 6 | 2021-06-01 | 2021-06-30 - year-month | 2021 | 7 | 2021-07-01 | 2021-07-31 - year-month | 2021 | 8 | 2021-08-01 | 2021-08-31 - year-month | 2021 | 9 | 2021-09-01 | 2021-09-30 - year-month | 2021 | 10 | 2021-10-01 | 2021-10-31 - year-month | 2021 | 11 | 2021-11-01 | 2021-11-30 - year-month | 2021 | 12 | 2021-12-01 | 2021-12-31 - year-month | 2022 | 1 | 2022-01-01 | 2022-01-31 - year-month | 2022 | 2 | 2022-02-01 | 2022-02-28 - year-month | 2022 | 3 | 2022-03-01 | 2022-03-31 - year-month | 2022 | 4 | 2022-04-01 | 2022-04-30 - year-month | 2022 | 5 | 2022-05-01 | 2022-05-31 - year-month | 2022 | 6 | 2022-06-01 | 2022-06-30 - year-month | 2022 | 7 | 2022-07-01 | 2022-07-31 - year-month | 2022 | 8 | 2022-08-01 | 2022-08-31 - year-month | 2022 | 9 | 2022-09-01 | 2022-09-30 - year-month | 2022 | 10 | 2022-10-01 | 2022-10-31 - year-month | 2022 | 11 | 2022-11-01 | 2022-11-30 - year-month | 2022 | 12 | 2022-12-01 | 2022-12-31 - year-month | 2023 | 1 | 2023-01-01 | 2023-01-31 - year-month | 2023 | 2 | 2023-02-01 | 2023-02-28 - year-month | 2023 | 3 | 2023-03-01 | 2023-03-31 - year-month | 2023 | 4 | 2023-04-01 | 2023-04-30 - year-month | 2023 | 5 | 2023-05-01 | 2023-05-31 - year-month | 2023 | 6 | 2023-06-01 | 2023-06-30 - year-month | 2023 | 7 | 2023-07-01 | 2023-07-31 - year-month | 2023 | 8 | 2023-08-01 | 2023-08-31 - year-month | 2023 | 9 | 2023-09-01 | 2023-09-30 - year-month | 2023 | 10 | 2023-10-01 | 2023-10-31 - year-month | 2023 | 11 | 2023-11-01 | 2023-11-30 - year-month | 2023 | 12 | 2023-12-01 | 2023-12-31 + resolution | year | month | prev_stop | curr_start | curr_stop +------------+------+-------+------------+------------+------------ + year | 2010 | | 2009-12-31 | 2010-01-01 | 2010-12-31 + year | 2011 | | 2010-12-31 | 2011-01-01 | 2011-12-31 + year | 2012 | | 2011-12-31 | 2012-01-01 | 2012-12-31 + year | 2013 | | 2012-12-31 | 2013-01-01 | 2013-12-31 + year | 2014 | | 2013-12-31 | 2014-01-01 | 2014-12-31 + year | 2015 | | 2014-12-31 | 2015-01-01 | 2015-12-31 + year | 2016 | | 2015-12-31 | 2016-01-01 | 2016-12-31 + year | 2017 | | 2016-12-31 | 2017-01-01 | 2017-12-31 + year | 2018 | | 2017-12-31 | 2018-01-01 | 2018-12-31 + year | 2019 | | 2018-12-31 | 2019-01-01 | 2019-12-31 + year | 2020 | | 2019-12-31 | 2020-01-01 | 2020-12-31 + year | 2021 | | 2020-12-31 | 2021-01-01 | 2021-12-31 + year | 2022 | | 2021-12-31 | 2022-01-01 | 2022-12-31 + year | 2023 | | 2022-12-31 | 2023-01-01 | 2023-12-31 + year-month | 2010 | 1 | 2009-12-31 | 2010-01-01 | 2010-01-31 + year-month | 2010 | 2 | 2010-01-31 | 2010-02-01 | 2010-02-28 + year-month | 2010 | 3 | 2010-02-28 | 2010-03-01 | 2010-03-31 + year-month | 2010 | 4 | 2010-03-31 | 2010-04-01 | 2010-04-30 + year-month | 2010 | 5 | 2010-04-30 | 2010-05-01 | 2010-05-31 + year-month | 2010 | 6 | 2010-05-31 | 2010-06-01 | 2010-06-30 + year-month | 2010 | 7 | 2010-06-30 | 2010-07-01 | 2010-07-31 + year-month | 2010 | 8 | 2010-07-31 | 2010-08-01 | 2010-08-31 + year-month | 2010 | 9 | 2010-08-31 | 2010-09-01 | 2010-09-30 + year-month | 2010 | 10 | 2010-09-30 | 2010-10-01 | 2010-10-31 + year-month | 2010 | 11 | 2010-10-31 | 2010-11-01 | 2010-11-30 + year-month | 2010 | 12 | 2010-11-30 | 2010-12-01 | 2010-12-31 + year-month | 2011 | 1 | 2010-12-31 | 2011-01-01 | 2011-01-31 + year-month | 2011 | 2 | 2011-01-31 | 2011-02-01 | 2011-02-28 + year-month | 2011 | 3 | 2011-02-28 | 2011-03-01 | 2011-03-31 + year-month | 2011 | 4 | 2011-03-31 | 2011-04-01 | 2011-04-30 + year-month | 2011 | 5 | 2011-04-30 | 2011-05-01 | 2011-05-31 + year-month | 2011 | 6 | 2011-05-31 | 2011-06-01 | 2011-06-30 + year-month | 2011 | 7 | 2011-06-30 | 2011-07-01 | 2011-07-31 + year-month | 2011 | 8 | 2011-07-31 | 2011-08-01 | 2011-08-31 + year-month | 2011 | 9 | 2011-08-31 | 2011-09-01 | 2011-09-30 + year-month | 2011 | 10 | 2011-09-30 | 2011-10-01 | 2011-10-31 + year-month | 2011 | 11 | 2011-10-31 | 2011-11-01 | 2011-11-30 + year-month | 2011 | 12 | 2011-11-30 | 2011-12-01 | 2011-12-31 + year-month | 2012 | 1 | 2011-12-31 | 2012-01-01 | 2012-01-31 + year-month | 2012 | 2 | 2012-01-31 | 2012-02-01 | 2012-02-29 + year-month | 2012 | 3 | 2012-02-29 | 2012-03-01 | 2012-03-31 + year-month | 2012 | 4 | 2012-03-31 | 2012-04-01 | 2012-04-30 + year-month | 2012 | 5 | 2012-04-30 | 2012-05-01 | 2012-05-31 + year-month | 2012 | 6 | 2012-05-31 | 2012-06-01 | 2012-06-30 + year-month | 2012 | 7 | 2012-06-30 | 2012-07-01 | 2012-07-31 + year-month | 2012 | 8 | 2012-07-31 | 2012-08-01 | 2012-08-31 + year-month | 2012 | 9 | 2012-08-31 | 2012-09-01 | 2012-09-30 + year-month | 2012 | 10 | 2012-09-30 | 2012-10-01 | 2012-10-31 + year-month | 2012 | 11 | 2012-10-31 | 2012-11-01 | 2012-11-30 + year-month | 2012 | 12 | 2012-11-30 | 2012-12-01 | 2012-12-31 + year-month | 2013 | 1 | 2012-12-31 | 2013-01-01 | 2013-01-31 + year-month | 2013 | 2 | 2013-01-31 | 2013-02-01 | 2013-02-28 + year-month | 2013 | 3 | 2013-02-28 | 2013-03-01 | 2013-03-31 + year-month | 2013 | 4 | 2013-03-31 | 2013-04-01 | 2013-04-30 + year-month | 2013 | 5 | 2013-04-30 | 2013-05-01 | 2013-05-31 + year-month | 2013 | 6 | 2013-05-31 | 2013-06-01 | 2013-06-30 + year-month | 2013 | 7 | 2013-06-30 | 2013-07-01 | 2013-07-31 + year-month | 2013 | 8 | 2013-07-31 | 2013-08-01 | 2013-08-31 + year-month | 2013 | 9 | 2013-08-31 | 2013-09-01 | 2013-09-30 + year-month | 2013 | 10 | 2013-09-30 | 2013-10-01 | 2013-10-31 + year-month | 2013 | 11 | 2013-10-31 | 2013-11-01 | 2013-11-30 + year-month | 2013 | 12 | 2013-11-30 | 2013-12-01 | 2013-12-31 + year-month | 2014 | 1 | 2013-12-31 | 2014-01-01 | 2014-01-31 + year-month | 2014 | 2 | 2014-01-31 | 2014-02-01 | 2014-02-28 + year-month | 2014 | 3 | 2014-02-28 | 2014-03-01 | 2014-03-31 + year-month | 2014 | 4 | 2014-03-31 | 2014-04-01 | 2014-04-30 + year-month | 2014 | 5 | 2014-04-30 | 2014-05-01 | 2014-05-31 + year-month | 2014 | 6 | 2014-05-31 | 2014-06-01 | 2014-06-30 + year-month | 2014 | 7 | 2014-06-30 | 2014-07-01 | 2014-07-31 + year-month | 2014 | 8 | 2014-07-31 | 2014-08-01 | 2014-08-31 + year-month | 2014 | 9 | 2014-08-31 | 2014-09-01 | 2014-09-30 + year-month | 2014 | 10 | 2014-09-30 | 2014-10-01 | 2014-10-31 + year-month | 2014 | 11 | 2014-10-31 | 2014-11-01 | 2014-11-30 + year-month | 2014 | 12 | 2014-11-30 | 2014-12-01 | 2014-12-31 + year-month | 2015 | 1 | 2014-12-31 | 2015-01-01 | 2015-01-31 + year-month | 2015 | 2 | 2015-01-31 | 2015-02-01 | 2015-02-28 + year-month | 2015 | 3 | 2015-02-28 | 2015-03-01 | 2015-03-31 + year-month | 2015 | 4 | 2015-03-31 | 2015-04-01 | 2015-04-30 + year-month | 2015 | 5 | 2015-04-30 | 2015-05-01 | 2015-05-31 + year-month | 2015 | 6 | 2015-05-31 | 2015-06-01 | 2015-06-30 + year-month | 2015 | 7 | 2015-06-30 | 2015-07-01 | 2015-07-31 + year-month | 2015 | 8 | 2015-07-31 | 2015-08-01 | 2015-08-31 + year-month | 2015 | 9 | 2015-08-31 | 2015-09-01 | 2015-09-30 + year-month | 2015 | 10 | 2015-09-30 | 2015-10-01 | 2015-10-31 + year-month | 2015 | 11 | 2015-10-31 | 2015-11-01 | 2015-11-30 + year-month | 2015 | 12 | 2015-11-30 | 2015-12-01 | 2015-12-31 + year-month | 2016 | 1 | 2015-12-31 | 2016-01-01 | 2016-01-31 + year-month | 2016 | 2 | 2016-01-31 | 2016-02-01 | 2016-02-29 + year-month | 2016 | 3 | 2016-02-29 | 2016-03-01 | 2016-03-31 + year-month | 2016 | 4 | 2016-03-31 | 2016-04-01 | 2016-04-30 + year-month | 2016 | 5 | 2016-04-30 | 2016-05-01 | 2016-05-31 + year-month | 2016 | 6 | 2016-05-31 | 2016-06-01 | 2016-06-30 + year-month | 2016 | 7 | 2016-06-30 | 2016-07-01 | 2016-07-31 + year-month | 2016 | 8 | 2016-07-31 | 2016-08-01 | 2016-08-31 + year-month | 2016 | 9 | 2016-08-31 | 2016-09-01 | 2016-09-30 + year-month | 2016 | 10 | 2016-09-30 | 2016-10-01 | 2016-10-31 + year-month | 2016 | 11 | 2016-10-31 | 2016-11-01 | 2016-11-30 + year-month | 2016 | 12 | 2016-11-30 | 2016-12-01 | 2016-12-31 + year-month | 2017 | 1 | 2016-12-31 | 2017-01-01 | 2017-01-31 + year-month | 2017 | 2 | 2017-01-31 | 2017-02-01 | 2017-02-28 + year-month | 2017 | 3 | 2017-02-28 | 2017-03-01 | 2017-03-31 + year-month | 2017 | 4 | 2017-03-31 | 2017-04-01 | 2017-04-30 + year-month | 2017 | 5 | 2017-04-30 | 2017-05-01 | 2017-05-31 + year-month | 2017 | 6 | 2017-05-31 | 2017-06-01 | 2017-06-30 + year-month | 2017 | 7 | 2017-06-30 | 2017-07-01 | 2017-07-31 + year-month | 2017 | 8 | 2017-07-31 | 2017-08-01 | 2017-08-31 + year-month | 2017 | 9 | 2017-08-31 | 2017-09-01 | 2017-09-30 + year-month | 2017 | 10 | 2017-09-30 | 2017-10-01 | 2017-10-31 + year-month | 2017 | 11 | 2017-10-31 | 2017-11-01 | 2017-11-30 + year-month | 2017 | 12 | 2017-11-30 | 2017-12-01 | 2017-12-31 + year-month | 2018 | 1 | 2017-12-31 | 2018-01-01 | 2018-01-31 + year-month | 2018 | 2 | 2018-01-31 | 2018-02-01 | 2018-02-28 + year-month | 2018 | 3 | 2018-02-28 | 2018-03-01 | 2018-03-31 + year-month | 2018 | 4 | 2018-03-31 | 2018-04-01 | 2018-04-30 + year-month | 2018 | 5 | 2018-04-30 | 2018-05-01 | 2018-05-31 + year-month | 2018 | 6 | 2018-05-31 | 2018-06-01 | 2018-06-30 + year-month | 2018 | 7 | 2018-06-30 | 2018-07-01 | 2018-07-31 + year-month | 2018 | 8 | 2018-07-31 | 2018-08-01 | 2018-08-31 + year-month | 2018 | 9 | 2018-08-31 | 2018-09-01 | 2018-09-30 + year-month | 2018 | 10 | 2018-09-30 | 2018-10-01 | 2018-10-31 + year-month | 2018 | 11 | 2018-10-31 | 2018-11-01 | 2018-11-30 + year-month | 2018 | 12 | 2018-11-30 | 2018-12-01 | 2018-12-31 + year-month | 2019 | 1 | 2018-12-31 | 2019-01-01 | 2019-01-31 + year-month | 2019 | 2 | 2019-01-31 | 2019-02-01 | 2019-02-28 + year-month | 2019 | 3 | 2019-02-28 | 2019-03-01 | 2019-03-31 + year-month | 2019 | 4 | 2019-03-31 | 2019-04-01 | 2019-04-30 + year-month | 2019 | 5 | 2019-04-30 | 2019-05-01 | 2019-05-31 + year-month | 2019 | 6 | 2019-05-31 | 2019-06-01 | 2019-06-30 + year-month | 2019 | 7 | 2019-06-30 | 2019-07-01 | 2019-07-31 + year-month | 2019 | 8 | 2019-07-31 | 2019-08-01 | 2019-08-31 + year-month | 2019 | 9 | 2019-08-31 | 2019-09-01 | 2019-09-30 + year-month | 2019 | 10 | 2019-09-30 | 2019-10-01 | 2019-10-31 + year-month | 2019 | 11 | 2019-10-31 | 2019-11-01 | 2019-11-30 + year-month | 2019 | 12 | 2019-11-30 | 2019-12-01 | 2019-12-31 + year-month | 2020 | 1 | 2019-12-31 | 2020-01-01 | 2020-01-31 + year-month | 2020 | 2 | 2020-01-31 | 2020-02-01 | 2020-02-29 + year-month | 2020 | 3 | 2020-02-29 | 2020-03-01 | 2020-03-31 + year-month | 2020 | 4 | 2020-03-31 | 2020-04-01 | 2020-04-30 + year-month | 2020 | 5 | 2020-04-30 | 2020-05-01 | 2020-05-31 + year-month | 2020 | 6 | 2020-05-31 | 2020-06-01 | 2020-06-30 + year-month | 2020 | 7 | 2020-06-30 | 2020-07-01 | 2020-07-31 + year-month | 2020 | 8 | 2020-07-31 | 2020-08-01 | 2020-08-31 + year-month | 2020 | 9 | 2020-08-31 | 2020-09-01 | 2020-09-30 + year-month | 2020 | 10 | 2020-09-30 | 2020-10-01 | 2020-10-31 + year-month | 2020 | 11 | 2020-10-31 | 2020-11-01 | 2020-11-30 + year-month | 2020 | 12 | 2020-11-30 | 2020-12-01 | 2020-12-31 + year-month | 2021 | 1 | 2020-12-31 | 2021-01-01 | 2021-01-31 + year-month | 2021 | 2 | 2021-01-31 | 2021-02-01 | 2021-02-28 + year-month | 2021 | 3 | 2021-02-28 | 2021-03-01 | 2021-03-31 + year-month | 2021 | 4 | 2021-03-31 | 2021-04-01 | 2021-04-30 + year-month | 2021 | 5 | 2021-04-30 | 2021-05-01 | 2021-05-31 + year-month | 2021 | 6 | 2021-05-31 | 2021-06-01 | 2021-06-30 + year-month | 2021 | 7 | 2021-06-30 | 2021-07-01 | 2021-07-31 + year-month | 2021 | 8 | 2021-07-31 | 2021-08-01 | 2021-08-31 + year-month | 2021 | 9 | 2021-08-31 | 2021-09-01 | 2021-09-30 + year-month | 2021 | 10 | 2021-09-30 | 2021-10-01 | 2021-10-31 + year-month | 2021 | 11 | 2021-10-31 | 2021-11-01 | 2021-11-30 + year-month | 2021 | 12 | 2021-11-30 | 2021-12-01 | 2021-12-31 + year-month | 2022 | 1 | 2021-12-31 | 2022-01-01 | 2022-01-31 + year-month | 2022 | 2 | 2022-01-31 | 2022-02-01 | 2022-02-28 + year-month | 2022 | 3 | 2022-02-28 | 2022-03-01 | 2022-03-31 + year-month | 2022 | 4 | 2022-03-31 | 2022-04-01 | 2022-04-30 + year-month | 2022 | 5 | 2022-04-30 | 2022-05-01 | 2022-05-31 + year-month | 2022 | 6 | 2022-05-31 | 2022-06-01 | 2022-06-30 + year-month | 2022 | 7 | 2022-06-30 | 2022-07-01 | 2022-07-31 + year-month | 2022 | 8 | 2022-07-31 | 2022-08-01 | 2022-08-31 + year-month | 2022 | 9 | 2022-08-31 | 2022-09-01 | 2022-09-30 + year-month | 2022 | 10 | 2022-09-30 | 2022-10-01 | 2022-10-31 + year-month | 2022 | 11 | 2022-10-31 | 2022-11-01 | 2022-11-30 + year-month | 2022 | 12 | 2022-11-30 | 2022-12-01 | 2022-12-31 + year-month | 2023 | 1 | 2022-12-31 | 2023-01-01 | 2023-01-31 + year-month | 2023 | 2 | 2023-01-31 | 2023-02-01 | 2023-02-28 + year-month | 2023 | 3 | 2023-02-28 | 2023-03-01 | 2023-03-31 + year-month | 2023 | 4 | 2023-03-31 | 2023-04-01 | 2023-04-30 + year-month | 2023 | 5 | 2023-04-30 | 2023-05-01 | 2023-05-31 + year-month | 2023 | 6 | 2023-05-31 | 2023-06-01 | 2023-06-30 + year-month | 2023 | 7 | 2023-06-30 | 2023-07-01 | 2023-07-31 + year-month | 2023 | 8 | 2023-07-31 | 2023-08-01 | 2023-08-31 + year-month | 2023 | 9 | 2023-08-31 | 2023-09-01 | 2023-09-30 + year-month | 2023 | 10 | 2023-09-30 | 2023-10-01 | 2023-10-31 + year-month | 2023 | 11 | 2023-10-31 | 2023-11-01 | 2023-11-30 + year-month | 2023 | 12 | 2023-11-30 | 2023-12-01 | 2023-12-31 (182 rows) \echo "Test yearly data" @@ -3726,8 +3726,8 @@ ORDER BY year,unit_type; 2011 | legal_unit | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 2011 | enterprise | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 2012 | establishment | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 - 2012 | legal_unit | 1 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 - 2012 | enterprise | 1 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 + 2012 | legal_unit | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 + 2012 | enterprise | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 2013 | establishment | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 2013 | legal_unit | 1 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 2013 | enterprise | 1 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 @@ -7017,8 +7017,8 @@ ORDER BY year,unit_type; 2011 | legal_unit | H.49.4.1.0 | | innl.a_ikke_fin.2100 | 11.21 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 2011 | enterprise | H.49.4.1.0 | | innl.a_ikke_fin.2100 | 11.21 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 2012 | establishment | H.49.4.1.0 | | | 11.21 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 - 2012 | legal_unit | H.49.4.1.0 | | innl.a_ikke_fin.2100 | 11.21 | 1 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 - 2012 | enterprise | H.49.4.1.0 | | innl.a_ikke_fin.2100 | 11.21 | 1 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 + 2012 | legal_unit | H.49.4.1.0 | | innl.a_ikke_fin.2100 | 11.21 | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 + 2012 | enterprise | H.49.4.1.0 | | innl.a_ikke_fin.2100 | 11.21 | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 2013 | establishment | H.49.4.1.0 | | | 11.21 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 2013 | legal_unit | H.49.4.1.0 | | innl.a_ikke_fin.2100 | 11.21 | 1 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 2013 | enterprise | H.49.4.1.0 | | innl.a_ikke_fin.2100 | 11.21 | 1 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 @@ -9343,7 +9343,7 @@ statistical_history_drilldown }, { "year": 2012, - "count": 1, + "count": 0, "month": null, "births": 0, "deaths": 1, @@ -9850,7 +9850,7 @@ statistical_history_drilldown "code": "11", "name": "Rogaland", "path": "11", - "count": 15, + "count": 14, "label": "11", "has_children": true } @@ -9860,7 +9860,7 @@ statistical_history_drilldown "code": null, "name": "Innenlandske sektorer", "path": "innl", - "count": 15, + "count": 14, "label": "innl", "has_children": true } @@ -9868,7 +9868,7 @@ statistical_history_drilldown "country": [ { "name": "Norway", - "count": 15, + "count": 14, "iso_2": "NO", "has_children": false } @@ -9883,7 +9883,7 @@ statistical_history_drilldown { "code": "ENK", "name": "Enkeltpersonforetak", - "count": 3, + "count": 2, "has_children": false } ], @@ -9892,7 +9892,7 @@ statistical_history_drilldown "code": "", "name": "Transport og lagring", "path": "H", - "count": 15, + "count": 14, "label": "H", "has_children": true } @@ -10003,7 +10003,7 @@ statistical_history_drilldown }, { "year": 2012, - "count": 1, + "count": 0, "month": null, "births": 0, "deaths": 1, @@ -10510,7 +10510,7 @@ statistical_history_drilldown "code": "11", "name": "Rogaland", "path": "11", - "count": 15, + "count": 14, "label": "11", "has_children": true } @@ -10520,7 +10520,7 @@ statistical_history_drilldown "code": null, "name": "Innenlandske sektorer", "path": "innl", - "count": 15, + "count": 14, "label": "innl", "has_children": true } @@ -10528,7 +10528,7 @@ statistical_history_drilldown "country": [ { "name": "Norway", - "count": 15, + "count": 14, "iso_2": "NO", "has_children": false } @@ -10543,7 +10543,7 @@ statistical_history_drilldown { "code": "ENK", "name": "Enkeltpersonforetak", - "count": 3, + "count": 2, "has_children": false } ], @@ -10552,7 +10552,7 @@ statistical_history_drilldown "code": "", "name": "Transport og lagring", "path": "H", - "count": 15, + "count": 14, "label": "H", "has_children": true } @@ -12259,7 +12259,7 @@ statistical_unit_hierarchy "name": "Number of people employed", "type": "int", "archived": false, - "priority": 2, + "priority": 1, "frequency": "yearly", "description": "The number of people receiving an official salary with government reporting." } @@ -12274,7 +12274,7 @@ statistical_unit_hierarchy "name": "Turnover", "type": "int", "archived": false, - "priority": 3, + "priority": 2, "frequency": "yearly", "description": "The amount (EUR)" } @@ -12497,7 +12497,7 @@ statistical_unit_hierarchy "name": "Number of people employed", "type": "int", "archived": false, - "priority": 2, + "priority": 1, "frequency": "yearly", "description": "The number of people receiving an official salary with government reporting." } @@ -12512,7 +12512,7 @@ statistical_unit_hierarchy "name": "Turnover", "type": "int", "archived": false, - "priority": 3, + "priority": 2, "frequency": "yearly", "description": "The amount (EUR)" } @@ -12734,7 +12734,7 @@ statistical_unit_hierarchy "name": "Number of people employed", "type": "int", "archived": false, - "priority": 2, + "priority": 1, "frequency": "yearly", "description": "The number of people receiving an official salary with government reporting." } @@ -12749,7 +12749,7 @@ statistical_unit_hierarchy "name": "Turnover", "type": "int", "archived": false, - "priority": 3, + "priority": 2, "frequency": "yearly", "description": "The amount (EUR)" } diff --git a/test/expected/05_modify_enterprise_connections.out b/test/expected/05_modify_enterprise_connections.out index b75405c92..0d0b9e508 100644 --- a/test/expected/05_modify_enterprise_connections.out +++ b/test/expected/05_modify_enterprise_connections.out @@ -297,7 +297,7 @@ SELECT jsonb_pretty( "name": "Number of people employed", + "type": "int", + "archived": false, + - "priority": 2, + + "priority": 1, + "frequency": "yearly", + "description": "The number of people receiving an official salary with government reporting." + } + @@ -312,7 +312,7 @@ SELECT jsonb_pretty( "name": "Turnover", + "type": "int", + "archived": false, + - "priority": 3, + + "priority": 2, + "frequency": "yearly", + "description": "The amount (EUR)" + } + @@ -543,7 +543,7 @@ SELECT jsonb_pretty( "name": "Number of people employed", + "type": "int", + "archived": false, + - "priority": 2, + + "priority": 1, + "frequency": "yearly", + "description": "The number of people receiving an official salary with government reporting." + } + @@ -558,7 +558,7 @@ SELECT jsonb_pretty( "name": "Turnover", + "type": "int", + "archived": false, + - "priority": 3, + + "priority": 2, + "frequency": "yearly", + "description": "The amount (EUR)" + } + @@ -658,7 +658,7 @@ SELECT jsonb_pretty( "name": "Number of people employed", + "type": "int", + "archived": false, + - "priority": 2, + + "priority": 1, + "frequency": "yearly", + "description": "The number of people receiving an official salary with government reporting." + } + @@ -673,7 +673,7 @@ SELECT jsonb_pretty( "name": "Turnover", + "type": "int", + "archived": false, + - "priority": 3, + + "priority": 2, + "frequency": "yearly", + "description": "The amount (EUR)" + } + @@ -1015,7 +1015,7 @@ SELECT jsonb_pretty( "name": "Number of people employed", + "type": "int", + "archived": false, + - "priority": 2, + + "priority": 1, + "frequency": "yearly", + "description": "The number of people receiving an official salary with government reporting." + } + @@ -1030,7 +1030,7 @@ SELECT jsonb_pretty( "name": "Turnover", + "type": "int", + "archived": false, + - "priority": 3, + + "priority": 2, + "frequency": "yearly", + "description": "The amount (EUR)" + } + @@ -1230,7 +1230,7 @@ SELECT jsonb_pretty( "name": "Number of people employed", + "type": "int", + "archived": false, + - "priority": 2, + + "priority": 1, + "frequency": "yearly", + "description": "The number of people receiving an official salary with government reporting." + } + @@ -1245,7 +1245,7 @@ SELECT jsonb_pretty( "name": "Turnover", + "type": "int", + "archived": false, + - "priority": 3, + + "priority": 2, + "frequency": "yearly", + "description": "The amount (EUR)" + } + @@ -1345,7 +1345,7 @@ SELECT jsonb_pretty( "name": "Number of people employed", + "type": "int", + "archived": false, + - "priority": 2, + + "priority": 1, + "frequency": "yearly", + "description": "The number of people receiving an official salary with government reporting." + } + @@ -1360,7 +1360,7 @@ SELECT jsonb_pretty( "name": "Turnover", + "type": "int", + "archived": false, + - "priority": 3, + + "priority": 2, + "frequency": "yearly", + "description": "The amount (EUR)" + } + diff --git a/test/expected/40_history_legal_units_changes_over_time.out b/test/expected/40_history_legal_units_changes_over_time.out index e69de29bb..de9475325 100644 --- a/test/expected/40_history_legal_units_changes_over_time.out +++ b/test/expected/40_history_legal_units_changes_over_time.out @@ -0,0 +1,392 @@ +SET datestyle TO 'ISO, DMY'; +BEGIN; +\echo "Setting up Statbus to test enterprise grouping and primary" +"Setting up Statbus to test enterprise grouping and primary" +\echo "User selected the Activity Category Standard" +"User selected the Activity Category Standard" +INSERT INTO settings(activity_category_standard_id,only_one_setting) +SELECT id, true FROM activity_category_standard WHERE code = 'nace_v2.1' +ON CONFLICT (only_one_setting) +DO UPDATE SET + activity_category_standard_id =(SELECT id FROM activity_category_standard WHERE code = 'nace_v2.1') + WHERE settings.id = EXCLUDED.id; +; +SELECT acs.code + FROM public.settings AS s + JOIN activity_category_standard AS acs + ON s.activity_category_standard_id = acs.id; + code +----------- + nace_v2.1 +(1 row) + +\echo "User uploads the sample activity categories" +"User uploads the sample activity categories" +\copy public.activity_category_available_custom(path,name,description) FROM 'app/public/activity_category_norway.csv' WITH (FORMAT csv, DELIMITER ',', QUOTE '"', HEADER true); +SELECT count(*) FROM public.activity_category_available; + count +------- + 2215 +(1 row) + +\echo "User uploads the sample regions" +"User uploads the sample regions" +\copy public.region_upload(path, name) FROM 'app/public/norway-regions-2024.csv' WITH (FORMAT csv, DELIMITER ',', QUOTE '"', HEADER true); +SELECT count(*) FROM public.region; + count +------- + 417 +(1 row) + +\echo "User uploads the sample legal forms" +"User uploads the sample legal forms" +\copy public.legal_form_custom_only(code,name) FROM 'app/public/legal_form_norway.csv' WITH (FORMAT csv, DELIMITER ',', QUOTE '"', HEADER true); +SELECT count(*) FROM public.legal_form_available; + count +------- + 46 +(1 row) + +\echo "User uploads the sample sectors" +"User uploads the sample sectors" +\copy public.sector_custom_only(path,name,description) FROM 'app/public/sector_norway.csv' WITH (FORMAT csv, DELIMITER ',', QUOTE '"', HEADER true); +SELECT count(*) FROM public.sector_available; + count +------- + 33 +(1 row) + +SAVEPOINT before_loading_units; +\echo "Test sector changes in the middle of a month" +"Test sector changes in the middle of a month" +SELECT + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.establishment) AS establishment_count, + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.legal_unit) AS legal_unit_count, + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.enterprise) AS enterprise_count; + establishment_count | legal_unit_count | enterprise_count +---------------------+------------------+------------------ + 0 | 0 | 0 +(1 row) + +\echo "User uploads the legal units" +"User uploads the legal units" +\copy public.import_legal_unit_era(valid_from,valid_to,tax_ident,name,birth_date,death_date,physical_address_part1,physical_postal_code,physical_postal_place,physical_region_code,physical_country_iso_2,postal_address_part1,postal_postal_code,postal_postal_place,postal_region_code,postal_country_iso_2,primary_activity_category_code,secondary_activity_category_code,sector_code,legal_form_code) FROM 'test/data/40_legal-units-sector-change-middle-of-month.csv' WITH (FORMAT csv, DELIMITER ',', QUOTE '"', HEADER true); +SELECT + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.establishment) AS establishment_count, + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.legal_unit) AS legal_unit_count, + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.enterprise) AS enterprise_count; + establishment_count | legal_unit_count | enterprise_count +---------------------+------------------+------------------ + 0 | 1 | 1 +(1 row) + +\echo "Refreshing materialized views" +"Refreshing materialized views" +-- Exclude the refresh_time_ms as it will vary. +SELECT view_name FROM statistical_unit_refresh_now(); + view_name +--------------------------- + statistical_unit + activity_category_used + region_used + sector_used + legal_form_used + country_used + statistical_unit_facet + statistical_history + statistical_history_facet +(9 rows) + +\echo "Check sector for legal units over time" +"Check sector for legal units over time" +SELECT external_idents ->> 'tax_ident' as tax_ident, name, valid_after, valid_from, valid_to, sector_code +FROM public.statistical_unit +WHERE unit_type = 'legal_unit'; + tax_ident | name | valid_after | valid_from | valid_to | sector_code +-----------+-------------------+-------------+------------+------------+------------- + 823573673 | Kranløft Vestland | 2009-12-31 | 2010-01-01 | 2011-01-14 | 2100 + 823573673 | Kranløft Vestland | 2011-01-14 | 2011-01-15 | infinity | 8200 +(2 rows) + +\echo "Test statistical unit history by year - sector_change_count should be 1 for year 2011" +"Test statistical unit history by year - sector_change_count should be 1 for year 2011" +SELECT resolution, year, unit_type, count, births, deaths, sector_change_count +FROM public.statistical_history +WHERE resolution = 'year' +AND year < 2013 +AND unit_type = 'legal_unit'; + resolution | year | unit_type | count | births | deaths | sector_change_count +------------+------+------------+-------+--------+--------+--------------------- + year | 2010 | legal_unit | 1 | 1 | 0 | 0 + year | 2011 | legal_unit | 1 | 0 | 0 | 1 + year | 2012 | legal_unit | 1 | 0 | 0 | 0 +(3 rows) + +\echo "Test statistical unit history by year-month - sector_change_count should be 1 for year-month 2011-1" +"Test statistical unit history by year-month - sector_change_count should be 1 for year-month 2011-1" +SELECT resolution, year, month, unit_type, count, births, deaths, sector_change_count +FROM public.statistical_history +WHERE resolution = 'year-month' +AND year < 2013 +AND unit_type = 'legal_unit'; + resolution | year | month | unit_type | count | births | deaths | sector_change_count +------------+------+-------+------------+-------+--------+--------+--------------------- + year-month | 2010 | 1 | legal_unit | 1 | 1 | 0 | 0 + year-month | 2010 | 2 | legal_unit | 1 | 0 | 0 | 0 + year-month | 2010 | 3 | legal_unit | 1 | 0 | 0 | 0 + year-month | 2010 | 4 | legal_unit | 1 | 0 | 0 | 0 + year-month | 2010 | 5 | legal_unit | 1 | 0 | 0 | 0 + year-month | 2010 | 6 | legal_unit | 1 | 0 | 0 | 0 + year-month | 2010 | 7 | legal_unit | 1 | 0 | 0 | 0 + year-month | 2010 | 8 | legal_unit | 1 | 0 | 0 | 0 + year-month | 2010 | 9 | legal_unit | 1 | 0 | 0 | 0 + year-month | 2010 | 10 | legal_unit | 1 | 0 | 0 | 0 + year-month | 2010 | 11 | legal_unit | 1 | 0 | 0 | 0 + year-month | 2010 | 12 | legal_unit | 1 | 0 | 0 | 0 + year-month | 2011 | 1 | legal_unit | 1 | 0 | 0 | 1 + year-month | 2011 | 2 | legal_unit | 1 | 0 | 0 | 0 + year-month | 2011 | 3 | legal_unit | 1 | 0 | 0 | 0 + year-month | 2011 | 4 | legal_unit | 1 | 0 | 0 | 0 + year-month | 2011 | 5 | legal_unit | 1 | 0 | 0 | 0 + year-month | 2011 | 6 | legal_unit | 1 | 0 | 0 | 0 + year-month | 2011 | 7 | legal_unit | 1 | 0 | 0 | 0 + year-month | 2011 | 8 | legal_unit | 1 | 0 | 0 | 0 + year-month | 2011 | 9 | legal_unit | 1 | 0 | 0 | 0 + year-month | 2011 | 10 | legal_unit | 1 | 0 | 0 | 0 + year-month | 2011 | 11 | legal_unit | 1 | 0 | 0 | 0 + year-month | 2011 | 12 | legal_unit | 1 | 0 | 0 | 0 + year-month | 2012 | 1 | legal_unit | 1 | 0 | 0 | 0 + year-month | 2012 | 2 | legal_unit | 1 | 0 | 0 | 0 + year-month | 2012 | 3 | legal_unit | 1 | 0 | 0 | 0 + year-month | 2012 | 4 | legal_unit | 1 | 0 | 0 | 0 + year-month | 2012 | 5 | legal_unit | 1 | 0 | 0 | 0 + year-month | 2012 | 6 | legal_unit | 1 | 0 | 0 | 0 + year-month | 2012 | 7 | legal_unit | 1 | 0 | 0 | 0 + year-month | 2012 | 8 | legal_unit | 1 | 0 | 0 | 0 + year-month | 2012 | 9 | legal_unit | 1 | 0 | 0 | 0 + year-month | 2012 | 10 | legal_unit | 1 | 0 | 0 | 0 + year-month | 2012 | 11 | legal_unit | 1 | 0 | 0 | 0 + year-month | 2012 | 12 | legal_unit | 1 | 0 | 0 | 0 +(36 rows) + +\x +ROLLBACK TO before_loading_units; +\echo "Test sector changes at the start of the second month" +"Test sector changes at the start of the second month" +\x +SELECT + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.establishment) AS establishment_count, + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.legal_unit) AS legal_unit_count, + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.enterprise) AS enterprise_count; + establishment_count | legal_unit_count | enterprise_count +---------------------+------------------+------------------ + 0 | 0 | 0 +(1 row) + +\echo "User uploads the legal units" +"User uploads the legal units" +\copy public.import_legal_unit_era(valid_from,valid_to,tax_ident,name,birth_date,death_date,physical_address_part1,physical_postal_code,physical_postal_place,physical_region_code,physical_country_iso_2,postal_address_part1,postal_postal_code,postal_postal_place,postal_region_code,postal_country_iso_2,primary_activity_category_code,secondary_activity_category_code,sector_code,legal_form_code) FROM 'test/data/40_legal-units-sector-change-start-of-second-month.csv' WITH (FORMAT csv, DELIMITER ',', QUOTE '"', HEADER true); +SELECT + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.establishment) AS establishment_count, + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.legal_unit) AS legal_unit_count, + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.enterprise) AS enterprise_count; + establishment_count | legal_unit_count | enterprise_count +---------------------+------------------+------------------ + 0 | 1 | 1 +(1 row) + +\echo "Refreshing materialized views" +"Refreshing materialized views" +-- Exclude the refresh_time_ms as it will vary. +SELECT view_name FROM statistical_unit_refresh_now(); + view_name +--------------------------- + statistical_unit + activity_category_used + region_used + sector_used + legal_form_used + country_used + statistical_unit_facet + statistical_history + statistical_history_facet +(9 rows) + +\echo "Check sector for legal units over time" +"Check sector for legal units over time" +SELECT external_idents ->> 'tax_ident' as tax_ident, name, valid_after, valid_from, valid_to, sector_code +FROM public.statistical_unit +WHERE unit_type = 'legal_unit'; + tax_ident | name | valid_after | valid_from | valid_to | sector_code +-----------+-------------------+-------------+------------+------------+------------- + 823573673 | Kranløft Vestland | 2009-12-31 | 2010-01-01 | 2011-01-31 | 2100 + 823573673 | Kranløft Vestland | 2011-01-31 | 2011-02-01 | infinity | 8200 +(2 rows) + +\echo "Check statistical unit history by year - sector_change_count should be 1 for year 2011" +"Check statistical unit history by year - sector_change_count should be 1 for year 2011" +SELECT resolution, year,month, unit_type, count, births, deaths, sector_change_count +FROM public.statistical_history +WHERE resolution = 'year' +AND year < 2012 +AND unit_type = 'legal_unit'; + resolution | year | month | unit_type | count | births | deaths | sector_change_count +------------+------+-------+------------+-------+--------+--------+--------------------- + year | 2010 | | legal_unit | 1 | 1 | 0 | 0 + year | 2011 | | legal_unit | 1 | 0 | 0 | 1 +(2 rows) + +\echo "Check statistical unit history by year-month - sector_change_count should be 1 for year-month 2011-2" +"Check statistical unit history by year-month - sector_change_count should be 1 for year-month 2011-2" +SELECT resolution, year, month, unit_type, count, births, deaths, sector_change_count +FROM public.statistical_history +WHERE resolution = 'year-month' +AND year < 2012 +AND unit_type = 'legal_unit'; + resolution | year | month | unit_type | count | births | deaths | sector_change_count +------------+------+-------+------------+-------+--------+--------+--------------------- + year-month | 2010 | 1 | legal_unit | 1 | 1 | 0 | 0 + year-month | 2010 | 2 | legal_unit | 1 | 0 | 0 | 0 + year-month | 2010 | 3 | legal_unit | 1 | 0 | 0 | 0 + year-month | 2010 | 4 | legal_unit | 1 | 0 | 0 | 0 + year-month | 2010 | 5 | legal_unit | 1 | 0 | 0 | 0 + year-month | 2010 | 6 | legal_unit | 1 | 0 | 0 | 0 + year-month | 2010 | 7 | legal_unit | 1 | 0 | 0 | 0 + year-month | 2010 | 8 | legal_unit | 1 | 0 | 0 | 0 + year-month | 2010 | 9 | legal_unit | 1 | 0 | 0 | 0 + year-month | 2010 | 10 | legal_unit | 1 | 0 | 0 | 0 + year-month | 2010 | 11 | legal_unit | 1 | 0 | 0 | 0 + year-month | 2010 | 12 | legal_unit | 1 | 0 | 0 | 0 + year-month | 2011 | 1 | legal_unit | 1 | 0 | 0 | 0 + year-month | 2011 | 2 | legal_unit | 1 | 0 | 0 | 1 + year-month | 2011 | 3 | legal_unit | 1 | 0 | 0 | 0 + year-month | 2011 | 4 | legal_unit | 1 | 0 | 0 | 0 + year-month | 2011 | 5 | legal_unit | 1 | 0 | 0 | 0 + year-month | 2011 | 6 | legal_unit | 1 | 0 | 0 | 0 + year-month | 2011 | 7 | legal_unit | 1 | 0 | 0 | 0 + year-month | 2011 | 8 | legal_unit | 1 | 0 | 0 | 0 + year-month | 2011 | 9 | legal_unit | 1 | 0 | 0 | 0 + year-month | 2011 | 10 | legal_unit | 1 | 0 | 0 | 0 + year-month | 2011 | 11 | legal_unit | 1 | 0 | 0 | 0 + year-month | 2011 | 12 | legal_unit | 1 | 0 | 0 | 0 +(24 rows) + +\x +ROLLBACK TO before_loading_units; +\echo "Test sector changes at the start of the year" +"Test sector changes at the start of the year" +\x +SELECT + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.establishment) AS establishment_count, + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.legal_unit) AS legal_unit_count, + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.enterprise) AS enterprise_count; + establishment_count | legal_unit_count | enterprise_count +---------------------+------------------+------------------ + 0 | 0 | 0 +(1 row) + +\echo "User uploads the legal units" +"User uploads the legal units" +\copy public.import_legal_unit_era(valid_from,valid_to,tax_ident,name,birth_date,death_date,physical_address_part1,physical_postal_code,physical_postal_place,physical_region_code,physical_country_iso_2,postal_address_part1,postal_postal_code,postal_postal_place,postal_region_code,postal_country_iso_2,primary_activity_category_code,secondary_activity_category_code,sector_code,legal_form_code) FROM 'test/data/40_legal-units-sector-change-start-of-year.csv' WITH (FORMAT csv, DELIMITER ',', QUOTE '"', HEADER true); +SELECT + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.establishment) AS establishment_count, + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.legal_unit) AS legal_unit_count, + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.enterprise) AS enterprise_count; + establishment_count | legal_unit_count | enterprise_count +---------------------+------------------+------------------ + 0 | 1 | 1 +(1 row) + +\echo "Refreshing materialized views" +"Refreshing materialized views" +-- Exclude the refresh_time_ms as it will vary. +SELECT view_name FROM statistical_unit_refresh_now(); + view_name +--------------------------- + statistical_unit + activity_category_used + region_used + sector_used + legal_form_used + country_used + statistical_unit_facet + statistical_history + statistical_history_facet +(9 rows) + +\echo "Check sector for legal units over time" +"Check sector for legal units over time" +SELECT external_idents ->> 'tax_ident' as tax_ident, name, valid_after, valid_from, valid_to, sector_code +FROM public.statistical_unit +WHERE unit_type = 'legal_unit'; + tax_ident | name | valid_after | valid_from | valid_to | sector_code +-----------+-------------------+-------------+------------+------------+------------- + 823573673 | Kranløft Vestland | 2009-12-31 | 2010-01-01 | 2010-12-31 | 2100 + 823573673 | Kranløft Vestland | 2010-12-31 | 2011-01-01 | 2011-12-31 | 7000 + 823573673 | Kranløft Vestland | 2011-12-31 | 2012-01-01 | infinity | 8200 +(3 rows) + +\echo "Check statistical unit history by year - sector_change_count should be 1 for year 2011 and 2012" +"Check statistical unit history by year - sector_change_count should be 1 for year 2011 and 2012" +SELECT resolution, year,month, unit_type, count, births, deaths, sector_change_count +FROM public.statistical_history +WHERE resolution = 'year' +AND year < 2014 +AND unit_type = 'legal_unit'; + resolution | year | month | unit_type | count | births | deaths | sector_change_count +------------+------+-------+------------+-------+--------+--------+--------------------- + year | 2010 | | legal_unit | 1 | 1 | 0 | 0 + year | 2011 | | legal_unit | 1 | 0 | 0 | 1 + year | 2012 | | legal_unit | 1 | 0 | 0 | 1 + year | 2013 | | legal_unit | 1 | 0 | 0 | 0 +(4 rows) + +\echo "Check statistical unit history by year-month - sector_change_count should be 1 for year-month 2011-1 and 2012-1" +"Check statistical unit history by year-month - sector_change_count should be 1 for year-month 2011-1 and 2012-1" +SELECT resolution, year, month, unit_type, count, births, deaths, sector_change_count +FROM public.statistical_history +WHERE resolution = 'year-month' +AND year < 2013 +AND unit_type = 'legal_unit'; + resolution | year | month | unit_type | count | births | deaths | sector_change_count +------------+------+-------+------------+-------+--------+--------+--------------------- + year-month | 2010 | 1 | legal_unit | 1 | 1 | 0 | 0 + year-month | 2010 | 2 | legal_unit | 1 | 0 | 0 | 0 + year-month | 2010 | 3 | legal_unit | 1 | 0 | 0 | 0 + year-month | 2010 | 4 | legal_unit | 1 | 0 | 0 | 0 + year-month | 2010 | 5 | legal_unit | 1 | 0 | 0 | 0 + year-month | 2010 | 6 | legal_unit | 1 | 0 | 0 | 0 + year-month | 2010 | 7 | legal_unit | 1 | 0 | 0 | 0 + year-month | 2010 | 8 | legal_unit | 1 | 0 | 0 | 0 + year-month | 2010 | 9 | legal_unit | 1 | 0 | 0 | 0 + year-month | 2010 | 10 | legal_unit | 1 | 0 | 0 | 0 + year-month | 2010 | 11 | legal_unit | 1 | 0 | 0 | 0 + year-month | 2010 | 12 | legal_unit | 1 | 0 | 0 | 0 + year-month | 2011 | 1 | legal_unit | 1 | 0 | 0 | 1 + year-month | 2011 | 2 | legal_unit | 1 | 0 | 0 | 0 + year-month | 2011 | 3 | legal_unit | 1 | 0 | 0 | 0 + year-month | 2011 | 4 | legal_unit | 1 | 0 | 0 | 0 + year-month | 2011 | 5 | legal_unit | 1 | 0 | 0 | 0 + year-month | 2011 | 6 | legal_unit | 1 | 0 | 0 | 0 + year-month | 2011 | 7 | legal_unit | 1 | 0 | 0 | 0 + year-month | 2011 | 8 | legal_unit | 1 | 0 | 0 | 0 + year-month | 2011 | 9 | legal_unit | 1 | 0 | 0 | 0 + year-month | 2011 | 10 | legal_unit | 1 | 0 | 0 | 0 + year-month | 2011 | 11 | legal_unit | 1 | 0 | 0 | 0 + year-month | 2011 | 12 | legal_unit | 1 | 0 | 0 | 0 + year-month | 2012 | 1 | legal_unit | 1 | 0 | 0 | 1 + year-month | 2012 | 2 | legal_unit | 1 | 0 | 0 | 0 + year-month | 2012 | 3 | legal_unit | 1 | 0 | 0 | 0 + year-month | 2012 | 4 | legal_unit | 1 | 0 | 0 | 0 + year-month | 2012 | 5 | legal_unit | 1 | 0 | 0 | 0 + year-month | 2012 | 6 | legal_unit | 1 | 0 | 0 | 0 + year-month | 2012 | 7 | legal_unit | 1 | 0 | 0 | 0 + year-month | 2012 | 8 | legal_unit | 1 | 0 | 0 | 0 + year-month | 2012 | 9 | legal_unit | 1 | 0 | 0 | 0 + year-month | 2012 | 10 | legal_unit | 1 | 0 | 0 | 0 + year-month | 2012 | 11 | legal_unit | 1 | 0 | 0 | 0 + year-month | 2012 | 12 | legal_unit | 1 | 0 | 0 | 0 +(36 rows) + +\x +ROLLBACK; diff --git a/test/expected/41_history_legal_units_births.out b/test/expected/41_history_legal_units_births.out index e69de29bb..196fe67d4 100644 --- a/test/expected/41_history_legal_units_births.out +++ b/test/expected/41_history_legal_units_births.out @@ -0,0 +1,402 @@ +SET datestyle TO 'ISO, DMY'; +BEGIN; +\echo "Setting up Statbus to test enterprise grouping and primary" +"Setting up Statbus to test enterprise grouping and primary" +\echo "User selected the Activity Category Standard" +"User selected the Activity Category Standard" +INSERT INTO settings(activity_category_standard_id,only_one_setting) +SELECT id, true FROM activity_category_standard WHERE code = 'nace_v2.1' +ON CONFLICT (only_one_setting) +DO UPDATE SET + activity_category_standard_id =(SELECT id FROM activity_category_standard WHERE code = 'nace_v2.1') + WHERE settings.id = EXCLUDED.id; +; +SELECT acs.code + FROM public.settings AS s + JOIN activity_category_standard AS acs + ON s.activity_category_standard_id = acs.id; + code +----------- + nace_v2.1 +(1 row) + +\echo "User uploads the sample activity categories" +"User uploads the sample activity categories" +\copy public.activity_category_available_custom(path,name,description) FROM 'app/public/activity_category_norway.csv' WITH (FORMAT csv, DELIMITER ',', QUOTE '"', HEADER true); +SELECT count(*) FROM public.activity_category_available; + count +------- + 2215 +(1 row) + +\echo "User uploads the sample regions" +"User uploads the sample regions" +\copy public.region_upload(path, name) FROM 'app/public/norway-regions-2024.csv' WITH (FORMAT csv, DELIMITER ',', QUOTE '"', HEADER true); +SELECT count(*) FROM public.region; + count +------- + 417 +(1 row) + +\echo "User uploads the sample legal forms" +"User uploads the sample legal forms" +\copy public.legal_form_custom_only(code,name) FROM 'app/public/legal_form_norway.csv' WITH (FORMAT csv, DELIMITER ',', QUOTE '"', HEADER true); +SELECT count(*) FROM public.legal_form_available; + count +------- + 46 +(1 row) + +\echo "User uploads the sample sectors" +"User uploads the sample sectors" +\copy public.sector_custom_only(path,name,description) FROM 'app/public/sector_norway.csv' WITH (FORMAT csv, DELIMITER ',', QUOTE '"', HEADER true); +SELECT count(*) FROM public.sector_available; + count +------- + 33 +(1 row) + +SAVEPOINT before_loading_units; +\echo "Test births at the start of the year" +"Test births at the start of the year" +SELECT + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.establishment) AS establishment_count, + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.legal_unit) AS legal_unit_count, + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.enterprise) AS enterprise_count; + establishment_count | legal_unit_count | enterprise_count +---------------------+------------------+------------------ + 0 | 0 | 0 +(1 row) + +\echo "User uploads the legal units" +"User uploads the legal units" +\copy public.import_legal_unit_era(valid_from,valid_to,tax_ident,name,birth_date,death_date,physical_address_part1,physical_postal_code,physical_postal_place,physical_region_code,physical_country_iso_2,postal_address_part1,postal_postal_code,postal_postal_place,postal_region_code,postal_country_iso_2,primary_activity_category_code,secondary_activity_category_code,sector_code,legal_form_code) FROM 'test/data/41_legal-units-births-start-of-year.csv' WITH (FORMAT csv, DELIMITER ',', QUOTE '"', HEADER true); +SELECT + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.establishment) AS establishment_count, + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.legal_unit) AS legal_unit_count, + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.enterprise) AS enterprise_count; + establishment_count | legal_unit_count | enterprise_count +---------------------+------------------+------------------ + 0 | 2 | 2 +(1 row) + +\echo "Refreshing materialized views" +"Refreshing materialized views" +-- Exclude the refresh_time_ms as it will vary. +SELECT view_name FROM statistical_unit_refresh_now(); + view_name +--------------------------- + statistical_unit + activity_category_used + region_used + sector_used + legal_form_used + country_used + statistical_unit_facet + statistical_history + statistical_history_facet +(9 rows) + +\echo "Check legal units over time" +"Check legal units over time" +SELECT external_idents ->> 'tax_ident' as tax_ident, name, valid_after, valid_from, valid_to, birth_date, death_date +FROM public.statistical_unit +WHERE unit_type = 'legal_unit'; + tax_ident | name | valid_after | valid_from | valid_to | birth_date | death_date +-----------+-------------------+-------------+------------+----------+------------+------------ + 823573673 | Kranløft Vestland | 2009-12-31 | 2010-01-01 | infinity | 2010-01-01 | + 921835809 | Kranløft Østland | 2010-12-31 | 2011-01-01 | infinity | 2011-01-01 | +(2 rows) + +\echo "Check statistical unit history by year - births should be 1 for year 2010 and 2011" +"Check statistical unit history by year - births should be 1 for year 2010 and 2011" +SELECT resolution, year, month, unit_type, count, births, deaths +FROM public.statistical_history +WHERE resolution = 'year' +AND year < 2013 +AND unit_type = 'legal_unit'; + resolution | year | month | unit_type | count | births | deaths +------------+------+-------+------------+-------+--------+-------- + year | 2010 | | legal_unit | 1 | 1 | 0 + year | 2011 | | legal_unit | 2 | 1 | 0 + year | 2012 | | legal_unit | 2 | 0 | 0 +(3 rows) + +\echo "Check statistical unit history by year-month - births should be 1 for year-month 2010-1 and 2011-1" +"Check statistical unit history by year-month - births should be 1 for year-month 2010-1 and 2011-1" +SELECT resolution, year, month, unit_type, count, births, deaths +FROM public.statistical_history +WHERE resolution = 'year-month' +AND year < 2013 +AND unit_type = 'legal_unit'; + resolution | year | month | unit_type | count | births | deaths +------------+------+-------+------------+-------+--------+-------- + year-month | 2010 | 1 | legal_unit | 1 | 1 | 0 + year-month | 2010 | 2 | legal_unit | 1 | 0 | 0 + year-month | 2010 | 3 | legal_unit | 1 | 0 | 0 + year-month | 2010 | 4 | legal_unit | 1 | 0 | 0 + year-month | 2010 | 5 | legal_unit | 1 | 0 | 0 + year-month | 2010 | 6 | legal_unit | 1 | 0 | 0 + year-month | 2010 | 7 | legal_unit | 1 | 0 | 0 + year-month | 2010 | 8 | legal_unit | 1 | 0 | 0 + year-month | 2010 | 9 | legal_unit | 1 | 0 | 0 + year-month | 2010 | 10 | legal_unit | 1 | 0 | 0 + year-month | 2010 | 11 | legal_unit | 1 | 0 | 0 + year-month | 2010 | 12 | legal_unit | 1 | 0 | 0 + year-month | 2011 | 1 | legal_unit | 2 | 1 | 0 + year-month | 2011 | 2 | legal_unit | 2 | 0 | 0 + year-month | 2011 | 3 | legal_unit | 2 | 0 | 0 + year-month | 2011 | 4 | legal_unit | 2 | 0 | 0 + year-month | 2011 | 5 | legal_unit | 2 | 0 | 0 + year-month | 2011 | 6 | legal_unit | 2 | 0 | 0 + year-month | 2011 | 7 | legal_unit | 2 | 0 | 0 + year-month | 2011 | 8 | legal_unit | 2 | 0 | 0 + year-month | 2011 | 9 | legal_unit | 2 | 0 | 0 + year-month | 2011 | 10 | legal_unit | 2 | 0 | 0 + year-month | 2011 | 11 | legal_unit | 2 | 0 | 0 + year-month | 2011 | 12 | legal_unit | 2 | 0 | 0 + year-month | 2012 | 1 | legal_unit | 2 | 0 | 0 + year-month | 2012 | 2 | legal_unit | 2 | 0 | 0 + year-month | 2012 | 3 | legal_unit | 2 | 0 | 0 + year-month | 2012 | 4 | legal_unit | 2 | 0 | 0 + year-month | 2012 | 5 | legal_unit | 2 | 0 | 0 + year-month | 2012 | 6 | legal_unit | 2 | 0 | 0 + year-month | 2012 | 7 | legal_unit | 2 | 0 | 0 + year-month | 2012 | 8 | legal_unit | 2 | 0 | 0 + year-month | 2012 | 9 | legal_unit | 2 | 0 | 0 + year-month | 2012 | 10 | legal_unit | 2 | 0 | 0 + year-month | 2012 | 11 | legal_unit | 2 | 0 | 0 + year-month | 2012 | 12 | legal_unit | 2 | 0 | 0 +(36 rows) + +\x +ROLLBACK TO before_loading_units; +\echo "Test births at the start of the second month" +"Test births at the start of the second month" +\x +SELECT + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.establishment) AS establishment_count, + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.legal_unit) AS legal_unit_count, + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.enterprise) AS enterprise_count; + establishment_count | legal_unit_count | enterprise_count +---------------------+------------------+------------------ + 0 | 0 | 0 +(1 row) + +\echo "User uploads the legal units" +"User uploads the legal units" +\copy public.import_legal_unit_era(valid_from,valid_to,tax_ident,name,birth_date,death_date,physical_address_part1,physical_postal_code,physical_postal_place,physical_region_code,physical_country_iso_2,postal_address_part1,postal_postal_code,postal_postal_place,postal_region_code,postal_country_iso_2,primary_activity_category_code,secondary_activity_category_code,sector_code,legal_form_code) FROM 'test/data/41_legal-units-births-start-of-second-month.csv' WITH (FORMAT csv, DELIMITER ',', QUOTE '"', HEADER true); +SELECT + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.establishment) AS establishment_count, + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.legal_unit) AS legal_unit_count, + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.enterprise) AS enterprise_count; + establishment_count | legal_unit_count | enterprise_count +---------------------+------------------+------------------ + 0 | 2 | 2 +(1 row) + +\echo "Refreshing materialized views" +"Refreshing materialized views" +-- Exclude the refresh_time_ms as it will vary. +SELECT view_name FROM statistical_unit_refresh_now(); + view_name +--------------------------- + statistical_unit + activity_category_used + region_used + sector_used + legal_form_used + country_used + statistical_unit_facet + statistical_history + statistical_history_facet +(9 rows) + +\echo "Check legal units over time" +"Check legal units over time" +SELECT external_idents ->> 'tax_ident' as tax_ident, name, valid_after, valid_from, valid_to, birth_date, death_date +FROM public.statistical_unit +WHERE unit_type = 'legal_unit'; + tax_ident | name | valid_after | valid_from | valid_to | birth_date | death_date +-----------+-------------------+-------------+------------+----------+------------+------------ + 823573673 | Kranløft Vestland | 2010-01-31 | 2010-02-01 | infinity | 2010-02-01 | + 921835809 | Kranløft Østland | 2011-01-31 | 2011-02-01 | infinity | 2011-02-01 | +(2 rows) + +\echo "Check statistical unit history by year - births should be 1 for year 2010 and 2011" +"Check statistical unit history by year - births should be 1 for year 2010 and 2011" +SELECT resolution, year, month, unit_type, count, births, deaths +FROM public.statistical_history +WHERE resolution = 'year' +AND year < 2013 +AND unit_type = 'legal_unit'; + resolution | year | month | unit_type | count | births | deaths +------------+------+-------+------------+-------+--------+-------- + year | 2010 | | legal_unit | 1 | 1 | 0 + year | 2011 | | legal_unit | 2 | 1 | 0 + year | 2012 | | legal_unit | 2 | 0 | 0 +(3 rows) + +\echo "Check statistical unit history by year-month - births should be 1 for year-month 2010-2 and 2011-2" +"Check statistical unit history by year-month - births should be 1 for year-month 2010-2 and 2011-2" +SELECT resolution, year, month, unit_type, count, births, deaths +FROM public.statistical_history +WHERE resolution = 'year-month' +AND year < 2013 +AND unit_type = 'legal_unit'; + resolution | year | month | unit_type | count | births | deaths +------------+------+-------+------------+-------+--------+-------- + year-month | 2010 | 2 | legal_unit | 1 | 1 | 0 + year-month | 2010 | 3 | legal_unit | 1 | 0 | 0 + year-month | 2010 | 4 | legal_unit | 1 | 0 | 0 + year-month | 2010 | 5 | legal_unit | 1 | 0 | 0 + year-month | 2010 | 6 | legal_unit | 1 | 0 | 0 + year-month | 2010 | 7 | legal_unit | 1 | 0 | 0 + year-month | 2010 | 8 | legal_unit | 1 | 0 | 0 + year-month | 2010 | 9 | legal_unit | 1 | 0 | 0 + year-month | 2010 | 10 | legal_unit | 1 | 0 | 0 + year-month | 2010 | 11 | legal_unit | 1 | 0 | 0 + year-month | 2010 | 12 | legal_unit | 1 | 0 | 0 + year-month | 2011 | 1 | legal_unit | 1 | 0 | 0 + year-month | 2011 | 2 | legal_unit | 2 | 1 | 0 + year-month | 2011 | 3 | legal_unit | 2 | 0 | 0 + year-month | 2011 | 4 | legal_unit | 2 | 0 | 0 + year-month | 2011 | 5 | legal_unit | 2 | 0 | 0 + year-month | 2011 | 6 | legal_unit | 2 | 0 | 0 + year-month | 2011 | 7 | legal_unit | 2 | 0 | 0 + year-month | 2011 | 8 | legal_unit | 2 | 0 | 0 + year-month | 2011 | 9 | legal_unit | 2 | 0 | 0 + year-month | 2011 | 10 | legal_unit | 2 | 0 | 0 + year-month | 2011 | 11 | legal_unit | 2 | 0 | 0 + year-month | 2011 | 12 | legal_unit | 2 | 0 | 0 + year-month | 2012 | 1 | legal_unit | 2 | 0 | 0 + year-month | 2012 | 2 | legal_unit | 2 | 0 | 0 + year-month | 2012 | 3 | legal_unit | 2 | 0 | 0 + year-month | 2012 | 4 | legal_unit | 2 | 0 | 0 + year-month | 2012 | 5 | legal_unit | 2 | 0 | 0 + year-month | 2012 | 6 | legal_unit | 2 | 0 | 0 + year-month | 2012 | 7 | legal_unit | 2 | 0 | 0 + year-month | 2012 | 8 | legal_unit | 2 | 0 | 0 + year-month | 2012 | 9 | legal_unit | 2 | 0 | 0 + year-month | 2012 | 10 | legal_unit | 2 | 0 | 0 + year-month | 2012 | 11 | legal_unit | 2 | 0 | 0 + year-month | 2012 | 12 | legal_unit | 2 | 0 | 0 +(35 rows) + +\x +ROLLBACK TO before_loading_units; +\echo "Test births in the middle of a month" +"Test births in the middle of a month" +\x +SELECT + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.establishment) AS establishment_count, + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.legal_unit) AS legal_unit_count, + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.enterprise) AS enterprise_count; + establishment_count | legal_unit_count | enterprise_count +---------------------+------------------+------------------ + 0 | 0 | 0 +(1 row) + +\echo "User uploads the legal units" +"User uploads the legal units" +\copy public.import_legal_unit_era(valid_from,valid_to,tax_ident,name,birth_date,death_date,physical_address_part1,physical_postal_code,physical_postal_place,physical_region_code,physical_country_iso_2,postal_address_part1,postal_postal_code,postal_postal_place,postal_region_code,postal_country_iso_2,primary_activity_category_code,secondary_activity_category_code,sector_code,legal_form_code) FROM 'test/data/41_legal-units-births-middle-of-month.csv' WITH (FORMAT csv, DELIMITER ',', QUOTE '"', HEADER true); +SELECT + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.establishment) AS establishment_count, + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.legal_unit) AS legal_unit_count, + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.enterprise) AS enterprise_count; + establishment_count | legal_unit_count | enterprise_count +---------------------+------------------+------------------ + 0 | 2 | 2 +(1 row) + +\echo "Refreshing materialized views" +"Refreshing materialized views" +-- Exclude the refresh_time_ms as it will vary. +SELECT view_name FROM statistical_unit_refresh_now(); + view_name +--------------------------- + statistical_unit + activity_category_used + region_used + sector_used + legal_form_used + country_used + statistical_unit_facet + statistical_history + statistical_history_facet +(9 rows) + +\echo "Check legal units over time" +"Check legal units over time" +SELECT external_idents ->> 'tax_ident' as tax_ident, name, valid_after, valid_from, valid_to, birth_date, death_date +FROM public.statistical_unit +WHERE unit_type = 'legal_unit'; + tax_ident | name | valid_after | valid_from | valid_to | birth_date | death_date +-----------+-------------------+-------------+------------+----------+------------+------------ + 823573673 | Kranløft Vestland | 2010-01-14 | 2010-01-15 | infinity | 2010-01-15 | + 921835809 | Kranløft Østland | 2011-01-14 | 2011-01-15 | infinity | 2011-01-15 | +(2 rows) + +\echo "Check statistical unit history by year - births should be 1 for year 2010 and 2011" +"Check statistical unit history by year - births should be 1 for year 2010 and 2011" +SELECT resolution, year, month, unit_type, count, births, deaths +FROM public.statistical_history +WHERE resolution = 'year' +AND year < 2013 +AND unit_type = 'legal_unit'; + resolution | year | month | unit_type | count | births | deaths +------------+------+-------+------------+-------+--------+-------- + year | 2010 | | legal_unit | 1 | 1 | 0 + year | 2011 | | legal_unit | 2 | 1 | 0 + year | 2012 | | legal_unit | 2 | 0 | 0 +(3 rows) + +\echo "Check statistical unit history by year-month - births should be 1 for year-month 2010-1 and 2011-1" +"Check statistical unit history by year-month - births should be 1 for year-month 2010-1 and 2011-1" +SELECT resolution, year, month, unit_type, count, births, deaths +FROM public.statistical_history +WHERE resolution = 'year-month' +AND year < 2013 +AND unit_type = 'legal_unit'; + resolution | year | month | unit_type | count | births | deaths +------------+------+-------+------------+-------+--------+-------- + year-month | 2010 | 1 | legal_unit | 1 | 1 | 0 + year-month | 2010 | 2 | legal_unit | 1 | 0 | 0 + year-month | 2010 | 3 | legal_unit | 1 | 0 | 0 + year-month | 2010 | 4 | legal_unit | 1 | 0 | 0 + year-month | 2010 | 5 | legal_unit | 1 | 0 | 0 + year-month | 2010 | 6 | legal_unit | 1 | 0 | 0 + year-month | 2010 | 7 | legal_unit | 1 | 0 | 0 + year-month | 2010 | 8 | legal_unit | 1 | 0 | 0 + year-month | 2010 | 9 | legal_unit | 1 | 0 | 0 + year-month | 2010 | 10 | legal_unit | 1 | 0 | 0 + year-month | 2010 | 11 | legal_unit | 1 | 0 | 0 + year-month | 2010 | 12 | legal_unit | 1 | 0 | 0 + year-month | 2011 | 1 | legal_unit | 2 | 1 | 0 + year-month | 2011 | 2 | legal_unit | 2 | 0 | 0 + year-month | 2011 | 3 | legal_unit | 2 | 0 | 0 + year-month | 2011 | 4 | legal_unit | 2 | 0 | 0 + year-month | 2011 | 5 | legal_unit | 2 | 0 | 0 + year-month | 2011 | 6 | legal_unit | 2 | 0 | 0 + year-month | 2011 | 7 | legal_unit | 2 | 0 | 0 + year-month | 2011 | 8 | legal_unit | 2 | 0 | 0 + year-month | 2011 | 9 | legal_unit | 2 | 0 | 0 + year-month | 2011 | 10 | legal_unit | 2 | 0 | 0 + year-month | 2011 | 11 | legal_unit | 2 | 0 | 0 + year-month | 2011 | 12 | legal_unit | 2 | 0 | 0 + year-month | 2012 | 1 | legal_unit | 2 | 0 | 0 + year-month | 2012 | 2 | legal_unit | 2 | 0 | 0 + year-month | 2012 | 3 | legal_unit | 2 | 0 | 0 + year-month | 2012 | 4 | legal_unit | 2 | 0 | 0 + year-month | 2012 | 5 | legal_unit | 2 | 0 | 0 + year-month | 2012 | 6 | legal_unit | 2 | 0 | 0 + year-month | 2012 | 7 | legal_unit | 2 | 0 | 0 + year-month | 2012 | 8 | legal_unit | 2 | 0 | 0 + year-month | 2012 | 9 | legal_unit | 2 | 0 | 0 + year-month | 2012 | 10 | legal_unit | 2 | 0 | 0 + year-month | 2012 | 11 | legal_unit | 2 | 0 | 0 + year-month | 2012 | 12 | legal_unit | 2 | 0 | 0 +(36 rows) + +\x +ROLLBACK; diff --git a/test/expected/42_history_legal_units_deaths.out b/test/expected/42_history_legal_units_deaths.out index e69de29bb..d0bc637cc 100644 --- a/test/expected/42_history_legal_units_deaths.out +++ b/test/expected/42_history_legal_units_deaths.out @@ -0,0 +1,396 @@ +SET datestyle TO 'ISO, DMY'; +BEGIN; +\echo "Setting up Statbus to test enterprise grouping and primary" +"Setting up Statbus to test enterprise grouping and primary" +\echo "User selected the Activity Category Standard" +"User selected the Activity Category Standard" +INSERT INTO settings(activity_category_standard_id,only_one_setting) +SELECT id, true FROM activity_category_standard WHERE code = 'nace_v2.1' +ON CONFLICT (only_one_setting) +DO UPDATE SET + activity_category_standard_id =(SELECT id FROM activity_category_standard WHERE code = 'nace_v2.1') + WHERE settings.id = EXCLUDED.id; +; +SELECT acs.code + FROM public.settings AS s + JOIN activity_category_standard AS acs + ON s.activity_category_standard_id = acs.id; + code +----------- + nace_v2.1 +(1 row) + +\echo "User uploads the sample activity categories" +"User uploads the sample activity categories" +\copy public.activity_category_available_custom(path,name,description) FROM 'app/public/activity_category_norway.csv' WITH (FORMAT csv, DELIMITER ',', QUOTE '"', HEADER true); +SELECT count(*) FROM public.activity_category_available; + count +------- + 2215 +(1 row) + +\echo "User uploads the sample regions" +"User uploads the sample regions" +\copy public.region_upload(path, name) FROM 'app/public/norway-regions-2024.csv' WITH (FORMAT csv, DELIMITER ',', QUOTE '"', HEADER true); +SELECT count(*) FROM public.region; + count +------- + 417 +(1 row) + +\echo "User uploads the sample legal forms" +"User uploads the sample legal forms" +\copy public.legal_form_custom_only(code,name) FROM 'app/public/legal_form_norway.csv' WITH (FORMAT csv, DELIMITER ',', QUOTE '"', HEADER true); +SELECT count(*) FROM public.legal_form_available; + count +------- + 46 +(1 row) + +\echo "User uploads the sample sectors" +"User uploads the sample sectors" +\copy public.sector_custom_only(path,name,description) FROM 'app/public/sector_norway.csv' WITH (FORMAT csv, DELIMITER ',', QUOTE '"', HEADER true); +SELECT count(*) FROM public.sector_available; + count +------- + 33 +(1 row) + +SAVEPOINT before_loading_units; +\echo "Test deaths at the end of the year" +"Test deaths at the end of the year" +SELECT + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.establishment) AS establishment_count, + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.legal_unit) AS legal_unit_count, + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.enterprise) AS enterprise_count; + establishment_count | legal_unit_count | enterprise_count +---------------------+------------------+------------------ + 0 | 0 | 0 +(1 row) + +\echo "User uploads the legal units" +"User uploads the legal units" +\copy public.import_legal_unit_era(valid_from,valid_to,tax_ident,name,birth_date,death_date,physical_address_part1,physical_postal_code,physical_postal_place,physical_region_code,physical_country_iso_2,postal_address_part1,postal_postal_code,postal_postal_place,postal_region_code,postal_country_iso_2,primary_activity_category_code,secondary_activity_category_code,sector_code,legal_form_code) FROM 'test/data/42_legal-units-deaths-end-of-year.csv' WITH (FORMAT csv, DELIMITER ',', QUOTE '"', HEADER true); +SELECT + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.establishment) AS establishment_count, + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.legal_unit) AS legal_unit_count, + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.enterprise) AS enterprise_count; + establishment_count | legal_unit_count | enterprise_count +---------------------+------------------+------------------ + 0 | 2 | 2 +(1 row) + +\echo "Refreshing materialized views" +"Refreshing materialized views" +-- Exclude the refresh_time_ms as it will vary. +SELECT view_name FROM statistical_unit_refresh_now(); + view_name +--------------------------- + statistical_unit + activity_category_used + region_used + sector_used + legal_form_used + country_used + statistical_unit_facet + statistical_history + statistical_history_facet +(9 rows) + +\echo "Check statistical units" +"Check statistical units" +SELECT external_idents ->> 'tax_ident' as tax_ident, name, valid_after, valid_from, valid_to, birth_date, death_date +FROM public.statistical_unit +WHERE unit_type = 'legal_unit'; + tax_ident | name | valid_after | valid_from | valid_to | birth_date | death_date +-----------+-------------------+-------------+------------+------------+------------+------------ + 823573673 | Kranløft Vestland | 2009-12-31 | 2010-01-01 | 2012-12-30 | 2010-01-01 | + 823573673 | Kranløft Vestland | 2012-12-30 | 2012-12-31 | 2012-12-31 | 2010-01-01 | 2012-12-31 + 921835809 | Kranløft Østland | 2009-12-31 | 2010-01-01 | 2011-12-30 | 2010-01-01 | + 921835809 | Kranløft Østland | 2011-12-30 | 2011-12-31 | 2011-12-31 | 2010-01-01 | 2011-12-31 +(4 rows) + +\echo "Check statistical unit history by year - deaths should be 1 for year 2011 and 2012" +"Check statistical unit history by year - deaths should be 1 for year 2011 and 2012" +SELECT resolution, year, month, unit_type, count, births, deaths +FROM public.statistical_history +WHERE resolution = 'year' +AND year < 2013 +AND unit_type = 'legal_unit'; + resolution | year | month | unit_type | count | births | deaths +------------+------+-------+------------+-------+--------+-------- + year | 2010 | | legal_unit | 2 | 2 | 0 + year | 2011 | | legal_unit | 1 | 0 | 1 + year | 2012 | | legal_unit | 0 | 0 | 1 +(3 rows) + +\echo "Check statistical unit history by year-month - deaths should be 1 for year-month 2011-12 and 2012-12" +"Check statistical unit history by year-month - deaths should be 1 for year-month 2011-12 and 2012-12" +SELECT resolution, year, month, unit_type, count, births, deaths +FROM public.statistical_history +WHERE resolution = 'year-month' +AND year < 2013 +AND unit_type = 'legal_unit'; + resolution | year | month | unit_type | count | births | deaths +------------+------+-------+------------+-------+--------+-------- + year-month | 2010 | 1 | legal_unit | 2 | 2 | 0 + year-month | 2010 | 2 | legal_unit | 2 | 0 | 0 + year-month | 2010 | 3 | legal_unit | 2 | 0 | 0 + year-month | 2010 | 4 | legal_unit | 2 | 0 | 0 + year-month | 2010 | 5 | legal_unit | 2 | 0 | 0 + year-month | 2010 | 6 | legal_unit | 2 | 0 | 0 + year-month | 2010 | 7 | legal_unit | 2 | 0 | 0 + year-month | 2010 | 8 | legal_unit | 2 | 0 | 0 + year-month | 2010 | 9 | legal_unit | 2 | 0 | 0 + year-month | 2010 | 10 | legal_unit | 2 | 0 | 0 + year-month | 2010 | 11 | legal_unit | 2 | 0 | 0 + year-month | 2010 | 12 | legal_unit | 2 | 0 | 0 + year-month | 2011 | 1 | legal_unit | 2 | 0 | 0 + year-month | 2011 | 2 | legal_unit | 2 | 0 | 0 + year-month | 2011 | 3 | legal_unit | 2 | 0 | 0 + year-month | 2011 | 4 | legal_unit | 2 | 0 | 0 + year-month | 2011 | 5 | legal_unit | 2 | 0 | 0 + year-month | 2011 | 6 | legal_unit | 2 | 0 | 0 + year-month | 2011 | 7 | legal_unit | 2 | 0 | 0 + year-month | 2011 | 8 | legal_unit | 2 | 0 | 0 + year-month | 2011 | 9 | legal_unit | 2 | 0 | 0 + year-month | 2011 | 10 | legal_unit | 2 | 0 | 0 + year-month | 2011 | 11 | legal_unit | 2 | 0 | 0 + year-month | 2011 | 12 | legal_unit | 1 | 0 | 1 + year-month | 2012 | 1 | legal_unit | 1 | 0 | 0 + year-month | 2012 | 2 | legal_unit | 1 | 0 | 0 + year-month | 2012 | 3 | legal_unit | 1 | 0 | 0 + year-month | 2012 | 4 | legal_unit | 1 | 0 | 0 + year-month | 2012 | 5 | legal_unit | 1 | 0 | 0 + year-month | 2012 | 6 | legal_unit | 1 | 0 | 0 + year-month | 2012 | 7 | legal_unit | 1 | 0 | 0 + year-month | 2012 | 8 | legal_unit | 1 | 0 | 0 + year-month | 2012 | 9 | legal_unit | 1 | 0 | 0 + year-month | 2012 | 10 | legal_unit | 1 | 0 | 0 + year-month | 2012 | 11 | legal_unit | 1 | 0 | 0 + year-month | 2012 | 12 | legal_unit | 0 | 0 | 1 +(36 rows) + +\x +ROLLBACK TO before_loading_units; +\echo "Test deaths at the end of the first month" +"Test deaths at the end of the first month" +\x +SELECT + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.establishment) AS establishment_count, + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.legal_unit) AS legal_unit_count, + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.enterprise) AS enterprise_count; + establishment_count | legal_unit_count | enterprise_count +---------------------+------------------+------------------ + 0 | 0 | 0 +(1 row) + +\echo "User uploads the legal units" +"User uploads the legal units" +\copy public.import_legal_unit_era(valid_from,valid_to,tax_ident,name,birth_date,death_date,physical_address_part1,physical_postal_code,physical_postal_place,physical_region_code,physical_country_iso_2,postal_address_part1,postal_postal_code,postal_postal_place,postal_region_code,postal_country_iso_2,primary_activity_category_code,secondary_activity_category_code,sector_code,legal_form_code) FROM 'test/data/42_legal-units-deaths-end-of-first-month.csv' WITH (FORMAT csv, DELIMITER ',', QUOTE '"', HEADER true); +SELECT + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.establishment) AS establishment_count, + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.legal_unit) AS legal_unit_count, + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.enterprise) AS enterprise_count; + establishment_count | legal_unit_count | enterprise_count +---------------------+------------------+------------------ + 0 | 2 | 2 +(1 row) + +\echo "Refreshing materialized views" +"Refreshing materialized views" +-- Exclude the refresh_time_ms as it will vary. +SELECT view_name FROM statistical_unit_refresh_now(); + view_name +--------------------------- + statistical_unit + activity_category_used + region_used + sector_used + legal_form_used + country_used + statistical_unit_facet + statistical_history + statistical_history_facet +(9 rows) + +\echo "Check statistical units" +"Check statistical units" +SELECT external_idents ->> 'tax_ident' as tax_ident, name, valid_after, valid_from, valid_to, birth_date, death_date +FROM public.statistical_unit +WHERE unit_type = 'legal_unit'; + tax_ident | name | valid_after | valid_from | valid_to | birth_date | death_date +-----------+-------------------+-------------+------------+------------+------------+------------ + 823573673 | Kranløft Vestland | 2009-12-31 | 2010-01-01 | 2012-12-30 | 2010-01-01 | + 823573673 | Kranløft Vestland | 2012-12-30 | 2012-12-31 | 2012-12-31 | 2010-01-01 | 2012-12-31 + 921835809 | Kranløft Østland | 2009-12-31 | 2010-01-01 | 2011-01-31 | 2010-01-01 | 2011-01-31 +(3 rows) + +\echo "Check statistical unit history by year - deaths should be 1 for year 2011 and 2012" +"Check statistical unit history by year - deaths should be 1 for year 2011 and 2012" +SELECT resolution, year, month, unit_type, count, births, deaths +FROM public.statistical_history +WHERE resolution = 'year' +AND year < 2013 +AND unit_type = 'legal_unit'; + resolution | year | month | unit_type | count | births | deaths +------------+------+-------+------------+-------+--------+-------- + year | 2010 | | legal_unit | 2 | 2 | 0 + year | 2011 | | legal_unit | 1 | 0 | 1 + year | 2012 | | legal_unit | 0 | 0 | 1 +(3 rows) + +\echo "Check statistical unit history by year-month - deaths should be 1 for year-month 2011-1 and 2012-12" +"Check statistical unit history by year-month - deaths should be 1 for year-month 2011-1 and 2012-12" +SELECT resolution, year, month, unit_type, count, births, deaths +FROM public.statistical_history +WHERE resolution = 'year-month' +AND year < 2013 +AND unit_type = 'legal_unit'; + resolution | year | month | unit_type | count | births | deaths +------------+------+-------+------------+-------+--------+-------- + year-month | 2010 | 1 | legal_unit | 2 | 2 | 0 + year-month | 2010 | 2 | legal_unit | 2 | 0 | 0 + year-month | 2010 | 3 | legal_unit | 2 | 0 | 0 + year-month | 2010 | 4 | legal_unit | 2 | 0 | 0 + year-month | 2010 | 5 | legal_unit | 2 | 0 | 0 + year-month | 2010 | 6 | legal_unit | 2 | 0 | 0 + year-month | 2010 | 7 | legal_unit | 2 | 0 | 0 + year-month | 2010 | 8 | legal_unit | 2 | 0 | 0 + year-month | 2010 | 9 | legal_unit | 2 | 0 | 0 + year-month | 2010 | 10 | legal_unit | 2 | 0 | 0 + year-month | 2010 | 11 | legal_unit | 2 | 0 | 0 + year-month | 2010 | 12 | legal_unit | 2 | 0 | 0 + year-month | 2011 | 1 | legal_unit | 1 | 0 | 1 + year-month | 2011 | 2 | legal_unit | 1 | 0 | 0 + year-month | 2011 | 3 | legal_unit | 1 | 0 | 0 + year-month | 2011 | 4 | legal_unit | 1 | 0 | 0 + year-month | 2011 | 5 | legal_unit | 1 | 0 | 0 + year-month | 2011 | 6 | legal_unit | 1 | 0 | 0 + year-month | 2011 | 7 | legal_unit | 1 | 0 | 0 + year-month | 2011 | 8 | legal_unit | 1 | 0 | 0 + year-month | 2011 | 9 | legal_unit | 1 | 0 | 0 + year-month | 2011 | 10 | legal_unit | 1 | 0 | 0 + year-month | 2011 | 11 | legal_unit | 1 | 0 | 0 + year-month | 2011 | 12 | legal_unit | 1 | 0 | 0 + year-month | 2012 | 1 | legal_unit | 1 | 0 | 0 + year-month | 2012 | 2 | legal_unit | 1 | 0 | 0 + year-month | 2012 | 3 | legal_unit | 1 | 0 | 0 + year-month | 2012 | 4 | legal_unit | 1 | 0 | 0 + year-month | 2012 | 5 | legal_unit | 1 | 0 | 0 + year-month | 2012 | 6 | legal_unit | 1 | 0 | 0 + year-month | 2012 | 7 | legal_unit | 1 | 0 | 0 + year-month | 2012 | 8 | legal_unit | 1 | 0 | 0 + year-month | 2012 | 9 | legal_unit | 1 | 0 | 0 + year-month | 2012 | 10 | legal_unit | 1 | 0 | 0 + year-month | 2012 | 11 | legal_unit | 1 | 0 | 0 + year-month | 2012 | 12 | legal_unit | 0 | 0 | 1 +(36 rows) + +\x +ROLLBACK TO before_loading_units; +\echo "Test deaths in the start of a month" +"Test deaths in the start of a month" +\x +SELECT + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.establishment) AS establishment_count, + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.legal_unit) AS legal_unit_count, + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.enterprise) AS enterprise_count; + establishment_count | legal_unit_count | enterprise_count +---------------------+------------------+------------------ + 0 | 0 | 0 +(1 row) + +\echo "User uploads the legal units" +"User uploads the legal units" +\copy public.import_legal_unit_era(valid_from,valid_to,tax_ident,name,birth_date,death_date,physical_address_part1,physical_postal_code,physical_postal_place,physical_region_code,physical_country_iso_2,postal_address_part1,postal_postal_code,postal_postal_place,postal_region_code,postal_country_iso_2,primary_activity_category_code,secondary_activity_category_code,sector_code,legal_form_code) FROM 'test/data/42_legal-units-deaths-start-of-month.csv' WITH (FORMAT csv, DELIMITER ',', QUOTE '"', HEADER true); +SELECT + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.establishment) AS establishment_count, + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.legal_unit) AS legal_unit_count, + (SELECT COUNT(DISTINCT id) AS distinct_unit_count FROM public.enterprise) AS enterprise_count; + establishment_count | legal_unit_count | enterprise_count +---------------------+------------------+------------------ + 0 | 2 | 2 +(1 row) + +\echo "Refreshing materialized views" +"Refreshing materialized views" +-- Exclude the refresh_time_ms as it will vary. +SELECT view_name FROM statistical_unit_refresh_now(); + view_name +--------------------------- + statistical_unit + activity_category_used + region_used + sector_used + legal_form_used + country_used + statistical_unit_facet + statistical_history + statistical_history_facet +(9 rows) + +\echo "Check statistical units" +"Check statistical units" +SELECT external_idents ->> 'tax_ident' as tax_ident, name, valid_after, valid_from, valid_to, birth_date, death_date +FROM public.statistical_unit +WHERE unit_type = 'legal_unit'; + tax_ident | name | valid_after | valid_from | valid_to | birth_date | death_date +-----------+-------------------+-------------+------------+------------+------------+------------ + 823573673 | Kranløft Vestland | 2009-12-31 | 2010-01-01 | 2011-12-31 | 2010-01-01 | + 823573673 | Kranløft Vestland | 2011-12-31 | 2012-01-01 | 2012-01-01 | 2010-01-01 | 2012-01-01 + 921835809 | Kranløft Østland | 2009-12-31 | 2010-01-01 | 2011-01-01 | 2010-01-01 | 2011-01-01 +(3 rows) + +\echo "Check statistical unit history by year - deaths should be 1 for year 2011 and 2012" +"Check statistical unit history by year - deaths should be 1 for year 2011 and 2012" +SELECT resolution, year, month, unit_type, count, births, deaths +FROM public.statistical_history +WHERE resolution = 'year' +AND year < 2013 +AND unit_type = 'legal_unit'; + resolution | year | month | unit_type | count | births | deaths +------------+------+-------+------------+-------+--------+-------- + year | 2010 | | legal_unit | 2 | 2 | 0 + year | 2011 | | legal_unit | 1 | 0 | 1 + year | 2012 | | legal_unit | 0 | 0 | 1 +(3 rows) + +\echo "Check statistical unit history by year-month - deaths should be 1 for year-month 2011-1 and 2012-1" +"Check statistical unit history by year-month - deaths should be 1 for year-month 2011-1 and 2012-1" +SELECT resolution, year, month, unit_type, count, births, deaths +FROM public.statistical_history +WHERE resolution = 'year-month' +AND year < 2013 +AND unit_type = 'legal_unit'; + resolution | year | month | unit_type | count | births | deaths +------------+------+-------+------------+-------+--------+-------- + year-month | 2010 | 1 | legal_unit | 2 | 2 | 0 + year-month | 2010 | 2 | legal_unit | 2 | 0 | 0 + year-month | 2010 | 3 | legal_unit | 2 | 0 | 0 + year-month | 2010 | 4 | legal_unit | 2 | 0 | 0 + year-month | 2010 | 5 | legal_unit | 2 | 0 | 0 + year-month | 2010 | 6 | legal_unit | 2 | 0 | 0 + year-month | 2010 | 7 | legal_unit | 2 | 0 | 0 + year-month | 2010 | 8 | legal_unit | 2 | 0 | 0 + year-month | 2010 | 9 | legal_unit | 2 | 0 | 0 + year-month | 2010 | 10 | legal_unit | 2 | 0 | 0 + year-month | 2010 | 11 | legal_unit | 2 | 0 | 0 + year-month | 2010 | 12 | legal_unit | 2 | 0 | 0 + year-month | 2011 | 1 | legal_unit | 1 | 0 | 1 + year-month | 2011 | 2 | legal_unit | 1 | 0 | 0 + year-month | 2011 | 3 | legal_unit | 1 | 0 | 0 + year-month | 2011 | 4 | legal_unit | 1 | 0 | 0 + year-month | 2011 | 5 | legal_unit | 1 | 0 | 0 + year-month | 2011 | 6 | legal_unit | 1 | 0 | 0 + year-month | 2011 | 7 | legal_unit | 1 | 0 | 0 + year-month | 2011 | 8 | legal_unit | 1 | 0 | 0 + year-month | 2011 | 9 | legal_unit | 1 | 0 | 0 + year-month | 2011 | 10 | legal_unit | 1 | 0 | 0 + year-month | 2011 | 11 | legal_unit | 1 | 0 | 0 + year-month | 2011 | 12 | legal_unit | 1 | 0 | 0 + year-month | 2012 | 1 | legal_unit | 0 | 0 | 1 +(25 rows) + +\x +ROLLBACK; diff --git a/test/sql/41_history_legal_units_births.sql b/test/sql/41_history_legal_units_births.sql index ed24b5880..8a22c6219 100644 --- a/test/sql/41_history_legal_units_births.sql +++ b/test/sql/41_history_legal_units_births.sql @@ -64,7 +64,7 @@ WHERE unit_type = 'legal_unit'; \echo "Check statistical unit history by year - births should be 1 for year 2010 and 2011" SELECT resolution, year, month, unit_type, count, births, deaths FROM public.statistical_history -WHERE resolution = 'year' +WHERE resolution = 'year' AND year < 2013 AND unit_type = 'legal_unit'; @@ -72,7 +72,7 @@ AND unit_type = 'legal_unit'; \echo "Check statistical unit history by year-month - births should be 1 for year-month 2010-1 and 2011-1" SELECT resolution, year, month, unit_type, count, births, deaths FROM public.statistical_history -WHERE resolution = 'year-month' +WHERE resolution = 'year-month' AND year < 2013 AND unit_type = 'legal_unit'; @@ -110,7 +110,7 @@ WHERE unit_type = 'legal_unit'; \echo "Check statistical unit history by year - births should be 1 for year 2010 and 2011" SELECT resolution, year, month, unit_type, count, births, deaths FROM public.statistical_history -WHERE resolution = 'year' +WHERE resolution = 'year' AND year < 2013 AND unit_type = 'legal_unit'; @@ -118,7 +118,7 @@ AND unit_type = 'legal_unit'; \echo "Check statistical unit history by year-month - births should be 1 for year-month 2010-2 and 2011-2" SELECT resolution, year, month, unit_type, count, births, deaths FROM public.statistical_history -WHERE resolution = 'year-month' +WHERE resolution = 'year-month' AND year < 2013 AND unit_type = 'legal_unit'; @@ -157,7 +157,7 @@ WHERE unit_type = 'legal_unit'; \echo "Check statistical unit history by year - births should be 1 for year 2010 and 2011" SELECT resolution, year, month, unit_type, count, births, deaths FROM public.statistical_history -WHERE resolution = 'year' +WHERE resolution = 'year' AND year < 2013 AND unit_type = 'legal_unit'; @@ -165,10 +165,10 @@ AND unit_type = 'legal_unit'; \echo "Check statistical unit history by year-month - births should be 1 for year-month 2010-1 and 2011-1" SELECT resolution, year, month, unit_type, count, births, deaths FROM public.statistical_history -WHERE resolution = 'year-month' +WHERE resolution = 'year-month' AND year < 2013 AND unit_type = 'legal_unit'; \x -ROLLBACK; \ No newline at end of file +ROLLBACK;