diff --git a/pipeline/dbt/models/staging/sources/mediation_numerique/stg_mediation_numerique__structures.sql b/pipeline/dbt/models/staging/sources/mediation_numerique/stg_mediation_numerique__structures.sql index e26ea0f4..9cbc90b6 100644 --- a/pipeline/dbt/models/staging/sources/mediation_numerique/stg_mediation_numerique__structures.sql +++ b/pipeline/dbt/models/staging/sources/mediation_numerique/stg_mediation_numerique__structures.sql @@ -20,42 +20,52 @@ labels_nationaux AS ( FROM labels_nationaux_prep WHERE labels_nationaux IN ( - SELECT labels_nationaux."value" FROM labels_nationaux_seed + SELECT labels_nationaux_seed."value" FROM labels_nationaux_seed ) GROUP BY id ), final AS ( SELECT - _di_source_id AS "_di_source_id", - (ARRAY(SELECT * FROM JSONB_ARRAY_ELEMENTS_TEXT(NULLIF(data -> 'thematiques', 'null'))))::TEXT [] AS "thematiques", - ((data ->> 'longitude'))::FLOAT AS "longitude", - ((data ->> 'latitude'))::FLOAT AS "latitude", - ((data ->> 'date_maj'))::TIMESTAMP WITH TIME ZONE AS "date_maj", + source._di_source_id AS "_di_source_id", + ( + ARRAY( + SELECT thematiques.* + FROM JSONB_ARRAY_ELEMENTS_TEXT(NULLIF(source.data -> 'thematiques', 'null')) AS thematiques + ) + )::TEXT [] AS "thematiques", + ((source.data ->> 'longitude'))::FLOAT AS "longitude", + ((source.data ->> 'latitude'))::FLOAT AS "latitude", + ((source.data ->> 'date_maj'))::TIMESTAMP WITH TIME ZONE AS "date_maj", -- SOURCEFIX(2024-10-14) -- SOURCEFIX(2024-11-18) - labels_nationaux.labels_nationaux AS "labels_nationaux", - (ARRAY(SELECT * FROM JSONB_ARRAY_ELEMENTS_TEXT(NULLIF(data -> 'labels_autres', 'null'))))::TEXT [] AS "labels_autres", - data ->> 'id' AS "id", - data ->> 'nom' AS "nom", - NULLIF(data ->> 'siret', REPEAT('0', 14)) AS "siret", - data ->> 'source' AS "source", - NULLIF(TRIM(data ->> 'adresse'), '') AS "adresse", - NULLIF(TRIM(data ->> 'commune'), '') AS "commune", - NULLIF(TRIM(data ->> 'courriel'), '') AS "courriel", - data ->> 'site_web' AS "site_web", - NULLIF(TRIM(data ->> 'telephone'), '') AS "telephone", - data ->> 'code_postal' AS "code_postal", - data ->> 'code_insee' AS "code_insee", - data ->> 'horaires_ouverture' AS "horaires_ouverture", - data ->> 'typologie' AS "typologie", - data ->> 'presentation_resume' AS "presentation_resume", - data ->> 'accessibilite' AS "accessibilite", - data ->> 'presentation_detail' AS "presentation_detail" + labels_nationaux.labels_nationaux AS "labels_nationaux", + ( + ARRAY( + SELECT labels_autres.* + FROM JSONB_ARRAY_ELEMENTS_TEXT(NULLIF(source.data -> 'labels_autres', 'null')) AS labels_autres + ) + )::TEXT [] AS "labels_autres", + source.data ->> 'id' AS "id", + source.data ->> 'nom' AS "nom", + NULLIF(source.data ->> 'siret', REPEAT('0', 14)) AS "siret", + source.data ->> 'source' AS "source", + NULLIF(TRIM(source.data ->> 'adresse'), '') AS "adresse", + NULLIF(TRIM(source.data ->> 'commune'), '') AS "commune", + NULLIF(TRIM(source.data ->> 'courriel'), '') AS "courriel", + source.data ->> 'site_web' AS "site_web", + NULLIF(TRIM(source.data ->> 'telephone'), '') AS "telephone", + source.data ->> 'code_postal' AS "code_postal", + source.data ->> 'code_insee' AS "code_insee", + source.data ->> 'horaires_ouverture' AS "horaires_ouverture", + source.data ->> 'typologie' AS "typologie", + source.data ->> 'presentation_resume' AS "presentation_resume", + source.data ->> 'accessibilite' AS "accessibilite", + source.data ->> 'presentation_detail' AS "presentation_detail" FROM source LEFT JOIN labels_nationaux ON source.data ->> 'id' = labels_nationaux.id WHERE - data ->> 'source' != 'dora' + source.data ->> 'source' != 'dora' ) SELECT * FROM final