Skip to content

Commit

Permalink
Merge pull request #13 from Jean-Baptiste-N/Dev
Browse files Browse the repository at this point in the history
big work
  • Loading branch information
Jean-Baptiste-N authored Dec 2, 2024
2 parents fd2984b + b6245a3 commit b115735
Show file tree
Hide file tree
Showing 14 changed files with 533 additions and 28 deletions.
7 changes: 7 additions & 0 deletions dbt_project.yml
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,11 @@ models:
+materialized: table
join_total_morbidite_capacite:
+schema: join_total_morbidite_capacite
+materialized: table

capacite:
+schema: capacite
+materialized: view
capacite_agg:
+schema: capacite_agg
+materialized: table
25 changes: 25 additions & 0 deletions models/capacite/int_capacite_etablissement.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
SELECT
fi as id_etablissement,
an as annee,
rs as nom_etablissement,
stj as statut_juridique,
cat as categ_etablissement,
dep as departement,
SPLIT(dep, " - ")[SAFE_OFFSET(0)] AS code_departement,
SPLIT(dep, " - ")[SAFE_OFFSET(1)] AS nom_departement,
reg as region,
SPLIT(reg, " - ")[SAFE_OFFSET(0)] AS code_region,
SPLIT(reg, " - ")[SAFE_OFFSET(1)] AS nom_region,
espic as code_espic,
LIT as lit_hospi_complete,
SEJHC as sejour_hospi_complete,
JOU as journee_hospi_complete,
PLA as place_hospi_partielle,
SEJHP as sejour_hospi_partielle,
PAS as passage_urgence,
SEJACC as sejour_accouchement,
SEHEM as seance_hemodyalise,
SERAD as seance_radiotherapie,
SECHI as seance_chimio,
FROM {{ref("stg_capacite_services_h__capacite_2022_par_etablissement")}}
WHERE an >= 2018
51 changes: 51 additions & 0 deletions models/capacite/int_capacite_etablissement_join.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
With join_capacite as (
SELECT
t2.id_etablissement,
t2.annee as annee,
DATE(CAST(t2.annee AS STRING) || "-12-31") as year,
t2.service_medical,
t1.nom_etablissement AS nom_etablissement,
t1.statut_juridique AS statut_juridique,
t1.categ_etablissement AS categ_etablissement,
t1.departement AS departement,
t1.code_departement AS code_departement,
t1.nom_departement AS nom_departement,
t1.region AS region,
t1.code_region AS code_region,
t1.nom_region AS nom_region,
t1.code_espic AS code_espic,
t2.lit_hospi_complete AS lit_hospi_complete_detail,
t2.sejour_hospi_complete AS sejour_hospi_complete_detail,
t2.journee_hospi_complete AS journee_hospi_complete_detail,
t2.place_hospi_partielle AS place_hospi_partielle_detail,
t2.sejour_hospi_partielle AS sejour_hospi_partielle_detail,
t2.passage_urgence AS passage_urgence_detail,
t2.sejour_accouchement AS sejour_accouchement_detail,
t2.seance_hemodyalise AS seance_hemodyalise_detail,
t2.seance_radiotherapie AS seance_radiotherapie_detail,
t2.seance_chimio AS seance_chimio_detail,
t1.lit_hospi_complete AS total_lit_hospi_complete,
t1.sejour_hospi_complete AS total_sejour_hospi_complete,
t1.journee_hospi_complete AS total_journee_hospi_complete,
t1.place_hospi_partielle AS total_place_hospi_partielle,
t1.sejour_hospi_partielle AS total_sejour_hospi_partielle,
FROM
{{ref("int_capacite_etablissement")}} AS t1
JOIN
{{ref("int_capacite_service")}} AS t2
ON
t1.id_etablissement = t2.id_etablissement
AND t1.annee = t2.annee
)

