|
1 | 1 | {{
|
2 | 2 | config(
|
3 |
| - materialized='table', |
| 3 | + materialized='incremental', |
| 4 | + incremental_strategy='insert_overwrite', |
| 5 | + partition_by={ |
| 6 | + 'field': 'dt', |
| 7 | + 'data_type': 'date', |
| 8 | + 'granularity': 'day', |
| 9 | + }, |
4 | 10 | cluster_by='base64_url',
|
5 | 11 | )
|
6 | 12 | }}
|
7 | 13 |
|
8 |
| - |
9 | 14 | WITH service_alerts AS (
|
10 |
| - SELECT *, |
11 |
| - -- https://gtfs.org/realtime/reference/#message-tripdescriptor |
| 15 | + SELECT * FROM {{ ref('fct_service_alerts_messages_unnested') }} |
| 16 | + WHERE {{ gtfs_rt_dt_where() }} |
| 17 | +), |
| 18 | + |
| 19 | +fct_service_alerts_trip_summaries AS ( |
| 20 | + SELECT |
| 21 | + -- https://gtfs.org/realtime/reference/#message-tripdescriptor |
12 | 22 | {{ dbt_utils.generate_surrogate_key([
|
13 |
| - 'calculated_service_date', |
| 23 | + 'dt', |
14 | 24 | 'base64_url',
|
15 | 25 | 'trip_id',
|
| 26 | + 'trip_route_id', |
| 27 | + 'trip_direction_id', |
16 | 28 | 'trip_start_time',
|
| 29 | + 'trip_start_date', |
17 | 30 | ]) }} as key,
|
18 |
| - FROM {{ ref('int_gtfs_rt__service_alerts_trip_day_map_grouping') }} |
19 |
| -), |
20 |
| - |
21 |
| -re_aggregate AS ( |
22 |
| - SELECT |
23 |
| - key, |
24 |
| - calculated_service_date, |
| 31 | + dt, |
25 | 32 | base64_url,
|
26 | 33 | trip_id,
|
27 | 34 | trip_route_id,
|
28 | 35 | trip_direction_id,
|
29 | 36 | trip_start_time,
|
30 | 37 | trip_start_date,
|
31 |
| - trip_schedule_relationship, |
32 |
| - feed_timezone, |
33 |
| - COUNT(DISTINCT unnested_message_ids) AS num_distinct_message_ids, |
34 |
| - COUNT(DISTINCT unested_header_timestamps) AS num_distinct_header_timestamps, |
35 |
| - COUNT(DISTINCT unnested_message_keys) AS num_distinct_message_keys, |
36 |
| - MIN(min_extract_ts) AS min_extract_ts, |
37 |
| - MAX(max_extract_ts) AS max_extract_ts, |
38 |
| - MIN(min_header_timestamp) AS min_header_timestamp, |
39 |
| - MAX(max_header_timestamp) AS max_header_timestamp, |
40 |
| - ARRAY_AGG(DISTINCT unnested_alert_content) |
41 |
| - AS alert_content_array |
| 38 | + COUNT(DISTINCT id) AS num_distinct_message_ids, |
| 39 | + COUNT(DISTINCT header_timestamp) AS num_distinct_header_timestamps, |
| 40 | + ARRAY_AGG(DISTINCT service_alert_message_key) AS service_alert_message_keys, |
| 41 | + MIN(_extract_ts) AS min_extract_ts, |
| 42 | + MAX(_extract_ts) AS max_extract_ts, |
| 43 | + MIN(header_timestamp) AS min_header_timestamp, |
| 44 | + MAX(header_timestamp) AS max_header_timestamp, |
42 | 45 | FROM service_alerts
|
43 |
| - LEFT JOIN UNNEST(message_ids_array) AS unnested_message_ids |
44 |
| - LEFT JOIN UNNEST(header_timestamps_array) AS unested_header_timestamps |
45 |
| - LEFT JOIN UNNEST(message_keys_array) AS unnested_message_keys |
46 |
| - LEFT JOIN UNNEST(alert_content_array) AS unnested_alert_content |
47 |
| - GROUP BY 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 |
48 |
| -), |
49 |
| - |
50 |
| -fct_service_alerts_trip_summaries AS ( |
51 |
| - SELECT |
52 |
| - key, |
53 |
| - calculated_service_date, |
54 |
| - base64_url, |
55 |
| - trip_id, |
56 |
| - trip_route_id, |
57 |
| - trip_direction_id, |
58 |
| - trip_start_time, |
59 |
| - trip_start_date, |
60 |
| - trip_schedule_relationship, |
61 |
| - feed_timezone, |
62 |
| - num_distinct_message_ids, |
63 |
| - num_distinct_header_timestamps, |
64 |
| - num_distinct_message_keys, |
65 |
| - min_extract_ts, |
66 |
| - max_extract_ts, |
67 |
| - min_header_timestamp, |
68 |
| - max_header_timestamp, |
69 |
| - ARRAY_AGG( |
70 |
| - STRUCT<message_id string, cause string, effect string, header string, description string > |
71 |
| - (JSON_VALUE(unnested_alert, '$.message_id'), |
72 |
| - JSON_VALUE(unnested_alert, '$.cause'), |
73 |
| - JSON_VALUE(unnested_alert, '$.effect'), |
74 |
| - JSON_VALUE(unnested_alert, '$.header'), |
75 |
| - JSON_VALUE(unnested_alert, '$.description'))) |
76 |
| - AS alert_content_array |
77 |
| - FROM re_aggregate |
78 |
| - LEFT JOIN UNNEST(alert_content_array) AS unnested_alert |
79 |
| - GROUP BY 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17 |
80 |
| - ) |
| 46 | + GROUP BY 1, 2, 3, 4, 5, 6, 7, 8 |
| 47 | +) |
81 | 48 |
|
82 | 49 | SELECT * FROM fct_service_alerts_trip_summaries
|
0 commit comments