Skip to content

Commit

Permalink
Updates cartographie model to include texs dd
Browse files Browse the repository at this point in the history
  • Loading branch information
Lawiss committed Nov 15, 2024
1 parent bb44f1d commit 4dd2461
Show file tree
Hide file tree
Showing 4 changed files with 147 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
version: 2
models:
- name: etablissements_texs_dd
columns:
- name: siret
description: 'Code région'
data_type: text
- name: num_texs_dd_as_emitter
description: ''
data_type: bigint
- name: quantity_texs_dd_as_emitter
description: ''
data_type: numeric
- name: num_texs_dd_as_transporter
description: ''
data_type: bigint
- name: quantity_texs_dd_as_transporter
description: ''
data_type: numeric
- name: num_texs_dd_as_destination
description: ''
data_type: bigint
- name: quantity_texs_dd_as_destination
description: ''
data_type: numeric
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
{{
config(
materialized = 'table',
indexes = [
{
"columns":["siret"],
"unique":true
}
]
)
}}

with emitter_stats as (
select
emitter_company_siret,
count(distinct id) as num_texs_dd_as_emitter,
sum(quantity_received) as quantity_texs_dd_as_emitter
from {{ ref('bordereaux_enriched') }}
where waste_code in ('17 05 03*', '17 05 05*')
group by 1
),

transporter_stats as (

select
transporter_company_siret,
count(distinct bs_id) as num_texs_dd_as_transporter,
sum(quantity_received) as quantity_texs_dd_as_transporter
from (
select
form_id as bs_id,
transporter_company_siret,
quantity_received
from
{{ ref('bsdd_transporter_enriched') }}
where waste_details_code in ('17 05 03*', '17 05 05*')
union all
select
bsda_id as bs_id,
transporter_company_siret,
destination_reception_weight as quantity_received
from
{{ ref('bsda_transporter_enriched') }}
where waste_code in ('17 05 03*', '17 05 05*')
) as t
group by 1
),

destination_stats as (
select
destination_company_siret,
count(distinct id) as num_texs_dd_as_destination,
sum(quantity_received) as quantity_texs_dd_as_destination
from {{ ref('bordereaux_enriched') }}
where waste_code in ('17 05 03*', '17 05 05*')
group by 1
)

select
coalesce(
emitter_company_siret,
transporter_company_siret,
destination_company_siret
) as siret,
coalesce(
num_texs_dd_as_emitter, 0
) as num_texs_dd_as_emitter,
coalesce(
quantity_texs_dd_as_emitter, 0
) as quantity_texs_dd_as_emitter,
coalesce(
num_texs_dd_as_transporter, 0
) as num_texs_dd_as_transporter,
coalesce(
quantity_texs_dd_as_transporter, 0
) as quantity_texs_dd_as_transporter,
coalesce(
num_texs_dd_as_destination, 0
) as num_texs_dd_as_destination,
coalesce(
quantity_texs_dd_as_destination, 0
) as quantity_texs_dd_as_destination
from emitter_stats as es
full outer join
transporter_stats as ts
on es.emitter_company_siret = ts.transporter_company_siret
full outer join
destination_stats as ds
on
coalesce(es.emitter_company_siret, ts.transporter_company_siret)
= ds.destination_company_siret
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ models:
- name: siret
description: 'Code région'
data_type: text
- name: nom_etablissement
description: ''
data_type: text
- name: profils
description: ''
data_type: ARRAY
Expand Down Expand Up @@ -32,6 +35,9 @@ models:
- name: bsvhu
description: ''
data_type: boolean
- name: texs_dd
description: ''
data_type: boolean
- name: dnd
description: ''
data_type: boolean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,24 +49,27 @@ with "stats" as (
joined as (
select
s.*,
c.company_types as profils,
c.collector_types as profils_collecteur,
c.waste_processor_types as profils_installation,
se.code_commune_etablissement as code_commune_insee,
cgc.code_departement as code_departement_insee,
cgc.code_region as code_region_insee,
c.address as adresse_td,
c.latitude as latitude_td,
c.longitude as longitude_td,
cban.latitude as latitude_ban,
cban.longitude as longitude_ban,
c.company_types as profils,
c.collector_types as profils_collecteur,
c.waste_processor_types as profils_installation,
se.code_commune_etablissement as code_commune_insee,
cgc.code_departement as code_departement_insee,
cgc.code_region as code_region_insee,
c.address as adresse_td,
c.latitude as latitude_td,
c.longitude as longitude_td,
cban.latitude as latitude_ban,
cban.longitude as longitude_ban,
et.num_texs_dd_as_emitter > 0
or et.num_texs_dd_as_transporter > 0
or et.num_texs_dd_as_destination > 0 as texs_dd,
st_setsrid(
st_point(
coalesce(c.latitude, cban.latitude),
coalesce(c.longitude, cban.longitude)
),
4326
) as coords,
) as coords,
nullif(
coalesce(se.complement_adresse_etablissement || ' ', '')
|| coalesce(se.numero_voie_etablissement || ' ', '')
Expand All @@ -77,8 +80,15 @@ joined as (
|| coalesce(se.libelle_commune_etablissement || ' ', '')
|| coalesce(se.libelle_commune_etranger_etablissement || ' ', '')
|| coalesce(se.distribution_speciale_etablissement, ''), ''
) as adresse_insee
) as adresse_insee,
coalesce(
c.name, se.enseigne_1_etablissement,
se.enseigne_2_etablissement,
se.enseigne_3_etablissement,
se.denomination_usuelle_etablissement
) as nom_etablissement
from "stats" as s
left join {{ ref('etablissements_texs_dd') }} as et on s.siret = et.siret
left join {{ ref("stock_etablissement") }} as se on s.siret = se.siret
left join {{ ref("company") }} as c on s.siret = c.siret
left join
Expand All @@ -93,6 +103,7 @@ joined as (

select
siret,
nom_etablissement,
profils,
profils_collecteur,
profils_installation,
Expand All @@ -102,6 +113,7 @@ select
bsff,
bsdasri,
bsvhu,
texs_dd,
dnd,
texs,
ssd,
Expand Down

0 comments on commit 4dd2461

Please sign in to comment.