diff --git a/models/core/dm_monthly_zone_revenue.sql b/models/core/dm_monthly_zone_revenue.sql index 6d92df4f..fb6b94fc 100644 --- a/models/core/dm_monthly_zone_revenue.sql +++ b/models/core/dm_monthly_zone_revenue.sql @@ -1,31 +1,39 @@ -{{ config(materialized='table') }} +{{ config( + materialized = 'table' +) }} -with trips_data as ( - select * from {{ ref('fact_trips') }} -) - select - -- Reveneue grouping - pickup_zone as revenue_zone, - date_trunc('month', pickup_datetime) as revenue_month, - --Note: For BQ use instead: date_trunc(pickup_datetime, month) as revenue_month, - - service_type, - - -- Revenue calculation - sum(fare_amount) as revenue_monthly_fare, - sum(extra) as revenue_monthly_extra, - sum(mta_tax) as revenue_monthly_mta_tax, - sum(tip_amount) as revenue_monthly_tip_amount, - sum(tolls_amount) as revenue_monthly_tolls_amount, - sum(ehail_fee) as revenue_monthly_ehail_fee, - sum(improvement_surcharge) as revenue_monthly_improvement_surcharge, - sum(total_amount) as revenue_monthly_total_amount, - sum(congestion_surcharge) as revenue_monthly_congestion_surcharge, +WITH trips_data AS ( - -- Additional calculations - count(tripid) as total_monthly_trips, - avg(passenger_count) as avg_montly_passenger_count, - avg(trip_distance) as avg_montly_trip_distance - - from trips_data - group by 1,2,3 + SELECT + * + FROM + {{ ref('fact_trips') }} +) +SELECT + -- Reveneue grouping + pickup_zone AS revenue_zone, + DATE_TRUNC( + 'month', + pickup_datetime + ) AS revenue_month, + --Note: For BQ use instead: date_trunc(pickup_datetime, month) as revenue_month, + service_type, + -- Revenue calculation + SUM(fare_amount) AS revenue_monthly_fare, + SUM(extra) AS revenue_monthly_extra, + SUM(mta_tax) AS revenue_monthly_mta_tax, + SUM(tip_amount) AS revenue_monthly_tip_amount, + SUM(tolls_amount) AS revenue_monthly_tolls_amount, + SUM(ehail_fee) AS revenue_monthly_ehail_fee, + SUM(improvement_surcharge) AS revenue_monthly_improvement_surcharge, + SUM(total_amount) AS revenue_monthly_total_amount, + SUM(congestion_surcharge) AS revenue_monthly_congestion_surcharge -- Additional calculations + --count(tripid) as total_monthly_trips, + --avg(passenger_count) as avg_montly_passenger_count, + --avg(trip_distance) as avg_montly_trip_distance +FROM + trips_data +GROUP BY + 1, + 2, + 3 diff --git a/models/core/dm_monthly_zone_statistics.sql b/models/core/dm_monthly_zone_statistics.sql new file mode 100644 index 00000000..401bcf47 --- /dev/null +++ b/models/core/dm_monthly_zone_statistics.sql @@ -0,0 +1,30 @@ +{{ config( + materialized = 'table' +) }} + +WITH trips_data AS ( + + SELECT + * + FROM + {{ ref('fact_trips') }} +) +SELECT + -- Reveneue grouping + pickup_zone AS revenue_zone, + DATE_TRUNC( + 'month', + pickup_datetime + ) AS revenue_month, + --Note: For BQ use instead: date_trunc(pickup_datetime, month) as revenue_month, + service_type, + -- Additional calculations + COUNT(tripid) AS total_monthly_trips, + AVG(passenger_count) AS avg_montly_passenger_count, + AVG(trip_distance) AS avg_montly_trip_distance +FROM + trips_data +GROUP BY + 1, + 2, + 3 diff --git a/models/core/schema.yml b/models/core/schema.yml index f90fe2c1..2c634ba1 100644 --- a/models/core/schema.yml +++ b/models/core/schema.yml @@ -21,6 +21,33 @@ models: description: Monthly sum of the the total_amount of the fare charged for the trip per pickup zone, month and service. tests: - not_null: - severity: error - - + severity: error +metrics: + - name: average_distance + label: Average Distance + model: ref('fact_trips') + description: "The average trip distance" + + calculation_method: average + expression: trip_distance + + timestamp: pickup_datetime + time_grains: [month, quarter, year] + + filters: + - field: pickup_borough + operator: '=' + value: "'Manhattan'" + - field: dropoff_borough + operator: '=' + value: "'Manhattan'" + + tags: + - piperider +filters: + - field: pickup_borough + operator: '=' + value: "'Manhattan'" + - field: dropoff_borough + operator: '=' + value: "'Manhattan'" \ No newline at end of file