I have a table structure where there are CARS
going between BUILDINGS
that are built on PLOTS
:
CARS
CAR_ID | ORIGIN | DESTINATION
----------------------------------
1 | A | C
2 | B | A
3 | A | B
BUILDINGS
BUILD_ID | PLOT
------------------
A | 2
B | 1
C | 3
PLOTS
PLOT_ID | COORD
------------------------
1 | "39.9,-75.5"
2 | "38.3,-74.7"
3 | "37.8,-76.1"
I'm trying to build a query that would show for each CAR
, the PLOT
coordinates of the origin and destination BUILDING
s, like this:
CAR_ID | ORIGIN_COORD | DEST_COORD
-------------------------------------------
1 | "38.3,-74.7" | "37.8,-76.1"
2 | "39.9,-75.5" | "38.3,-74.7"
3 | "39.9,-75.5" | "39.9,-75.5"
This is what I tried but I don't think I'm approaching this right.
SELECT * FROM
(SELECT BUILD_ID, PLOT, COORD FROM BUILDINGS
INNER JOIN PLOTS ON PLOT = PLOT_ID) X
RIGHT JOIN CARS C
ON C.ORIGIN = X.BUILD_ID
Could someone please help me understand how to lookup/join for multiple columns (ORIGIN
and DEST
)?