-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
update establishment queries (#1051)
Signed-off-by: Raphaël Courivaud <[email protected]>
- Loading branch information
1 parent
ddb00fb
commit 205ad89
Showing
23 changed files
with
4,137 additions
and
758 deletions.
There are no files selected for viewing
25 changes: 25 additions & 0 deletions
25
analytics/dbt/macros/production/events/get_last_establishment_event_status.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
{% macro get_last_establishment_event_status(user_source, event_name, all_users=false) %} | ||
( | ||
SELECT | ||
u.establishment_id, | ||
events.new_status AS status, | ||
events.event_status_label AS status_label, | ||
events.new_occupancy AS occupancy, | ||
events.new_sub_status, | ||
events.created_at, | ||
ROW_NUMBER() OVER (PARTITION BY u.establishment_id ORDER BY events.created_at DESC) AS row_num | ||
FROM {{ ref('int_production_events') }} AS events | ||
LEFT JOIN {{ ref('int_production_users') }} AS u ON events.created_by = u.id | ||
WHERE | ||
1=1 | ||
{% if event_name == 'suivi' %} | ||
AND events.status_changed = TRUE | ||
{% elif event_name == 'occupation' %} | ||
AND events.occupancy_changed = TRUE | ||
{% else %} | ||
{% endif %} | ||
{% if not all_users %} | ||
AND events.user_source = '{{ user_source }}' | ||
{% endif %} | ||
) | ||
{% endmacro %} |
14 changes: 14 additions & 0 deletions
14
analytics/dbt/macros/production/events/select_last_establishment_event.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{% macro select_last_establishment_event(establishment_table, event_table, column_suffix) %} | ||
( | ||
SELECT | ||
e.id AS establishment_id, | ||
evt.status AS last_event_status_{{ column_suffix }}, | ||
evt.status_label AS last_event_status_label_{{ column_suffix }}, | ||
evt.occupancy AS last_event_occupancy_{{ column_suffix }}, | ||
evt.new_sub_status AS last_event_sub_status_label_{{ column_suffix }}, | ||
evt.created_at AS last_event_date_{{ column_suffix }} | ||
FROM {{ establishment_table }} AS e | ||
LEFT JOIN {{ event_table }} AS evt | ||
ON evt.establishment_id = e.id AND evt.row_num = 1 | ||
) | ||
{% endmacro %} |
20 changes: 20 additions & 0 deletions
20
analytics/dbt/models/intermediate/ff/int_ff_history_housing.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
WITH ff_history as ( | ||
SELECT ff_idlocal, 'ff-' || ff_millesime as file_year, | ||
FROM {{ ref('int_ff_ext_2023') }} | ||
UNION ALL | ||
SELECT ff_idlocal, 'ff-' || ff_millesime as file_year, | ||
FROM {{ ref('int_ff_ext_2022') }} | ||
UNION ALL | ||
SELECT ff_idlocal, 'ff-' || ff_millesime as file_year, | ||
FROM {{ ref('int_ff_ext_2021') }} | ||
UNION ALL | ||
SELECT ff_idlocal, 'ff-' || ff_millesime as file_year, | ||
FROM {{ ref('int_ff_ext_2020') }} | ||
UNION ALL | ||
SELECT ff_idlocal, 'ff-' || ff_millesime as file_year, | ||
FROM {{ ref('int_ff_ext_2019') }}) | ||
SELECT | ||
ff_idlocal as local_id, | ||
listagg(file_year, ',') as file_years, | ||
FROM ff_history lh | ||
GROUP BY local_id |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
...cs/dbt/models/intermediate/production/int_production_establishment_events_last_status.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
WITH | ||
establishment_status_user_followup AS {{ get_last_establishment_event_status('user', 'suivi') }}, | ||
establishment_status_user_occupancy AS {{ get_last_establishment_event_status('user', "occupation") }}, | ||
last_establishment_status_user_followup AS {{ select_last_establishment_event(ref('int_production_establishments'), 'establishment_status_user_followup', 'user_followup') }}, | ||
last_establishment_status_user_occupancy AS {{ select_last_establishment_event(ref('int_production_establishments'), 'establishment_status_user_occupancy', 'user_occupancy') }} | ||
|
||
SELECT | ||
e.id AS establishment_id, | ||
hsuf.last_event_status_user_followup, | ||
hsuf.last_event_status_label_user_followup, | ||
hsuf.last_event_date_user_followup, | ||
hsuf.last_event_sub_status_label_user_followup, | ||
hszo.last_event_occupancy_user_occupancy, | ||
hszo.last_event_date_user_occupancy | ||
FROM {{ ref('int_production_establishments') }} AS e | ||
LEFT JOIN last_establishment_status_user_followup AS hsuf ON e.id = hsuf.establishment_id | ||
LEFT JOIN last_establishment_status_user_occupancy AS hszo ON e.id = hszo.establishment_id |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
analytics/dbt/models/intermediate/production/int_production_housing_establishments.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
|
||
SELECT | ||
ph.id as housing_id, | ||
STRING_AGG(DISTINCT pel.establishment_id, ' | ') as establishment_ids, | ||
ARRAY_AGG(DISTINCT pel.establishment_id) as establishment_ids_array, | ||
CASE SUM(peu.user_number) | ||
WHEN 0 THEN FALSE | ||
ELSE TRUE | ||
END as has_users | ||
FROM {{ ref('int_production_housing') }} as ph | ||
LEFT JOIN {{ ref('int_production_establishments_localities') }} as pel ON pel.geo_code = ph.geo_code | ||
LEFT JOIN {{ ref('int_production_establishments_users') }} as peu ON peu.establishment_id = pel.establishment_id | ||
GROUP BY housing_id |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
WITH ff_data AS ( | ||
SELECT | ||
ff_idlocal, | ||
ff_jdatat AS last_mutation_date, | ||
ff_dcapec2 AS cadastral_classification, | ||
ff_dcntpa AS plot_area, | ||
ff_geomloc AS geometry, | ||
ff_ccthp AS occupancy, | ||
ST_X(ST_Transform(ST_GeomFromWKB(FROM_HEX(ff_geomloc)), 'EPSG:2154', 'EPSG:4326')) AS latitude, | ||
ST_Y(ST_Transform(ST_GeomFromWKB(FROM_HEX(ff_geomloc)), 'EPSG:2154', 'EPSG:4326')) AS longitude | ||
FROM {{ ref('int_ff_ext_2023') }} | ||
), | ||
lovac_2024 AS ( | ||
SELECT | ||
ff_idlocal, | ||
ff_dcntpa AS plot_area, | ||
ff_jdatat AS last_mutation_date, | ||
dvf_datemut AS last_transaction_date, | ||
dvf_valeurfonc AS last_transaction_value, | ||
ffh_ccthp AS occupancy_history, | ||
ban_latitude AS latitude, | ||
ban_longitude AS longitude, | ||
'lovac-2024' AS data_file_years | ||
FROM {{ ref('int_lovac_fil_2024') }} | ||
), | ||
lovac_2023 AS ( | ||
SELECT | ||
ff_idlocal, | ||
ff_dcntpa AS plot_area, | ||
ff_jdatat AS last_mutation_date, | ||
dvf_datemut AS last_transaction_date, | ||
dvf_valeurfonc AS last_transaction_value, | ||
ffh_ccthp AS occupancy_history, | ||
ban_latitude AS latitude, | ||
ban_longitude AS longitude, | ||
'lovac-2023' AS data_file_years | ||
FROM {{ ref('int_lovac_fil_2023') }} | ||
), | ||
lovac_history AS ( | ||
SELECT * FROM {{ ref('int_lovac_history_housing') }} | ||
) | ||
SELECT | ||
COALESCE(l24.ff_idlocal, l23.ff_idlocal, fd.ff_idlocal) AS local_id, | ||
COALESCE(l24.plot_area, l23.plot_area, fd.plot_area) AS plot_area, | ||
fd.cadastral_classification AS cadastral_classification, | ||
COALESCE(l24.latitude, l23.latitude, fd.latitude) AS latitude, | ||
COALESCE(l24.longitude, l23.longitude, fd.longitude) AS longitude, | ||
COALESCE(l24.last_mutation_date, l23.last_mutation_date, fd.last_mutation_date) AS last_mutation_date, | ||
COALESCE(l24.last_transaction_date, l23.last_transaction_date) AS last_transaction_date, | ||
COALESCE(l24.last_transaction_value, l23.last_transaction_value) AS last_transaction_value, | ||
COALESCE(l24.occupancy_history, l23.occupancy_history) AS occupancy_history | ||
FROM ff_data fd | ||
FULL OUTER JOIN lovac_2024 l24 ON fd.ff_idlocal = l24.ff_idlocal | ||
FULL OUTER JOIN lovac_2023 l23 ON COALESCE(fd.ff_idlocal, l24.ff_idlocal) = l23.ff_idlocal |
42 changes: 42 additions & 0 deletions
42
analytics/dbt/models/marts/import/import_gold_housing_file_years.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
WITH ff_data AS ( | ||
SELECT | ||
ff_idlocal, | ||
ff_ccthp AS occupancy | ||
FROM {{ ref('int_ff_ext_2023') }} | ||
), | ||
lovac_2024 AS ( | ||
SELECT | ||
ff_idlocal | ||
FROM {{ ref('int_lovac_fil_2024') }} | ||
), | ||
lovac_2023 AS ( | ||
SELECT | ||
ff_idlocal | ||
FROM {{ ref('int_lovac_fil_2023') }} | ||
), | ||
lovac_history AS ( | ||
SELECT * FROM {{ ref('int_lovac_history_housing') }} | ||
) | ||
SELECT | ||
COALESCE(l24.ff_idlocal, l23.ff_idlocal, fd.ff_idlocal) AS local_id, | ||
CASE | ||
-- Si présent dans LOVAC 2024, absent de LOVAC 2023 et loué dans FF 2023 | ||
WHEN l24.ff_idlocal IS NOT NULL | ||
AND l23.ff_idlocal IS NULL | ||
AND fd.occupancy = 'L' THEN 'lovac-2024,ff-23' | ||
|
||
-- Si présent uniquement dans LOVAC 2024 | ||
WHEN l24.ff_idlocal IS NOT NULL THEN 'lovac-2024' | ||
|
||
-- Si présent uniquement dans LOVAC 2023 | ||
WHEN l23.ff_idlocal IS NOT NULL THEN 'lovac-2023' | ||
|
||
-- Si loué dans FF 2023 | ||
WHEN fd.occupancy = 'L' THEN 'ff-2023-locatif' | ||
|
||
ELSE NULL | ||
END AS data_file_years | ||
FROM ff_data fd | ||
FULL OUTER JOIN lovac_2024 l24 ON fd.ff_idlocal = l24.ff_idlocal | ||
FULL OUTER JOIN lovac_2023 l23 ON COALESCE(fd.ff_idlocal, l24.ff_idlocal) = l23.ff_idlocal | ||
WHERE fd.occupancy = 'L' |
6 changes: 6 additions & 0 deletions
6
analytics/dbt/models/marts/production/marts_production_campaigns.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,9 @@ | ||
{{ | ||
config( | ||
materialized='table', | ||
unique_key='campaign_id', | ||
) | ||
}} | ||
|
||
SELECT | ||
CAST(pc.id as VARCHAR) as campaign_id, | ||
|
12 changes: 10 additions & 2 deletions
12
analytics/dbt/models/marts/production/marts_production_establishments.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,18 @@ | ||
|
||
{{ | ||
config( | ||
materialized='table', | ||
unique_key='establishment_id', | ||
) | ||
}} | ||
|
||
SELECT | ||
CAST(pe.id as VARCHAR) AS establishment_id, | ||
pe.*, peu.*, pec.*, peg.*, pep.*, pecc.* | ||
pe.*, peu.*, pec.*, peg.*, pep.*, pecc.*, pes.* | ||
FROM {{ ref('int_production_establishments') }} pe | ||
LEFT JOIN {{ ref('int_production_establishments_users')}} peu ON pe.id = peu.establishment_id | ||
LEFT JOIN {{ ref('int_production_establishments_campaigns')}} pec ON pe.id = pec.establishment_id | ||
LEFT JOIN {{ ref('int_production_establishments_groups')}} peg ON pe.id = peg.establishment_id | ||
LEFT JOIN {{ ref('int_production_establishments_perimeters')}} pep ON pe.id = pep.establishment_id | ||
LEFT JOIN {{ ref('int_production_establishments_campaigns_contacts') }} pecc ON pe.id = pecc.establishment_id | ||
LEFT JOIN {{ ref('int_production_establishments_campaigns_contacts') }} pecc ON pe.id = pecc.establishment_id | ||
LEFT JOIN {{ ref('int_production_establishment_events_last_status') }} pes ON pe.id = pes.establishment_id |
1 change: 0 additions & 1 deletion
1
analytics/dbt/models/marts/production/marts_production_groups.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
|
||
SELECT | ||
pg.* | ||
FROM {{ ref('int_production_groups') }} pg |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
analytics/dbt/models/marts/production/marts_production_owners.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,9 @@ | ||
{{ | ||
config( | ||
materialized='table', | ||
unique_key='owner_id', | ||
) | ||
}} | ||
|
||
SELECT | ||
CAST(po.id AS VARCHAR) as owner_id, | ||
|
6 changes: 6 additions & 0 deletions
6
analytics/dbt/models/marts/production/marts_production_users.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,9 @@ | ||
{{ | ||
config( | ||
materialized='table', | ||
unique_key='user_id', | ||
) | ||
}} | ||
|
||
SELECT | ||
CAST(pu.id AS VARCHAR) user_id, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.