|
1 | | --- Exercise 6: Build views, then query them |
| 1 | +-- Exercise 7: Build views, then query them |
2 | 2 | -- |
3 | 3 | -- Wrap the cleaned-up logic in two views, then query them. This is exactly the |
4 | 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, |
| 5 | +-- 7a. Create vw_dim_zones from nyc_taxi.raw_zones and vw_fact_trips from nyc_taxi.raw_trips, |
6 | 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. |
| 7 | +-- 7b. Using your views, find which borough had the highest total fare revenue. |
| 8 | +-- 7c. Using your views, find the top 5 pickup zones by trip count. |
9 | 9 | -- |
10 | 10 | -- Dataset: nyc_taxi.raw_trips (~57K green-taxi rows, Jan 2024) and nyc_taxi.raw_zones (265 rows). |
11 | 11 | -- 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. |
| 12 | +-- NOTE: 7a creates views in YOUR OWN schema. CREATE OR REPLACE VIEW is safe to re-run. |
13 | 13 | -- NOTE: this practice view is scaled down. The Week 9 assignment's vw_fact_trips |
14 | 14 | -- also casts pickup_datetime to a TIMESTAMP (pickup_datetime::TIMESTAMP). |
15 | 15 | -- Add that cast when you build the assignment version. |
|
19 | 19 | -- vw_fact_trips.pickup_location_id = vw_dim_zones.location_id for any |
20 | 20 | -- name-level breakdown. |
21 | 21 |
|
22 | | --- 6a. The two views |
| 22 | +-- 7a. The two views |
23 | 23 | -- TODO: CREATE OR REPLACE VIEW vw_dim_zones from nyc_taxi.raw_zones. |
24 | 24 | -- TODO: CREATE OR REPLACE VIEW vw_fact_trips from nyc_taxi.raw_trips, excluding negative fares. |
| 25 | +-- Starter skeletons: |
| 26 | +CREATE OR REPLACE VIEW vw_dim_zones AS |
| 27 | +SELECT 1; -- replace with columns from raw_zones |
25 | 28 |
|
| 29 | +CREATE OR REPLACE VIEW vw_fact_trips AS |
| 30 | +SELECT 1; -- replace with columns from raw_trips and filter negative fares |
26 | 31 |
|
27 | | --- 6b. Highest total fare revenue by borough |
| 32 | + |
| 33 | +-- 7b. Highest total fare revenue by borough |
28 | 34 | -- TODO: join the two views, sum fare per borough, and return the top borough. |
| 35 | +-- Starter skeleton: |
| 36 | +SELECT d.borough, SUM(f.fare_amount) AS total_revenue |
| 37 | +FROM vw_fact_trips f |
| 38 | +INNER JOIN vw_dim_zones d ON f.pickup_location_id = d.location_id |
| 39 | +GROUP BY 1 |
| 40 | +LIMIT 1; -- replace with correct ORDER BY and final touches |
29 | 41 |
|
30 | 42 |
|
31 | | --- 6c. Top 5 pickup zones by trip count |
| 43 | +-- 7c. Top 5 pickup zones by trip count |
32 | 44 | -- TODO: join the two views, count trips per zone, and return the top 5 zones. |
| 45 | +-- Starter skeleton: |
| 46 | +SELECT d.zone, COUNT(*) AS trips |
| 47 | +FROM vw_fact_trips f |
| 48 | +INNER JOIN vw_dim_zones d ON f.pickup_location_id = d.location_id |
| 49 | +GROUP BY 1 |
| 50 | +LIMIT 5; -- replace with correct ORDER BY and final touches |
| 51 | + |
0 commit comments