Skip to content

Commit

Permalink
Korjattu organisaatioiden nimiä, toimipisteen alla toimipiste
Browse files Browse the repository at this point in the history
  • Loading branch information
Matz Rasmus committed Dec 12, 2024
1 parent 1504f31 commit 2586bde
Showing 1 changed file with 46 additions and 19 deletions.
65 changes: 46 additions & 19 deletions dbt/models/int/int_organisaatio.sql
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,27 @@
)
}}

with organisaatio as (
with organisaatio_raw as (
select
*,
row_number() over (partition by organisaatio_oid order by muokattu desc) as rownr
from {{ ref('dw_organisaatio_organisaatio') }}
),

organisaatio as not materialized (
select * from organisaatio_raw where rownr = 1
),

ylempi_toimipiste as (
select
organisaatio_oid,
nimi_fi,
nimi_sv
from organisaatio
where organisaatiotyypit @> '["organisaatiotyyppi_03"]'
),


kunta as (
select * from {{ ref('int_koodisto_kunta') }} where viimeisin_versio
),
Expand All @@ -23,36 +37,49 @@ organisaatiotyyppi as (
select * from {{ ref('int_organisaatio_organisaatiotyyppi') }}
),

raw as (
int as (
select
organisaatio_oid,
coalesce(nimi_fi, nimi_sv) as nimi_fi_new,
coalesce(nimi_sv, nimi_fi) as nimi_sv_new,
coalesce(nimi_fi, nimi_sv) as nimi_en_new,
ylempi_organisaatio,
sijaintikunta,
tila,
opetuskielet,
organisaatiotyypit
from organisaatio
where rownr = 1
org1.organisaatio_oid,
coalesce(org1.nimi_fi, org1.nimi_sv) as nimi_fi_new,
coalesce(org1.nimi_sv, org1.nimi_fi) as nimi_sv_new,
coalesce(org1.nimi_fi, org1.nimi_sv) as nimi_en_new,
coalesce(ylto.nimi_fi, ylto.nimi_sv) as nimi_fi_new_ylempi,
coalesce(ylto.nimi_sv, ylto.nimi_fi) as nimi_sv_new_ylempi,
coalesce(ylto.nimi_fi, ylto.nimi_sv) as nimi_en_new_ylempi,
org1.ylempi_organisaatio,
org1.sijaintikunta,
org1.tila,
org1.opetuskielet,
org1.organisaatiotyypit
from organisaatio as org1
left join ylempi_toimipiste as ylto on org1.ylempi_organisaatio = ylto.organisaatio_oid
),

final as (
select
raw1.organisaatio_oid,
jsonb_build_object(
'en', raw1.nimi_en_new,
'sv', raw1.nimi_sv_new,
'fi', raw1.nimi_fi_new
) as organisaatio_nimi,
case
when nimi_fi_new_ylempi is not null
then
jsonb_build_object(
'en', nimi_en_new_ylempi || ', ' || nimi_en_new,
'sv', nimi_sv_new_ylempi || ', ' || nimi_sv_new,
'fi', nimi_fi_new_ylempi || ', ' || nimi_fi_new
)
else
jsonb_build_object(
'en', nimi_en_new,
'sv', nimi_sv_new,
'fi', nimi_fi_new
)
end as organisaatio_nimi,
raw1.ylempi_organisaatio,
raw1.sijaintikunta,
kunt.koodinimi as sijaintikunta_nimi,
raw1.opetuskielet,
orgt.organisaatiotyypit,
raw1.tila
from raw as raw1
from int as raw1
left join kunta as kunt on raw1.sijaintikunta = kunt.koodiuri
left join organisaatiotyyppi as orgt on raw1.organisaatio_oid = orgt.organisaatio_oid
)
Expand Down

0 comments on commit 2586bde

Please sign in to comment.