Correction of finding postcode + set the center postcode as coordinates for address without reference

This commit is contained in:
2025-09-16 13:41:48 +02:00
parent 08e87503f0
commit 45755bf797

View File

@@ -302,7 +302,7 @@ INSERT INTO chill_person_person_center_history (id, person_id, center_id, startd
-- a) add new columns
ALTER TABLE import.personnes ADD column address_id BIGINT;
ALTER TABLE import.personnes ADD column address_ref_id BIGINT;
ALTER TABLE import.personnes ADD column postcode_arr BIGINT[];
ALTER TABLE import.personnes ADD column postcode_id BIGINT[];
-- special command to extract postcode values in text
ALTER TABLE import.personnes ADD COLUMN postcode1 TEXT;
@@ -310,18 +310,18 @@ UPDATE import.personnes SET postcode1=SUBSTRING(postcode FROM '\y\d{5}\y') WHERE
-- b) find and add postal code references. Check missing correspondances (if you can)
UPDATE import.personnes
SET postcode_arr = (
SELECT array_agg(pc.id)
SET postcode_id = (
SELECT pc.id
FROM chill_main_postal_code AS pc
WHERE pc.code = import.personnes.postcode1
WHERE pc.canonical ILIKE import.personnes.postcode
AND pc.origin = 0
);
--SELECT * FROM import.personnes as ip WHERE ip.postcode_arr IS NULL -- missing correspondances
--SELECT * FROM import.personnes as ip WHERE ip.postcode_id IS NULL -- missing correspondances
-- c) find and add reference addresses. Check missing correspondances (if you can)
UPDATE import.personnes AS ip SET address_ref_id = cmar.id FROM chill_main_address_reference AS cmar
WHERE
cmar.postcode_id = ANY(ip.postcode_arr)
cmar.postcode_id = ip.postcode_id
AND similarity(trim(ip.street), trim(cmar.street)) > 0.6
AND trim(ip.streetnumber) = trim(cmar.streetnumber);
--SELECT * FROM import.personnes as ip WHERE ip.address_ref_id IS NULL -- missing correspondances
@@ -331,7 +331,9 @@ UPDATE import.personnes SET address_id = nextval('chill_main_address_id_seq');
-- e) insert reference address into chill_main_addresses
INSERT INTO chill_main_address (id, postcode_id, street, streetnumber, extra, validFrom, addressreference_id, refstatus, point, createdat, updatedat, createdby_id, updatedby_id)
SELECT address_id, postcode_arr[1], street, coalesce(streetnumber, ''), coalesce(extra,''), CURRENT_DATE, address_ref_id, 'match',
SELECT address_id, postcode_id, street, coalesce(streetnumber, ''), coalesce(extra,''),
CURRENT_DATE,
address_ref_id, 'match',
(SELECT point FROM chill_main_address_reference WHERE id = address_ref_id),
CURRENT_DATE,
CURRENT_DATE,
@@ -340,13 +342,16 @@ INSERT INTO chill_main_address (id, postcode_id, street, streetnumber, extra, va
FROM import.personnes WHERE address_ref_id IS NOT NULL;
-- f) insert created addresses in chill_main_addresses
INSERT INTO chill_main_address (id, postcode_id, street, streetnumber, extra, validFrom, createdat, updatedat, createdby_id, updatedby_id)
SELECT address_id, postcode_arr[1], coalesce(street, ''), coalesce(streetnumber, ''), coalesce(extra,''), CURRENT_DATE,
INSERT INTO chill_main_address (id, postcode_id, street, streetnumber, extra, validFrom, point, createdat, updatedat, createdby_id, updatedby_id)
SELECT address_id, postcode_id, coalesce(street, ''), coalesce(streetnumber, ''), coalesce(extra,''),
CURRENT_DATE,
(SELECT center FROM chill_main_postal_code WHERE id = postcode_id), -- geolocation is given by the postcode
CURRENT_DATE,
CURRENT_DATE,
(SELECT distinct(first_value(id) OVER(ORDER BY id)) FROM users),
(SELECT distinct(first_value(id) OVER(ORDER BY id)) FROM users)
FROM import.personnes WHERE address_ref_id IS NULL AND postcode_arr[1] IS NOT NULL;
FROM import.personnes WHERE address_ref_id IS NULL AND postcode_id IS NOT NULL;
SELECT setval('chill_main_address_id_seq', (SELECT COALESCE(max(id)) FROM chill_main_address));
-- 44. Add houshold and household member with position