diff --git a/models/br_ms_sia/br_ms_sia__producao_ambulatorial.sql b/models/br_ms_sia/br_ms_sia__producao_ambulatorial.sql index 62433139..26c16afc 100644 --- a/models/br_ms_sia/br_ms_sia__producao_ambulatorial.sql +++ b/models/br_ms_sia/br_ms_sia__producao_ambulatorial.sql @@ -197,7 +197,9 @@ select safe_cast(pa_fler as string) flag_erro_corpo_apac, from sia_add_municipios {% if is_incremental() %} - where - date(cast(ano as int64), cast(mes as int64), 1) - > (select max(date(cast(ano as int64), cast(mes as int64), 1)) from {{ this }}) + left join + {{ this }} as materialized + on sia_add_municipios.ano = materialized.ano + and sia_add_municipios.mes = materialized.mes + where materialized.ano is null and materialized.mes is null {% endif %}