I have a table of clients where I have the location of the client. The table has the latitude and longitude column and the geom field that creates it from latitude and longitude with the following query
ALTER TABLE clients_details_locations ADD COLUMN geom geometry(Point, 4326);
UPDATE clients_details_locations
SET geom = ST_SetSRID(ST_MakePoint(longitude , latitude), 4326);
Then create an index like this
CREATE INDEX clients_details_locations_geom_idx ON clients_details_locations USING GIST (geom);
and then
VACUUM ANALYZE public.clients_details_locations
what I want to get are the first 100 points and from them create a polygon
I have tried the following query
SELECT cdl.id,client_id ,cdl.agent_id , cdl.department_id , cdl.district_id ,cdl.latitude , cdl.longitude , cdl.geom
FROM clients_details_locations cdl
INNER JOIN "agents_georegions3" ON st_within(st_setsrid(cdl.geom, 4326), agents_georegions3.geom) = True
and "agents_georegions3"."id" in (SELECT id FROM agents_georegions3 WHERE cabecera ilike '%mar del plata%')
and cdl.district_id = 11673
and cdl.agent_id in (SELECT id FROM agents where type_id = 1)
and cdl.department_id in (SELECT id FROM departments WHERE description='BUENOS AIRES')
ORDER BY cdl.geom
LIMIT 100 offset 0
query output
As can be seen in the image, the first 100 points (red points) do not bring it to me well, so that the result can be understood, see that the points inside the red triangle are close and it did not bring it to me among the first 100 points. I don't know how to solve to get the first 100 points ordered by their location or their proximity.
I use LIMIT and OFFSET because I want to get the points by 100 to create a polygon with 100 points each.