SELECT *,
CASE
WHEN service_medical = 'Soins de suite et réadaptation' THEN 'SSR'
WHEN service_medical = 'Gynéco-Obstétrique' THEN 'O'
WHEN service_medical = 'Médecine' THEN 'M'
WHEN service_medical = 'Chirurgie' THEN 'C'
WHEN service_medical = 'Soins de longue durée' THEN 'ESND'
WHEN service_medical = 'Psychiatrie' THEN 'PSY'
WHEN service_medical = 'Hospitalisation à domicile' THEN NULL
END AS service_classification ,
FROM `join_capacite`
63 changes: 63 additions & 0 deletions models/capacite/int_capacite_service.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
-- With stjr1 as (
-- SELECT
-- t1.*,
-- t2.stjr
-- FROM {{ref('stg_capacite_services_h__capacite_2022_par_services')}} t1
-- LEFT JOIN (
-- SELECT DISTINCT FI, AN, stjr
-- FROM {{ref('stg_capacite_services_h__capacite_2021_service_stjr')}}
-- WHERE AN = 2021
-- ) t2
-- ON t1.fi = t2.FI
-- WHERE t1.an >= 2018 AND t1.an < 2022
-- ),
-- stjr2 as (
-- SELECT
-- t1.*,
-- t2.stjr
-- FROM {{ref('stg_capacite_services_h__capacite_2022_par_services')}} t1
-- LEFT JOIN (
-- SELECT DISTINCT fi, an, stjr
-- FROM {{ref('stg_capacite_services_h__capacite_2022_etablissement_stjr')}}
-- WHERE AN = 2022
-- ) t2
-- ON CAST(t1.fi AS STRING) = t2.fi
-- WHERE t1.an = 2022
-- ),
-- join_stjr as (
-- SELECT *
-- FROM stjr1
-- UNION ALL
-- SELECT *
-- FROM stjr2
-- )

SELECT
fi as id_etablissement,
an as annee,
rs as nom_etablissement,
stj as statut_juridique,
cat as categ_etablissement,
dep as departement,
SPLIT(dep, " - ")[SAFE_OFFSET(0)] AS code_departement,
SPLIT(dep, " - ")[SAFE_OFFSET(1)] AS nom_departement,
reg as region,
SPLIT(reg, " - ")[SAFE_OFFSET(0)] AS code_region,
SPLIT(reg, " - ")[SAFE_OFFSET(1)] AS nom_region,
-- stjr as statut_entite,
espic as code_espic,
DISCI as discipline_equipement,
DISCIPLINE as service_medical,
LIT as lit_hospi_complete,
SEJHC as sejour_hospi_complete,
JOU as journee_hospi_complete,
PLA as place_hospi_partielle,
SEJHP as sejour_hospi_partielle,
PAS as passage_urgence,
SEJACC as sejour_accouchement,
SEHEM as seance_hemodyalise,
SERAD as seance_radiotherapie,
SECHI as seance_chimio,
cle_unique
FROM {{ref('stg_capacite_services_h__capacite_2022_par_services')}}
WHERE an >= 2018
103 changes: 103 additions & 0 deletions models/capacite_agg/mart_capacite_dpt_reg.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
WITH dpt as (
SELECT
id_etablissement,
annee,
year,
service_medical,
service_classification,
nom_etablissement,
statut_juridique,
categ_etablissement,
departement as region,
code_departement as code_region,
nom_departement as nom_region,
code_espic,
lit_hospi_complete_detail,
sejour_hospi_complete_detail,
journee_hospi_complete_detail,
place_hospi_partielle_detail,
sejour_hospi_partielle_detail,
passage_urgence_detail,
sejour_accouchement_detail,
seance_hemodyalise_detail,
seance_radiotherapie_detail,
seance_chimio_detail,
total_lit_hospi_complete,
total_sejour_hospi_complete,
total_journee_hospi_complete,
total_place_hospi_partielle,
total_sejour_hospi_partielle,
FROM {{ref("int_capacite_etablissement_join")}}
),
reg as (
SELECT
id_etablissement,
annee,
year,
service_medical,
service_classification,
nom_etablissement,
statut_juridique,
categ_etablissement,
region,
code_region,
nom_region,
code_espic,
lit_hospi_complete_detail,
sejour_hospi_complete_detail,
journee_hospi_complete_detail,
place_hospi_partielle_detail,
sejour_hospi_partielle_detail,
passage_urgence_detail,
sejour_accouchement_detail,
seance_hemodyalise_detail,
seance_radiotherapie_detail,
seance_chimio_detail,
total_lit_hospi_complete,
total_sejour_hospi_complete,
total_journee_hospi_complete,
total_place_hospi_partielle,
total_sejour_hospi_partielle,
FROM {{ref("int_capacite_etablissement_join")}}
),
union1 as (
SELECT *
FROM dpt
UNION ALL
SELECT *
FROM reg
)

