I already have found a similar answer here:
Spatial clustering with PostGIS including attributes
but grouping after by cluster.
I cluster my data in this way:
SELECT ST_AsText(unnest(ST_ClusterWithin(geom, 2)))
from (
values
(ST_MakePoint(10,9),'mark',20),
(ST_MakePoint(10,9),'mark',22),
(ST_MakePoint(10,10),'steve',21),
(ST_MakePoint(10,13),'john',23)
) T(geom,name,age);
that correctly returns:
st_astext
----------------------------------------------------------
GEOMETRYCOLLECTION(POINT(10 9),POINT(10 9),POINT(10 10))
GEOMETRYCOLLECTION(POINT(10 13))
But what I really need is that once the data is clustered by "location" it needs to pick the first in alphabetical order desc, and if the same name, by the age desc. A sort of:
select * from (
select *, row_number () over (order by name desc, age desc)
from (pre-clustered-data) T
) K where row_number = 1;
How can I do that with Postgis?