forked from toddwschneider/nyc-taxi-data
-
Notifications
You must be signed in to change notification settings - Fork 0
/
populate_green_trips.sql
70 lines (65 loc) · 2.76 KB
/
populate_green_trips.sql
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
CREATE TABLE tmp_points AS
SELECT
id,
ST_SetSRID(ST_MakePoint(pickup_longitude, pickup_latitude), 4326) as pickup,
ST_SetSRID(ST_MakePoint(dropoff_longitude, dropoff_latitude), 4326) as dropoff
FROM green_tripdata_staging
WHERE pickup_longitude IS NOT NULL OR dropoff_longitude IS NOT NULL;
CREATE INDEX idx_tmp_points_pickup ON tmp_points USING gist (pickup);
CREATE INDEX idx_tmp_points_dropoff ON tmp_points USING gist (dropoff);
CREATE TABLE tmp_pickups AS
SELECT t.id, n.gid
FROM tmp_points t, nyct2010 n
WHERE ST_Within(t.pickup, n.geom);
CREATE TABLE tmp_dropoffs AS
SELECT t.id, n.gid
FROM tmp_points t, nyct2010 n
WHERE ST_Within(t.dropoff, n.geom);
INSERT INTO trips
(cab_type_id, vendor_id, pickup_datetime, dropoff_datetime, store_and_fwd_flag, rate_code_id, pickup_longitude, pickup_latitude, dropoff_longitude, dropoff_latitude, passenger_count, trip_distance, fare_amount, extra, mta_tax, tip_amount, tolls_amount, ehail_fee, improvement_surcharge, total_amount, payment_type, trip_type, pickup, dropoff, pickup_nyct2010_gid, dropoff_nyct2010_gid, pickup_location_id, dropoff_location_id)
SELECT
cab_types.id,
vendor_id,
lpep_pickup_datetime::timestamp,
lpep_dropoff_datetime::timestamp,
store_and_fwd_flag,
rate_code_id::integer,
CASE WHEN pickup_longitude != 0 THEN pickup_longitude END,
CASE WHEN pickup_latitude != 0 THEN pickup_latitude END,
CASE WHEN dropoff_longitude != 0 THEN dropoff_longitude END,
CASE WHEN dropoff_latitude != 0 THEN dropoff_latitude END,
passenger_count::integer,
trip_distance::numeric,
fare_amount::numeric,
extra::numeric,
mta_tax::numeric,
tip_amount::numeric,
tolls_amount::numeric,
ehail_fee::numeric,
improvement_surcharge::numeric,
total_amount::numeric,
payment_type,
trip_type::integer,
CASE
WHEN pickup_longitude != 0 AND pickup_latitude != 0
THEN ST_SetSRID(ST_MakePoint(pickup_longitude, pickup_latitude), 4326)
END,
CASE
WHEN dropoff_longitude != 0 AND dropoff_latitude != 0
THEN ST_SetSRID(ST_MakePoint(dropoff_longitude, dropoff_latitude), 4326)
END,
tmp_pickups.gid,
tmp_dropoffs.gid,
COALESCE(pickup_location_id::integer, map_pickups.taxi_zone_location_id),
COALESCE(dropoff_location_id::integer, map_dropoffs.taxi_zone_location_id)
FROM
green_tripdata_staging
INNER JOIN cab_types ON cab_types.type = 'green'
LEFT JOIN tmp_pickups ON green_tripdata_staging.id = tmp_pickups.id
LEFT JOIN nyct2010_taxi_zones_mapping map_pickups ON tmp_pickups.gid = map_pickups.nyct2010_gid
LEFT JOIN tmp_dropoffs ON green_tripdata_staging.id = tmp_dropoffs.id
LEFT JOIN nyct2010_taxi_zones_mapping map_dropoffs ON tmp_dropoffs.gid = map_dropoffs.nyct2010_gid;
TRUNCATE TABLE green_tripdata_staging;
DROP TABLE tmp_points;
DROP TABLE tmp_pickups;
DROP TABLE tmp_dropoffs;