Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Added statistics model, updated revenue model #5

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 37 additions & 29 deletions models/core/dm_monthly_zone_revenue.sql
Original file line number Diff line number Diff line change
@@ -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
30 changes: 30 additions & 0 deletions models/core/dm_monthly_zone_statistics.sql
Original file line number Diff line number Diff line change
@@ -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
33 changes: 30 additions & 3 deletions models/core/schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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'"