Skip to content

Commit

Permalink
Subsidio - Materializa tabelas sumario_servico_dia_historico e sumari…
Browse files Browse the repository at this point in the history
…o_servico_dia_tipo (#334)

* materializa sumario_servico_dia_historico e tipo

* ajusta filtro

* link pr

* descomenta refs

* remove comentario
  • Loading branch information
pixuimpou authored Jun 6, 2024
1 parent 7ee9b89 commit 0a8f8c0
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 29 deletions.
6 changes: 6 additions & 0 deletions models/dashboard_subsidio_sppo/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog - dashboard_subsidio_sppo

## [7.1.0] - 2024-06-06

### Alterado

- Altera materialização dos modelos `sumario_servico_dia_historico.sql` e `sumario_servico_dia_tipo.sql` de view para incremental (https://github.com/prefeitura-rio/queries-rj-smtr/pull/334)

## [7.0.0] - 2024-05-22

### Adicionado
Expand Down
57 changes: 39 additions & 18 deletions models/dashboard_subsidio_sppo/sumario_servico_dia_historico.sql
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
{{
config(
materialized="incremental",
partition_by={"field": "data", "data_type": "date", "granularity": "day"},
incremental_strategy="insert_overwrite",
)
}}

WITH
viagem_planejada AS (
(
Expand All @@ -7,13 +15,15 @@ WITH
o.vista
FROM
{{ ref("viagem_planejada") }} AS v
--`rj-smtr`.`projeto_subsidio_sppo`.`viagem_planejada`
-- `rj-smtr`.`projeto_subsidio_sppo`.`viagem_planejada` AS v
LEFT JOIN
{{ ref("subsidio_data_versao_efetiva") }}
-- rj-smtr.projeto_subsidio_sppo.subsidio_data_versao_efetiva
USING
(data)
LEFT JOIN
{{ ref("ordem_servico_gtfs") }} AS o
-- rj-smtr.gtfs.ordem_servico AS o
USING
(feed_start_date, servico, tipo_os)
WHERE
Expand All @@ -27,7 +37,7 @@ WITH
vista
FROM
{{ ref("viagem_planejada") }}
--`rj-smtr`.`projeto_subsidio_sppo`.`viagem_planejada`
-- `rj-smtr`.`projeto_subsidio_sppo`.`viagem_planejada`
WHERE
(id_tipo_trajeto = 0
OR id_tipo_trajeto IS NULL)
Expand Down Expand Up @@ -92,7 +102,7 @@ WITH
0 AS valor_penalidade
FROM
{{ ref("sumario_servico_dia_tipo") }}
--`rj-smtr`.`dashboard_subsidio_sppo`.`sumario_servico_dia_tipo`
-- `rj-smtr`.`dashboard_subsidio_sppo`.`sumario_servico_dia_tipo`
WHERE
DATA BETWEEN "2023-07-16"
AND "2023-08-31"),
Expand All @@ -109,21 +119,32 @@ WITH
sumario_com_glosa AS s
USING
( `data`,
servico ))
SELECT
*
FROM
sumario_sem_glosa
UNION ALL (
servico )),
dados_completos AS (
SELECT
*
*
FROM
sumario_com_glosa
sumario_sem_glosa
UNION ALL (
SELECT
*
FROM
sumario_com_glosa
WHERE
`data` < "2023-07-16"
OR `data` > "2023-08-31" )
UNION ALL (
SELECT
*
FROM
sumario_glosa_suspensa )
)
SELECT
*
FROM
dados_completos
{% if is_incremental() %}
WHERE
`data` < "2023-07-16"
OR `data` > "2023-08-31" )
UNION ALL (
SELECT
*
FROM
sumario_glosa_suspensa )
data BETWEEN DATE("{{ var("start_date") }}" )
AND DATE( "{{ var("end_date") }}" )
{% endif %}
46 changes: 35 additions & 11 deletions models/dashboard_subsidio_sppo/sumario_servico_dia_tipo.sql
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
{{
config(
materialized="incremental",
partition_by={"field": "data", "data_type": "date", "granularity": "day"},
incremental_strategy="insert_overwrite",
)
}}

WITH
planejado AS (
SELECT
Expand All @@ -6,25 +14,33 @@ WITH
consorcio,
servico,
distancia_total_planejada AS km_planejada
FROM
FROM
-- rj-smtr.projeto_subsidio_sppo.viagem_planejada
{{ ref("viagem_planejada") }}
WHERE
DATA BETWEEN DATE( "{{ var("DATA_SUBSIDIO_V2_INICIO") }}" )
AND DATE( "{{ var("end_date") }}" )
DATA >= DATE( "{{ var("DATA_SUBSIDIO_V2_INICIO") }}" )
{% if is_incremental() %}
AND DATA BETWEEN DATE("{{ var("start_date") }}" )
AND DATE( "{{ var("end_date") }}" )
{% endif %}
AND (distancia_total_planejada > 0
OR distancia_total_planejada IS NOT NULL)

),
veiculos AS (
SELECT
DATA,
id_veiculo,
status
FROM
-- rj-smtr.veiculo.sppo_veiculo_dia
{{ ref("sppo_veiculo_dia") }}
WHERE
DATA BETWEEN DATE( "{{ var("DATA_SUBSIDIO_V2_INICIO") }}" )
AND DATE( "{{ var("end_date") }}" )),
DATA >= DATE( "{{ var("DATA_SUBSIDIO_V2_INICIO") }}" )
{% if is_incremental() %}
AND DATA BETWEEN DATE("{{ var("start_date") }}" )
AND DATE( "{{ var("end_date") }}" )
{% endif %}
),
viagem AS (
SELECT
DATA,
Expand All @@ -33,10 +49,15 @@ WITH
id_viagem,
distancia_planejada
FROM
-- rj-smtr.projeto_subsidio_sppo.viagem_completa
{{ ref("viagem_completa") }}
WHERE
DATA BETWEEN DATE( "{{ var("DATA_SUBSIDIO_V2_INICIO") }}" )
AND DATE( "{{ var("end_date") }}" )),
DATA >= DATE( "{{ var("DATA_SUBSIDIO_V2_INICIO") }}" )
{% if is_incremental() %}
AND DATA BETWEEN DATE("{{ var("start_date") }}" )
AND DATE( "{{ var("end_date") }}" )
{% endif %}
),
servico_km_tipo AS (
SELECT
v.DATA,
Expand Down Expand Up @@ -116,11 +137,14 @@ SELECT
consorcio)
FROM
{{ ref("sumario_servico_dia") }} AS sd
-- rj-smtr.dashboard_subsidio_sppo.sumario_servico_dia AS sd
LEFT JOIN
pivot_data AS pd
ON
sd.data = pd.data
AND sd.servico = pd.servico
ORDER BY
DATA,
servico
{% if is_incremental() %}
WHERE
sd.data BETWEEN DATE("{{ var("start_date") }}" )
AND DATE( "{{ var("end_date") }}" )
{% endif %}

0 comments on commit 0a8f8c0

Please sign in to comment.