# Incremental from multiple refs / sources

**URL:** <https://discourse.getdbt.com/t/incremental-from-multiple-refs-sources/8488>\
**Category:** Help\
**Tags:** best-practice, incremental, dbt-core\
**Created:** [May 30, 2023, 9:01pm UTC](https://discourse.getdbt.com/t/incremental-from-multiple-refs-sources/8488 "2023-05-30T21:01:08Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![ktl](https://avatars.discourse-cdn.com/v4/letter/k/f9ae1b/32.png) [@ktl](https://discourse.getdbt.com/u/ktl)\
**Post date:** [May 31, 2023, 3:12pm UTC](https://discourse.getdbt.com/t/incremental-from-multiple-refs-sources/8488/2 "2023-05-31T15:12:10Z")

</div>

**edit** : I think I figured out how to do it using `UNION`. Not sure how fast this will be but seems logically correct to me, using actual tables from my org:

```auto
with users_count as (
    SELECT
        cu.owned_by_organization_id,
        count(*) as user_count,
        max(cu.date_updated) as user_date_updated
    FROM
        user_user cu
    group by cu.owned_by_organization_id
),

sites_count as (
    SELECT
        si.owned_by_organization_id,
        count(*) as site_count,
        max(si.date_updated) as site_date_updated
    FROM
        site_site si
    group by si.owned_by_organization_id
),

user_org_ids as (
    SELECT owned_by_organization_id as org_id
    from users_count
    where users_count.user_date_updated > (select max(date_updated) from {{ this }})
),

sites_org_ids as (
    SELECT
        owned_by_organization_id as org_id
    FROM
        sites_count
    WHERE
        sites_count.site_date_updated > (select max(date_updated) from {{ this }})
),

org_final as (
    select
        org.*,
        users_count.*,
        sites_count.*
    from user_organization org
    left join users_count on users_count.owned_by_organization_id = org.id
    left join sites_count on sites_count.owned_by_organization_id = org.id
    {% if is_incremental() %}
    where org.id in (
        select org_id from user_org_ids
        union
        select org_id from sites_org_ids
    )
    {% endif %}
)

select count(*) from org_final

```

---

_[View the full topic](https://discourse.getdbt.com/t/incremental-from-multiple-refs-sources/8488)._
