From 0c7ea76178057ba5d6c762d1e9795ef61e00c492 Mon Sep 17 00:00:00 2001 From: Mathieu Jaumotte Date: Wed, 8 Mar 2023 23:44:39 +0100 Subject: [PATCH] 42. Insert query bloc for center history insertion --- sql/import.sql | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/sql/import.sql b/sql/import.sql index 85ddbb8..5839a97 100644 --- a/sql/import.sql +++ b/sql/import.sql @@ -265,7 +265,15 @@ INSERT INTO chill_person_person ( */ FROM import.personnes ip; --- 42. Add address, and link it to person +-- 42. Fill Person center history +INSERT INTO chill_person_person_center_history (id, person_id, center_id, startdate) SELECT + nextval('chill_person_person_center_history_id_seq'), person_id, + (SELECT distinct(first_value(id) OVER(ORDER BY id)) FROM centers) , -- center_id + CURRENT_DATE + FROM import.personnes ip; +-- SELECT ip.id, ip.lastname, ip.firstname, ip.person_id, hist.id, hist.person_id, hist.center_id, hist.startdate, p.id, p.fullnamecanonical, p.email FROM chill_person_person p RIGHT JOIN import.personnes ip ON ip.person_id = p.id FULL JOIN chill_person_person_center_history hist on p.id = hist.person_id ORDER BY ip.person_id; + +-- 43. Add address, and link it to person ALTER TABLE import.personnes ADD column address_id BIGINT; UPDATE import.personnes SET address_id = nextval('chill_main_address_id_seq'); ALTER TABLE import.personnes ADD column postcode_id BIGINT; @@ -276,7 +284,7 @@ INSERT INTO chill_main_address (id, postcode_id, street, streetnumber, extra, va SELECT ip.person_id, ip.address_id FROM import.personnes ip ; -- SELECT ip.id, ip.person_id, ip.firstname, ip.lastname, addr.* FROM chill_main_address addr RIGHT JOIN import.personnes ip ON addr.id = ip.address_id ORDER BY ip.id; --- 43. Add houshold and household member with position +-- 44. Add houshold and household member with position INSERT INTO chill_person_household (id) SELECT person_id from import.personnes; SELECT setval('chill_person_household_id_seq', (SELECT max(id) FROM chill_person_household)); INSERT INTO chill_person_household_members (id, person_id, household_id, startdate, sharedhousehold, position_id, holder) SELECT @@ -286,11 +294,11 @@ INSERT INTO chill_person_household_members (id, person_id, household_id, startda -- SELECT ip.id, ip.person_id, ip.firstname, ip.lastname, hh.* FROM chill_person_household hh FULL JOIN import.personnes ip ON hh.id = ip.person_id ORDER BY hh.id; -- SELECT ip.id, ip.person_id, ip.firstname, ip.lastname, memb.* FROM chill_person_household_members memb JOIN import.personnes ip ON memb.household_id = ip.person_id ORDER BY memb.household_id; --- 44. Add address to household +-- 45. Add address to household INSERT INTO chill_person_household_to_addresses (household_id, address_id) SELECT person_id, address_id FROM import.personnes WHERE postcode_id IS NOT NULL; -- cfr (*) adresse insérées seulement si postcode --- 45. Add household composition +-- 46. Add household composition -- (not yet implemented in canvas) @@ -300,21 +308,25 @@ INSERT INTO chill_person_household_to_addresses (household_id, address_id) SELEC -- DOWN -- --- Undo 44. +-- Undo 45. DELETE FROM chill_person_household_to_addresses hhaddr USING import.personnes ip WHERE hhaddr.household_id = ip.person_id ; --- Undo 43. +-- Undo 44. DELETE FROM chill_person_household_members memb USING import.personnes ip WHERE memb.person_id = ip.person_id; SELECT setval('chill_person_household_members_id_seq', (SELECT max(id) FROM chill_person_household_members)); DELETE FROM chill_person_household hh USING import.personnes ip WHERE hh.id = ip.person_id; SELECT setval('chill_person_household_id_seq', (SELECT max(id) FROM chill_person_household)); --- Undo 42. +-- Undo 43. DELETE FROM chill_main_address addr USING import.personnes ip WHERE addr.id = ip.address_id; SELECT setval('chill_main_address_id_seq', (SELECT max(id) FROM chill_main_address)); ALTER TABLE import.personnes DROP column postcode_id; ALTER TABLE import.personnes DROP column address_id; +-- Undo 42. +DELETE FROM chill_person_person_center_history hist USING import.personnes ip WHERE hist.person_id = ip.person_id; +SELECT setval('chill_person_person_center_history_id_seq', (SELECT max(id) FROM chill_person_person_center_history)); + -- Undo 41. DELETE FROM chill_person_person p USING import.personnes ip WHERE p.id = ip.person_id; SELECT setval('chill_person_person_id_seq', (SELECT max(id) FROM chill_person_person));