-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathnyc-taxi-queries.sql
More file actions
88 lines (80 loc) · 2.23 KB
/
Copy pathnyc-taxi-queries.sql
File metadata and controls
88 lines (80 loc) · 2.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
-- Sample analytical queries for NYC Taxi dataset
-- These showcase TimescaleDB's columnstore performance
\echo '=== Sample Queries for NYC Taxi Data ==='
\echo ''
-- Query 1: Total trips and revenue
\echo 'Query 1: Overall statistics'
SELECT
COUNT(*) as total_trips,
SUM(fare_amount) as total_revenue,
AVG(fare_amount) as avg_fare,
AVG(trip_distance) as avg_distance
FROM trips;
-- Query 2: Trips by vendor
\echo ''
\echo 'Query 2: Breakdown by vendor'
SELECT
vendor_id,
COUNT(*) as trips,
AVG(fare_amount) as avg_fare,
AVG(tip_amount) as avg_tip,
AVG(passenger_count) as avg_passengers
FROM trips
GROUP BY vendor_id
ORDER BY trips DESC;
-- Query 3: Hourly patterns using time_bucket
\echo ''
\echo 'Query 3: Hourly trip patterns (using time_bucket)'
SELECT
time_bucket('1 hour', pickup_datetime) AS hour,
COUNT(*) as trips,
AVG(fare_amount) as avg_fare,
SUM(tip_amount) as total_tips
FROM trips
GROUP BY hour
ORDER BY hour DESC
LIMIT 24;
-- Query 4: Payment type analysis
\echo ''
\echo 'Query 4: Payment type analysis'
SELECT
payment_type,
COUNT(*) as trip_count,
SUM(fare_amount) as total_revenue,
AVG(trip_distance) as avg_distance,
AVG(tip_amount) as avg_tip
FROM trips
GROUP BY payment_type
ORDER BY total_revenue DESC;
-- Query 5: Daily aggregation with time_bucket
\echo ''
\echo 'Query 5: Daily statistics by borough'
SELECT
time_bucket('1 day', pickup_datetime) AS day,
pickup_boroname,
COUNT(*) as trips,
AVG(fare_amount) as avg_fare,
MAX(fare_amount) as max_fare
FROM trips
GROUP BY day, pickup_boroname
ORDER BY day DESC, pickup_boroname
LIMIT 20;
-- Query 6: Distance-based analysis
\echo ''
\echo 'Query 6: Trips by distance category'
SELECT
CASE
WHEN trip_distance < 1 THEN 'Short (< 1 mile)'
WHEN trip_distance < 5 THEN 'Medium (1-5 miles)'
WHEN trip_distance < 10 THEN 'Long (5-10 miles)'
ELSE 'Very Long (> 10 miles)'
END as distance_category,
COUNT(*) as trips,
AVG(fare_amount) as avg_fare,
AVG(tip_amount) as avg_tip
FROM trips
GROUP BY distance_category
ORDER BY trips DESC;
\echo ''
\echo '=== Query examples complete! ==='
\echo 'Notice how fast these analytical queries run on compressed columnar data.'