-
Notifications
You must be signed in to change notification settings - Fork 7
completed SQL queries #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,7 +5,9 @@ | |
| -- TODO: complete the SELECT (location_id, zone, borough). | ||
| CREATE OR REPLACE VIEW vw_dim_zones AS | ||
| SELECT | ||
| -- TODO | ||
| location_id, | ||
| zone, | ||
| borough | ||
| FROM nyc_taxi.raw_zones; | ||
|
|
||
| -- Fact: one row per taxi trip. | ||
|
|
@@ -15,12 +17,25 @@ FROM nyc_taxi.raw_zones; | |
| -- TODO: complete the SELECT and the WHERE. | ||
| CREATE OR REPLACE VIEW vw_fact_trips AS | ||
| SELECT | ||
| -- TODO | ||
| vendor_id, | ||
| pickup_datetime::TIMESTAMP AS pickup_datetime, | ||
| dropoff_datetime, | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It was not in the assignment but it's best to cast all similar columns to similar type, i.e. cast this one to datetime as well. |
||
| pickup_location_id, | ||
| dropoff_location_id, | ||
| passenger_count, | ||
| trip_distance, | ||
| fare_amount, | ||
| tip_amount, | ||
| total_amount, | ||
| payment_type | ||
| FROM nyc_taxi.raw_trips | ||
| -- TODO: WHERE fare_amount >= 0 | ||
| ; | ||
| WHERE fare_amount >= 0; | ||
|
|
||
| -- Join-readiness test (run after creating the views; it must run without error | ||
| -- and return a count close to the vw_fact_trips row count): | ||
| -- SELECT COUNT(*) FROM vw_fact_trips f | ||
| -- JOIN vw_dim_zones d ON f.pickup_location_id = d.location_id; | ||
| SELECT COUNT(*) | ||
| FROM vw_fact_trips f | ||
| JOIN vw_dim_zones d | ||
| ON f.pickup_location_id = d.location_id; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,18 +5,89 @@ | |
| -- 1. Volume: how many total rows in vw_fact_trips? How many rows per borough? | ||
| -- What is the most common pickup/dropoff location combination? | ||
| -- TODO | ||
| SELECT COUNT(*) AS total_trips | ||
| FROM vw_fact_trips; | ||
| SELECT | ||
| d.borough, | ||
| COUNT(*) AS trip_count | ||
| FROM vw_fact_trips f | ||
| JOIN vw_dim_zones d | ||
| ON f.pickup_location_id = d.location_id | ||
| GROUP BY d.borough | ||
| ORDER BY trip_count DESC; | ||
| SELECT | ||
| pickup_location_id, | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not wrong, but this just gives you the IDs, how would you create pairs of zone names instead? |
||
| dropoff_location_id, | ||
| COUNT(*) AS trip_count | ||
| FROM vw_fact_trips | ||
| GROUP BY | ||
| pickup_location_id, | ||
| dropoff_location_id | ||
| ORDER BY trip_count DESC | ||
| LIMIT 1; | ||
|
|
||
| -- (Take a screenshot of the per-borough counts and save it as assets/borough_count.png.) | ||
|
|
||
|
|
||
| -- 2. Revenue: which pickup zone (name, not ID) generated the highest total fare_amount? | ||
| -- Which pickup zone collected the highest total fare_amount on any single day? | ||
| -- TODO | ||
| SELECT | ||
| d.zone, | ||
| SUM(f.fare_amount) AS total_revenue | ||
| FROM vw_fact_trips f | ||
| JOIN vw_dim_zones d | ||
| ON f.pickup_location_id = d.location_id | ||
| GROUP BY d.zone | ||
| ORDER BY total_revenue DESC | ||
| LIMIT 1; | ||
| SELECT | ||
| d.zone, | ||
| DATE(f.pickup_datetime) AS trip_date, | ||
| SUM(f.fare_amount) AS total_revenue | ||
| FROM vw_fact_trips f | ||
| JOIN vw_dim_zones d | ||
| ON f.pickup_location_id = d.location_id | ||
| GROUP BY | ||
| d.zone, | ||
| DATE(f.pickup_datetime) | ||
| ORDER BY total_revenue DESC | ||
| LIMIT 1; | ||
|
|
||
|
|
||
| -- 3. Geospatial: total number of trips and average trip_distance for each borough. | ||
| -- TODO | ||
| SELECT | ||
| d.borough, | ||
| COUNT(*) AS total_trips, | ||
| AVG(f.trip_distance) AS average_trip_distance | ||
| FROM vw_fact_trips f | ||
| JOIN vw_dim_zones d | ||
| ON f.pickup_location_id = d.location_id | ||
| GROUP BY d.borough | ||
| ORDER BY total_trips DESC; | ||
|
|
||
|
|
||
| -- 4. Time patterns: which day of the week had the highest total tip_amount? | ||
| -- What hour of the day has the highest average tip? | ||
| -- TODO | ||
| SELECT | ||
| TO_CHAR(pickup_datetime, 'Day') AS day_of_week, | ||
| SUM(tip_amount) AS total_tips | ||
| FROM vw_fact_trips | ||
| GROUP BY day_of_week | ||
| ORDER BY total_tips DESC | ||
| LIMIT 1; | ||
|
|
||
| SELECT | ||
| EXTRACT(HOUR FROM pickup_datetime) AS hour_of_day, | ||
| AVG(tip_amount) AS average_tip | ||
| FROM vw_fact_trips | ||
| GROUP BY hour_of_day | ||
| ORDER BY average_tip DESC | ||
| LIMIT 1; | ||
|
|
||
| SELECT * | ||
| FROM vw_fact_trips | ||
| LIMIT 1; | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
make sure to specify a schema as well so you don't write all your tables in the public schema that is potentially shared with others!