SELECT
annee,
year,
region,
code_region,
nom_region,
service_medical,
service_classification,
SUM(lit_hospi_complete_detail) as lit_hospi_complete,
SUM(sejour_hospi_complete_detail) as sejour_hospi_complete,
SUM(journee_hospi_complete_detail) as journee_hospi_complete,
SUM(place_hospi_partielle_detail) as place_hospi_partielle,
SUM(sejour_hospi_partielle_detail) as sejour_hospi_partielle,
SUM(passage_urgence_detail) as passage_urgence,
SUM(sejour_accouchement_detail) as sejour_accouchement,
SUM(seance_hemodyalise_detail) as seance_hemodyalise,
SUM(seance_radiotherapie_detail) as seance_radiotherapie,
SUM(seance_chimio_detail) as seance_chimio,
FROM union1
GROUP BY
annee,
year,
region,
code_region,
nom_region,
service_medical,
service_classification






69 changes: 69 additions & 0 deletions models/capacite_agg/mart_capacite_dpt_reg_evol.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
WITH base_data AS (
SELECT
annee,
year,
region,
code_region,
nom_region,
service_medical,
service_classification,
lit_hospi_complete,
sejour_hospi_complete,
journee_hospi_complete,
place_hospi_partielle,
sejour_hospi_partielle,
passage_urgence
,AVG(lit_hospi_complete) OVER (PARTITION BY annee, region, code_region, nom_region, service_medical, service_classification) AS lit_hospi_complete1
,AVG(sejour_hospi_complete) OVER (PARTITION BY annee, region, code_region, nom_region, service_medical, service_classification) AS sejour_hospi_complete1
,AVG(journee_hospi_complete) OVER (PARTITION BY annee, region, code_region, nom_region, service_medical, service_classification) AS journee_hospi_complete1
,AVG(place_hospi_partielle) OVER (PARTITION BY annee, region, code_region, nom_region, service_medical, service_classification) AS place_hospi_partielle1
,AVG(sejour_hospi_partielle) OVER (PARTITION BY annee, region, code_region, nom_region, service_medical, service_classification) AS sejour_hospi_partielle1
,AVG(passage_urgence) OVER (PARTITION BY annee, region, code_region, nom_region, service_medical, service_classification) AS passage_urgence1
FROM {{ref("mart_capacite_dpt_reg")}}
),
evolution_data AS (
SELECT
bd1.*,
round(bd1.lit_hospi_complete1 - COALESCE(bd2.lit_hospi_complete1, 0),2) AS evolution_lit_hospi_complete1,
CASE
WHEN COALESCE(bd2.lit_hospi_complete1, 0) = 0 THEN NULL
ELSE Round(safe_divide((bd1.lit_hospi_complete1 - bd2.lit_hospi_complete1) , bd2.lit_hospi_complete1), 2)
END AS evolution_percent_lit_hospi_complete1,
round(bd1.sejour_hospi_complete1 - COALESCE(bd2.sejour_hospi_complete1, 0),2) AS evolution_sejour_hospi_complete1,
CASE
WHEN COALESCE(bd2.sejour_hospi_complete1, 0) = 0 THEN NULL
ELSE Round(safe_divide((bd1.sejour_hospi_complete1 - bd2.sejour_hospi_complete1) , bd2.sejour_hospi_complete1), 2)
END AS evolution_percent_sejour_hospi_complete1,
round(bd1.journee_hospi_complete1 - COALESCE(bd2.journee_hospi_complete1, 0),2) AS evolution_journee_hospi_complete1,
CASE
WHEN COALESCE(bd2.journee_hospi_complete1, 0) = 0 THEN NULL
ELSE Round(safe_divide((bd1.journee_hospi_complete1 - bd2.journee_hospi_complete1) , bd2.journee_hospi_complete1), 2)
END AS evolution_percent_journee_hospi_complete1,
round(bd1.place_hospi_partielle1 - COALESCE(bd2.place_hospi_partielle1, 0),2) AS evolution_place_hospi_partielle1,
CASE
WHEN COALESCE(bd2.place_hospi_partielle1, 0) = 0 THEN NULL
ELSE Round(safe_divide((bd1.place_hospi_partielle1 - bd2.place_hospi_partielle1) , bd2.place_hospi_partielle1), 2)
END AS evolution_percent_place_hospi_partielle1,
round(bd1.sejour_hospi_partielle1 - COALESCE(bd2.sejour_hospi_partielle1, 0),2) AS evolution_sejour_hospi_partielle1,
CASE
WHEN COALESCE(bd2.sejour_hospi_partielle1, 0) = 0 THEN NULL
ELSE Round(safe_divide((bd1.sejour_hospi_partielle1 - bd2.sejour_hospi_partielle1) , bd2.sejour_hospi_partielle1), 2)
END AS evolution_percent_sejour_hospi_partielle1,
round(bd1.passage_urgence1 - COALESCE(bd2.passage_urgence1, 0),2) AS evolution_passage_urgence1,
CASE
WHEN COALESCE(bd2.passage_urgence1, 0) = 0 THEN NULL
ELSE Round(safe_divide((bd1.passage_urgence1 - bd2.passage_urgence1) , bd2.passage_urgence1), 2)
END AS evolution_percent_passage_urgence1
FROM base_data bd1
LEFT JOIN base_data bd2
ON bd1.region = bd2.region
AND bd1.code_region = bd2.code_region
AND bd1.nom_region = bd2.nom_region
AND bd1.service_medical = bd2.service_medical
AND bd1.service_classification = bd2.service_classification
AND bd1.annee = bd2.annee + 1
)
SELECT *,
CONCAT(region , "_" , annee , "_" , service_classification) as cle_unique
FROM evolution_data
ORDER BY region, annee
3 changes: 2 additions & 1 deletion models/join_total_morbidite/class_join_total_morbidite.sql
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ WITH mega_join AS (
ON t1.cle_unique = t3.cle_unique
WHERE
t1.sexe = 'Ensemble' AND t3.sexe = 'Ensemble'
AND t1.tranche_age = 'Tous âges confondus'
)


