From 202e7ec496c64a7afd0fff6c19e70b28c615e6f5 Mon Sep 17 00:00:00 2001 From: Gabrielle Carvalho <77730866+gabrielle-carv@users.noreply.github.com> Date: Fri, 6 Oct 2023 10:06:14 -0300 Subject: [PATCH] incremental --- models/br_me_cnpj/estabelecimentos.sql | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/models/br_me_cnpj/estabelecimentos.sql b/models/br_me_cnpj/estabelecimentos.sql index 6c184ab9..b1114eba 100644 --- a/models/br_me_cnpj/estabelecimentos.sql +++ b/models/br_me_cnpj/estabelecimentos.sql @@ -1,7 +1,7 @@ {{ config( schema='br_me_cnpj', - materialized='table', + materialized='incremental', partition_by={ "field": "data", "data_type": "date", @@ -17,8 +17,8 @@ GRANT TO ("group:bd-pro@basedosdados.org", "group:sudo@basedosdados.org") FILTER USING (EXTRACT(YEAR from data) = EXTRACT(YEAR from CURRENT_DATE()))' ]) }} - -SELECT +WITH cnpj_estabelecimentos AS +(SELECT SAFE_CAST(data AS DATE) data, SAFE_CAST(lpad(cnpj,14,"0") AS STRING) cnpj, SAFE_CAST(lpad(cnpj_basico, 8, '0') AS STRING) cnpj_basico, @@ -54,5 +54,8 @@ SELECT SAFE_CAST(data_situacao_especial AS DATE) data_situacao_especial FROM basedosdados-staging.br_me_cnpj_staging.estabelecimentos a LEFT JOIN basedosdados.br_bd_diretorios_brasil.municipio b - ON a.id_municipio_rf = b.id_municipio_rf - + ON a.id_municipio_rf = b.id_municipio_rf) +SELECT * FROM cnpj_estabelecimentos +{% if is_incremental() %} +WHERE data > (SELECT MAX(data) FROM {{ this }} ) +{% endif %} \ No newline at end of file