The problem I’m having
I want to make a model containing multiple fact and dimension tables incremental, so that only new or updated data is processed instead of running the entire query every time.
The context of why I’m trying to do this
I’m working on a Medallion architecture in Snowflake and want to optimize data processing and warehouse usage. The model combines multiple fact and dimension tables, and I want to understand how to handle incremental processing while maintaining data consistency.
What I’ve already tried
I tries dynamic table along with stream based task (have some limitations) but to make platform independence, wanted to use dbt.
Some example code or error messages
I can share the SQL query for the model, which includes multiple fact and dimension tables, to explore how it can be made incremental.
Model structure
The model combines data from three sources using UNION ALL:
-
Online transactions: Joins a trip/waybill fact table, a ticket fact table, a passenger/ride fact table, and a transaction/payment fact table.
-
Mobile transactions: Reads booking data from a separate source, including semi-structured JSON fields.
-
Manual transactions: Joins a trip/waybill table with manual ticket reports and a denomination/reference table.
The combined data is aggregated by transaction_date, location_name, fare_category, and tax_rate.
The final output includes passenger counts, ticket counts, revenue, luggage revenue, toll revenue, and calculated surcharges.
### Simplified SQL structure
```sql
--CREATE OR REPLACE DYNAMIC TABLE target_schema.daily_sales_summary
-- TARGET_LAG = '1 day'
-- REFRESH_MODE = FULL
-- AS
WITH online_transactions AS (
SELECT
CAST(t.trip_timestamp AS DATE) AS transaction_date,
t.location_id,
t.trip_id,
tk.ticket_id,
r.passenger_category,
r.passenger_count,
r.paid_amount,
p.tax_amount,
d.fare_category,
d.fare_value
FROM source_schema.trip_fact t
JOIN source_schema.ticket_fact tk
ON t.trip_id = tk.trip_id
JOIN source_schema.ride_fact r
ON r.ticket_id = tk.ticket_id
JOIN source_schema.payment_fact p
ON p.ticket_id = tk.ticket_id
LEFT JOIN source_schema.fare_dimension d
ON tk.fare_id = d.fare_id
WHERE t.status = 'COMPLETED'
),
online_aggregated AS (
SELECT
transaction_date,
location_id,
fare_category,
SUM(passenger_count) AS passenger_count,
SUM(paid_amount) AS revenue
FROM online_transactions
GROUP BY 1, 2, 3
),
mobile_transactions AS (
SELECT
CAST(booking_timestamp AS DATE) AS transaction_date,
'MOBILE' AS location_name,
booking_details:fare::NUMBER AS fare_category,
booking_details:passenger_count::NUMBER AS passenger_count,
booking_details:fare::NUMBER
* booking_details:passenger_count::NUMBER AS revenue
FROM mobile_source.booking_fact
WHERE booking_status = 'SUCCESS'
),
manual_transactions AS (
SELECT
CAST(t.trip_timestamp AS DATE) AS transaction_date,
t.location_id,
d.fare_category,
SUM(m.ticket_count) AS passenger_count,
SUM(m.ticket_count * d.fare_value) AS revenue
FROM source_schema.trip_fact t
JOIN manual_source.ticket_report m
ON t.trip_id = m.trip_id
JOIN source_schema.fare_dimension d
ON m.fare_name = d.fare_name
WHERE t.status = 'COMPLETED'
AND m.is_deleted = FALSE
GROUP BY 1, 2, 3
),
combined_data AS (
SELECT * FROM online_aggregated
UNION ALL
SELECT * FROM mobile_transactions
UNION ALL
SELECT * FROM manual_transactions
)
SELECT
transaction_date,
location_id,
fare_category,
SUM(passenger_count) AS total_passengers,
SUM(revenue) AS total_revenue
FROM combined_data
GROUP BY 1, 2, 3;
```