Expand All @@ -82,4 +83,4 @@ SELECT
FROM mega_join AS t1
LEFT JOIN {{ref("stg_morbidite_h__class_services")}} c2
ON t1.nom_pathologie = c2.pathologie

ORDER BY cle_unique
Original file line number Diff line number Diff line change
@@ -1,23 +1,27 @@
WITH population as (
WITH
join_dpt AS (
SELECT
CAST(code_departement as INT64) as code_region
,nom_departement as region
,CONCAT(code_departement ," - ", nom_departement) as cle_region
,annee
,population
FROM {{ref("stg_pop_departement__population_departement")}}
UNION ALL
t1.*,
t2.population AS population
FROM {{ ref("class_join_total_morbidite") }} t1
LEFT JOIN {{ ref("stg_pop_departement__population_departement") }} t2
ON t1.annee = t2.annee
AND t1.nom_region = t2.nom_departement
WHERE t1.niveau = 'Départements'
),
join_reg AS (
SELECT
code_region
,region
,CONCAT(code_region ," - ", region) as cle_region
,annee
,population
FROM {{ref("stg_pop_departement__population_region")}}
)
SELECT
t1.*
,t2.population
FROM {{ref("class_join_total_morbidite")}} t1
LEFT JOIN population as t2
ON t1.annee = t2.annee AND t1.region = t2.cle_region
t1.*,
t2.population AS population
FROM {{ ref("class_join_total_morbidite") }} t1
LEFT JOIN {{ ref("stg_pop_departement__population_region") }} t2
ON t1.annee = t2.annee
AND t1.nom_region = t2.region
WHERE t1.niveau = 'Régions'
)

SELECT *
FROM join_dpt
UNION ALL
SELECT *
FROM join_reg
Loading

0 comments on commit b115735

Please sign in to comment.