forked from fivetran/dbt_hubspot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hubspot__companies.sql
56 lines (37 loc) · 1.13 KB
/
hubspot__companies.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
{{ config(enabled=fivetran_utils.enabled_vars(['hubspot_sales_enabled','hubspot_company_enabled'])) }}
with companies as (
select *
from {{ var('company') }}
{% if fivetran_utils.enabled_vars(['hubspot_engagement_enabled','hubspot_engagement_company_enabled']) %}
), engagements as (
select *
from {{ ref('hubspot__engagements') }}
), engagement_companies as (
select *
from {{ var('engagement_company') }}
), engagement_companies_joined as (
select
engagements.engagement_type,
engagement_companies.company_id
from engagements
inner join engagement_companies
using (engagement_id)
), engagement_companies_agg as (
{{ engagements_aggregated('engagement_companies_joined', 'company_id') }}
), joined as (
select
companies.*,
{% for metric in engagement_metrics() %}
coalesce(engagement_companies_agg.{{ metric }},0) as {{ metric }} {% if not loop.last %},{% endif %}
{% endfor %}
from companies
left join engagement_companies_agg
using (company_id)
)
select *
from joined
{% else %}
)
select *
from companies
{% endif %}