I have a table called DE
(let say it’s a table of individuals) with multiple fields, two of them are latitude (lat
field) and longitude (long
field).
I also have another point with latitude and longitude (it’s unique point I pass as arguments into the function).
I’m trying to get all the DE
records sorted based on the minimum distance between this last point and the DE point.
I’ve looked at stackoverflow and here is what I got so far, this is full of mistakes but if you had any idea how to achieve that?
create or replace function get_sorted_de(lat float, long float)
returns setof "DE" as $$
BEGIN
with distances as (
select *,point(DE.lat,DE.long)<->point(lat, long) distance from DE
)
return query ( select * from distances
order by distance LIMIT 20 );
END;
$$ language plpgsql;