1- {{ config(
1+ {{
2+ config(
23 materialized= ' incremental' ,
34 unique_key= [' territory_id' , ' direction' , ' start_date' , ' operator_id' ],
4- post_hook= [
5- ' DO $$ BEGIN IF NOT EXISTS (SELECT 1 FROM pg_constraint WHERE conname = ' " 'operators_by_day_pkey'" ' ) THEN ALTER TABLE {{ this }} ADD CONSTRAINT operators_by_day_pkey PRIMARY KEY (territory_id, direction, start_date, operator_id); END IF; END $$;'
6- ' CREATE INDEX IF NOT EXISTS operators_by_day_idx ON {{ this }} using btree(territory_id, direction, start_date, operator_id)' ,
5+ indexes = [
6+ {
7+ ' columns' :[
8+ ' territory_id' ,
9+ ' direction' ,
10+ ' start_date' ,
11+ ' operator_id' ,
12+ ],
13+ ' unique' :true
14+ }
715 ]
816 )
917}}
1018with directions as (
11- select
12- " from" as territory_id,
13- ' from' as direction,
14- start_date,
15- operator_id,
16- operator_name,
17- sum (journeys) as journeys,
18- coalesce(
19- sum (journeys) filter (where " from" = " to" ), 0
20- ) as intra_journeys,
21- sum (incented_journeys) as incented_journeys,
22- coalesce(
23- sum (incented_journeys) filter (where " from" = " to" ), 0
24- ) as intra_incented_journeys,
25- sum (incentive_amount) as incentive_amount,
26- coalesce(
27- sum (incentive_amount) filter (where " from" = " to" ), 0
28- ) as intra_incentive_amount
29- from {{ ref(' carpools_by_day' ) }}
30- {% if is_incremental() %}
31- where start_date >= (select max (start_date) from {{ this }})
32- {% endif %}
33- group by 1 , 3 , 4 , 5
34- union
35- select
36- " to" as territory_id,
37- ' to' as direction,
38- start_date,
39- operator_id,
40- operator_name,
41- sum (journeys) as journeys,
42- 0 as intra_journeys,
43- sum (incented_journeys) as incented_journeys,
44- 0 as intra_incented_journeys,
45- sum (incentive_amount) as incentive_amount,
46- 0 as intra_incentive_amount
47- from {{ ref(' carpools_by_day' ) }}
48- {% if is_incremental() %}
49- where start_date >= (select max (start_date) from {{ this }})
50- {% endif %}
51- group by 1 , 3 , 4 , 5
19+ SELECT
20+ c .territory_id AS territory_id,
21+ ' from' AS direction,
22+ a .start_datetime ::date AS start_date,
23+ a .operator_id ,
24+ a .operator_name ,
25+ count (distinct a .carpool_id ) filter (where b .territory_id = c .territory_id ) AS journeys,
26+ count (distinct a .carpool_id ) filter (where a .incentive_amount > 0 AND b .territory_id = c .territory_id ) AS incented_journeys,
27+ sum (a .incentive_amount ) filter (where b .territory_id = c .territory_id ) as incentive_amount
28+ FROM {{ ref(' view_dashboard_carpools' ) }} a
29+ LEFT JOIN {{ ref(' view_perimeters_territories' ) }} b ON a .start_geo_code = b .arr
30+ LEFT JOIN {{ source(' policy' , ' policies' ) }} c ON a .policy_id = c ._id
31+ WHERE
32+ c .territory_id IS NOT NULL
33+ {% if is_incremental() %}
34+ AND a .start_datetime ::date >= (SELECT MAX (start_date) FROM {{ this }})::date
35+ {% endif %}
36+ GROUP BY
37+ 1 , 2 , 3 , 4 , 5
38+ UNION
39+ SELECT
40+ c .territory_id AS territory_id,
41+ ' to' AS direction,
42+ a .start_datetime ::date AS start_date,
43+ a .operator_id ,
44+ a .operator_name ,
45+ count (distinct a .carpool_id ) filter (where b .territory_id = c .territory_id ) AS journeys,
46+ count (distinct a .carpool_id ) filter (where a .incentive_amount > 0 AND b .territory_id = c .territory_id ) AS incented_journeys,
47+ sum (a .incentive_amount ) filter (where b .territory_id = c .territory_id ) as incentive_amount
48+ FROM {{ ref(' view_dashboard_carpools' ) }} a
49+ LEFT JOIN {{ ref(' view_perimeters_territories' ) }} b ON a .end_geo_code = b .arr
50+ LEFT JOIN {{ source(' policy' , ' policies' ) }} c ON a .policy_id = c ._id
51+ WHERE
52+ c .territory_id IS NOT NULL
53+ {% if is_incremental() %}
54+ AND a .start_datetime ::date >= (SELECT MAX (start_date) FROM {{ this }})::date
55+ {% endif %}
56+ GROUP BY
57+ 1 , 2 , 3 , 4 , 5
58+ UNION
59+ SELECT
60+ c .territory_id AS territory_id,
61+ ' intra' AS direction,
62+ a .start_datetime ::date AS start_date,
63+ a .operator_id ,
64+ a .operator_name ,
65+ count (distinct a .carpool_id ) filter (where b .territory_id = c .territory_id ) AS journeys,
66+ count (distinct a .carpool_id ) filter (where a .incentive_amount > 0 AND b .territory_id = c .territory_id ) AS incented_journeys,
67+ sum (a .incentive_amount ) filter (where b .territory_id = c .territory_id ) as incentive_amount
68+ FROM {{ ref(' view_dashboard_carpools' ) }} a
69+ LEFT JOIN {{ ref(' view_perimeters_territories' ) }} b ON a .start_geo_code = b .arr and a .end_geo_code = b .arr
70+ LEFT JOIN {{ source(' policy' , ' policies' ) }} c ON a .policy_id = c ._id
71+ WHERE
72+ c .territory_id IS NOT NULL
73+ {% if is_incremental() %}
74+ AND a .start_datetime ::date >= (SELECT MAX (start_date) FROM {{ this }})::date
75+ {% endif %}
76+ GROUP BY
77+ 1 , 2 , 3 , 4 , 5
5278)
53-
5479select
5580 territory_id,
5681 start_date,
@@ -63,16 +88,15 @@ select
6388from directions
6489where territory_id is not null
6590group by 1 , 2 , 3 , 4 , 5
66- union
67- select
68- territory_id,
91+ UNION
92+ select
93+ territory_id,
6994 start_date,
70- ' both' as direction,
95+ ' both' AS direction,
7196 operator_id,
7297 operator_name,
73- sum (journeys) - sum (intra_journeys) as journeys,
74- sum (incented_journeys) - sum (intra_incented_journeys ) as incented_journeys,
75- sum (incentive_amount) - sum (intra_incentive_amount) as incentive_amount
98+ sum (journeys) filter ( where direction <> ' intra ' ) - sum (journeys) filter ( where direction = ' intra ' ) as journeys,
99+ sum (incented_journeys) filter ( where direction <> ' intra ' ) - sum (incented_journeys) filter ( where direction = ' intra ' ) as incented_journeys,
100+ sum (incentive_amount) filter ( where direction <> ' intra ' ) - sum (incentive_amount) filter ( where direction = ' intra ' ) as incentive_amount
76101from directions
77- where territory_id is not null
78102group by 1 , 2 , 3 , 4 , 5
0 commit comments