|
1 | | --- Exercise 6: Build views, then query them |
| 1 | +-- Exercise 5: Validate the raw data |
2 | 2 | -- |
3 | | --- Wrap the cleaned-up logic in two views, then query them. This is exactly the |
4 | | --- star-schema deliverable the assignment asks for, scaled down to practice once. |
5 | | --- 6a. Create vw_dim_zones from nyc_taxi.raw_zones and vw_fact_trips from nyc_taxi.raw_trips, |
6 | | --- excluding rows where fare_amount is negative. |
7 | | --- 6b. Using your views, find which borough had the highest total fare revenue. |
8 | | --- 6c. Using your views, find the top 5 pickup zones by trip count. |
| 3 | +-- Raw data is rarely clean. Write three checks that the assignment's audit task |
| 4 | +-- expects you to run: |
| 5 | +-- 5a. Count trips with a NULL pickup_location_id. |
| 6 | +-- 5b. Find duplicate trips: rows that share the same vendor_id, pickup_datetime, |
| 7 | +-- and dropoff_datetime. |
| 8 | +-- 5c. Find orphaned pickup IDs: pickup_location_id values in nyc_taxi.raw_trips that do |
| 9 | +-- not exist in nyc_taxi.raw_zones. |
9 | 10 | -- |
10 | 11 | -- Dataset: nyc_taxi.raw_trips (~57K green-taxi rows, Jan 2024) and nyc_taxi.raw_zones (265 rows). |
11 | 12 | -- Run these against your OWN schema on the shared Azure PostgreSQL, not public. |
12 | | --- NOTE: 6a creates views in YOUR OWN schema. CREATE OR REPLACE VIEW is safe to re-run. |
13 | | --- NOTE: this practice view is scaled down. The Week 9 assignment's vw_fact_trips |
14 | | --- also casts pickup_datetime to a TIMESTAMP (pickup_datetime::TIMESTAMP). |
15 | | --- Add that cast when you build the assignment version. |
16 | 13 | -- |
17 | | --- Hint: A view is a saved query: CREATE VIEW name AS SELECT ... The borough and |
18 | | --- zone names live in vw_dim_zones, so join |
19 | | --- vw_fact_trips.pickup_location_id = vw_dim_zones.location_id for any |
20 | | --- name-level breakdown. |
| 14 | +-- Hint: For duplicates, GROUP BY the three columns and keep groups with |
| 15 | +-- HAVING COUNT(*) > 1. For orphans, a LEFT JOIN to nyc_taxi.raw_zones with |
| 16 | +-- WHERE z.location_id IS NULL surfaces the unmatched IDs. |
21 | 17 |
|
22 | | --- 6a. The two views |
23 | | --- TODO: CREATE OR REPLACE VIEW vw_dim_zones from nyc_taxi.raw_zones. |
24 | | --- TODO: CREATE OR REPLACE VIEW vw_fact_trips from nyc_taxi.raw_trips, excluding negative fares. |
| 18 | +-- 5a. Trips with a missing pickup location |
| 19 | +-- TODO: count rows where pickup_location_id IS NULL. |
| 20 | +-- Expect 0 here: the pickup IDs are complete. A 0 is the check passing, |
| 21 | +-- not a mistake. The real dirt is duplicates (5b) and negative fares. |
25 | 22 |
|
26 | 23 |
|
27 | | --- 6b. Highest total fare revenue by borough |
28 | | --- TODO: join the two views, sum fare per borough, and return the top borough. |
| 24 | +-- 5b. Duplicate trips (same vendor + pickup + dropoff time) |
| 25 | +-- TODO: group by the three columns and keep groups with more than one row. |
29 | 26 |
|
30 | 27 |
|
31 | | --- 6c. Top 5 pickup zones by trip count |
32 | | --- TODO: join the two views, count trips per zone, and return the top 5 zones. |
| 28 | +-- 5c. Orphaned pickup IDs not present in nyc_taxi.raw_zones |
| 29 | +-- TODO: LEFT JOIN nyc_taxi.raw_trips to nyc_taxi.raw_zones and keep rows with no matching zone. |
0 commit comments