I have a table with the lat & lng for two points:
CREATE TABLE location_pairs (
lat_1 float,
lng_1 float,
lat_2 float,
lng_2 float
);
I also have a function that takes two lat/lng coordinates pairs and returns a table with several calculated columns (distance in meters, distance in degrees, etc).
Simplified function:
CREATE OR REPLACE FUNCTION distances(lng_1 float, lat_1 float, lng_2 float, lat_2 float)
RETURNS TABLE (
distance_m float,
distance_l float,
distance_actual float,
corrected_ratio float
) AS $$
BEGIN
RETURN QUERY
...
;
END;
$$ LANGUAGE plpgsql;
I want to write a query that will take the original table and add the columns from the function results:
lat_1 | lng_1 | lat_2 | lng_2 | distance_m | etc
-------+-------+-------+--------+------------+-----
0 | 0 | 1 | 1 | x | ...
So far, what I have is this:
SELECT
*,
distances(lng_1, lat_1, lng_2, lat_2)
FROM location_pairs;
which gives the result
lat_1 | lng_1 | lat_2 | lng_2 | distances
-------+-------+-------+--------+-----------------------------
0 | 0 | 1 | 1 | (x, y, other, results, ...)
How do I split the distances
column into the columns that result from the function? I’m assuming I need to re-structure my query, but I’m not sure how.
2
Answers
Use the function like a table in the FROM:
You have to use the function in the
FROM
. But first I suggest you to create a type to avoid complaints from the server and declare the return as the type instead of as table.If you need to join with the inputs