In dbt, i want to append table if exists. Basically, i am building an incremental model where i find out lagged values from previous day, for the first run, i want to union today’s data along with {{this}}, i know that for the first day {{this}} has no values, so i am trying to skip it in the union.
I am using the below query but it is returning an error:
{{
config(
materialized='incremental',
unique_key='date_run',
full_refresh=true
)
}}
with subs_count as (
select
current_date() as date_run,
count(distinct email_address) as distinct_email,
subscriber
from {{ source('fan_table_sandbox', 'international_fan_data') }}
group by date_run,subscriber
),
aggregated_data as (
select
*
from subs_count
union all
if(exists( (select
*
from {{this}} if exists {{this}}
order by date_run desc limit 1))
),
lag_values as (
select
*,
lag(distinct_email) over (partition by nba_subscriber order by date_run asc) as previous_day_count
from aggregated_data
)
select * from lag_values
how can this be resolved?