Skip to content

Commit

Permalink
Fix how to handle missing inventory position data for the first day o…
Browse files Browse the repository at this point in the history
…f the month
  • Loading branch information
ThiagoTrabach committed Sep 20, 2024
1 parent fbd726c commit 6b7d084
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 48 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
--- Gera a maior data com dados da posição de estoque até o primeiro dia do mês atual

with

primeiro_dia_mes as (select date_trunc(current_date('America/Sao_Paulo'), month) as data),

posicao as (
select * from {{ ref("fct_estoque_posicao") }} where sistema_origem = 'vitacare'
),

primeira_posicao_por_estabelecimento as (
select id_cnes, max(data_particao) as data_particao
from posicao
where data_particao <= (select data from primeiro_dia_mes)
group by id_cnes
)

select *
from primeira_posicao_por_estabelecimento
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,33 @@


with
-- SOURCES
movimento as (
posicao_inicial_por_estabelecimento as (
select *
from {{ ref("fct_estoque_movimento") }}
from
{{ ref('int_estoque__report_controlados__primeira_posicao_por_estabelecimento') }}
),

menor_data_posicao as (
select min(data_particao) as menor_data
from posicao_inicial_por_estabelecimento
),

primeiro_dia_mes as (
select date_trunc(current_date('America/Sao_Paulo'), month) as data
),

movimento as (
select mov.*

from {{ ref("fct_estoque_movimento") }} as mov

inner join posicao_inicial_por_estabelecimento as pos
on mov.id_cnes = pos.id_cnes and mov.data_particao >= pos.data_particao

where
sistema_origem = 'vitacare'
and material_quantidade <> 0
and data_particao >= date_sub(
current_date('America/Sao_Paulo'),
interval {{ dbt_date.day_of_month("current_date('America/Sao_Paulo')") }} -1 day
)
and id_cnes in ("2280787", "7523246", "2288370") -- Nilza Rosa (22), Nelio de
-- Oliveira (10), Pindaro de Carvalho (21)
mov.sistema_origem = 'vitacare'
and mov.material_quantidade <> 0

),

posicao as (
Expand All @@ -31,11 +45,7 @@ with
data_particao,
sum(material_quantidade) as posicao_quantidade
from {{ ref("fct_estoque_posicao") }}
where
data_particao = date_sub(
current_date('America/Sao_Paulo'),
interval {{ dbt_date.day_of_month("current_date('America/Sao_Paulo')") }} -1 day
)
inner join posicao_inicial_por_estabelecimento using (id_cnes, data_particao)
group by 1, 2, 3
),

Expand Down Expand Up @@ -168,8 +178,8 @@ with
order by id_cnes, id_material, ordem
),

-- FINAL TABLE
final as (
--
joined as (
select
eventos.*,
coalesce(posicao_quantidade, 0) as posicao_inicial,
Expand All @@ -184,36 +194,45 @@ with
from eventos_final as eventos
left join posicao using (id_cnes, id_material)
left join estabelecimento as est using (id_cnes)
),

final as (
select
id_cnes,
estabelecimento_nome,
estabelecimento_area_programatica,
estabelecimento_endereco,
id_material,
upper(
trim(
regexp_replace(
regexp_replace(normalize(nome, nfd), r"\pM", ''),
r'[^ A-Za-z0-9.,]',
' '
)
)
) as nome,
controlado_tipo,
id_lote,
format_date('%d-%m-%Y', data_validade) as data_validade_br,
if(tipo_evento = "saida", "saída", tipo_evento) as tipo_evento,
evento,
movimento_tipo,
movimento_justificativa,
data_evento,
format_date('%d-%m-%Y', data_evento) as data_evento_br,
ordem,
-- posicao_inicial,
movimento_quantidade,
-- movimento_quantidade_acumulada,
posicao_final
from joined
where data_evento >= (select data from primeiro_dia_mes)
)

select
id_cnes,
estabelecimento_nome,
estabelecimento_area_programatica,
estabelecimento_endereco,
id_material,
upper(
trim(
regexp_replace(
regexp_replace(normalize(nome, nfd), r"\pM", ''),
r'[^ A-Za-z0-9.,]',
' '
)
)
) as nome,
controlado_tipo,
id_lote,
format_date('%d-%m-%Y', data_validade) as data_validade_br,
if(tipo_evento = "saida", "saída", tipo_evento) as tipo_evento,
evento,
movimento_tipo,
movimento_justificativa,
data_evento,
format_date('%d-%m-%Y', data_evento) as data_evento_br,
ordem,
-- posicao_inicial,
movimento_quantidade,
-- movimento_quantidade_acumulada,
posicao_final
select *
from final

where
id_cnes in ("2280787", "7523246", "2288370", "2280795") -- Nilza Rosa (22), Nelio de
-- Oliveira (10), Pindaro de Carvalho (21)

Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ with
estabelecimento as (
select *, concat(endereco_logradouro, ', ', endereco_numero) as endereco
from {{ ref("dim_estabelecimento") }}
),

farmaceuticos as (
select id_cnes, array_agg(struct(farmaceutico_nome as nome, farmaceutico_crf as crf)) as farmaceutico
from {{ ref("raw_sheets__aps_farmacias") }}
group by 1
)

select
Expand All @@ -35,6 +41,8 @@ select
est.area_programatica as estabelecimento_area_programatica,
est.endereco as estabelecimento_endereco,
cc.controlado_tipo,
f.farmaceutico,
from cnes_controlados as cc
left join estabelecimento as est using (id_cnes)
left join farmaceuticos as f using (id_cnes)
order by est.nome_limpo, cc.controlado_tipo

0 comments on commit 6b7d084

Please sign in to comment.