|
1 | 1 | CREATE TABLE cities ( |
2 | | - id serial PRIMARY KEY, |
3 | | - name text, |
| 2 | + name text PRIMARY KEY, |
4 | 3 | geom geometry(Point, 4326) |
5 | 4 | ); |
6 | 5 |
|
| 6 | +CREATE TABLE trips ( |
| 7 | + id serial PRIMARY KEY, |
| 8 | + city text REFERENCES cities(name), |
| 9 | + time timestamptz, |
| 10 | + start_time timestamptz, |
| 11 | + end_time timestamptz |
| 12 | +); |
| 13 | + |
| 14 | +CREATE TABLE receipts ( |
| 15 | + id serial PRIMARY KEY, |
| 16 | + trip_id int REFERENCES trips(id), |
| 17 | + time timestamptz, |
| 18 | + amount numeric |
| 19 | +); |
| 20 | + |
| 21 | + |
7 | 22 | INSERT INTO cities (name, geom) VALUES |
8 | 23 | ('Paris', ST_SetSRID(ST_MakePoint(2.3522, 48.8566), 4326)), |
| 24 | + ('London', ST_SetSRID(ST_MakePoint(-0.1276, 51.5074), 4326)), |
| 25 | + ('Tokyo', ST_SetSRID(ST_MakePoint(139.6917, 35.6895), 4326)), |
| 26 | + ('Sydney', ST_SetSRID(ST_MakePoint(151.2093, -33.8688), 4326)), |
9 | 27 | ('NYC', ST_SetSRID(ST_MakePoint(-74.0060, 40.7128), 4326)); |
10 | 28 |
|
| 29 | + |
| 30 | + |
| 31 | +INSERT INTO trips (city, time, start_time, end_time) VALUES |
| 32 | + ('Paris', '2025-01-01 12:00:00', '2024-01-01 12:00:00', '2025-01-01 12:00:00'), |
| 33 | + ('London', '2025-02-01 12:00:00', '2024-02-01 12:00:00', '2025-02-01 12:00:00'), |
| 34 | + ('Tokyo', '2025-03-01 12:00:00', '2024-03-01 12:00:00', '2025-03-01 12:00:00'), |
| 35 | + ('Sydney', '2025-04-01 12:00:00', '2024-04-01 12:00:00', '2025-04-01 12:00:00'), |
| 36 | + ('NYC', '2025-05-01 12:00:00', '2024-05-01 12:00:00', '2025-05-01 12:00:00'); |
| 37 | + |
| 38 | + |
| 39 | +INSERT INTO receipts (trip_id, time, amount) VALUES |
| 40 | + (1, '2024-06-01 12:00:00', 100.00), |
| 41 | + (1, '2024-07-01 12:00:00', 150.00), |
| 42 | + (2, '2024-06-15 12:00:00', 200.00), |
| 43 | + (3, '2024-08-01 12:00:00', 250.00), |
| 44 | + (4, '2024-09-01 12:00:00', 300.00), |
| 45 | + (5, '2024-10-01 12:00:00', 350.00); |
| 46 | + |
11 | 47 | -- View with geometry and featureID column (no PK) |
12 | 48 | CREATE VIEW cities_view AS |
13 | | - SELECT id AS id, name, geom FROM cities; |
| 49 | + SELECT * FROM cities; |
| 50 | + |
| 51 | +CREATE VIEW trips_view AS |
| 52 | + SELECT trips.*, cities.geom FROM trips LEFT JOIN cities ON trips.city = cities.name; |
14 | 53 |
|
| 54 | +CREATE VIEW receipts_view AS |
| 55 | + SELECT receipts.*, cities.geom FROM receipts LEFT JOIN trips ON receipts.trip_id = trips.id LEFT JOIN cities ON trips.city = cities.name; |
0 commit comments