S
S
Sergei Iamskoi2016-07-06 11:50:52
PostgreSQL
Sergei Iamskoi, 2016-07-06 11:50:52

How to sort by array in PostgreSQL?

There are two tables. The first planet_osm_ways:
id :: bigint, nodes :: bigint[], tags :: text[] - it contains the object and the nodes field (bigint array) contains a list of the object's points in the required order.
The second table is planet_osm_nodes - it contains points with coordinates.
id :: bigint, lat::bigint, lon::bigint
Request:

select
    pon.id,
    ST_FlipCoordinates(ST_Transform(ST_GeomFromText('POINT(' || lon / 100 || ' ' || lat / 100 || ')', 3785), 4326)) as coords
from
    planet_osm_ways AS pow
left join planet_osm_nodes AS pon on pon.id = any (pow.nodes)
where
    pow.id = 128676234
order by 
    pow.nodes -- ??

No matter how he twists the request and sorting, it sorts by pon.id. Is it possible to make it output a list of points in the order specified in the planet_osm_ways.nodes array?? For if you do it sorted in ascending order, then the polygons are not collected as it should (((

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
terrier, 2016-07-06
@syamskoy

If I understand correctly, perhaps this will help:
You can sort by the results of some function. That is,
select
...
from
planet_osm_ways AS pow
left join planet_osm_nodes AS pon on pon.id = any (pow.nodes)
where
pow.id = 128676234
order by
my_cool_sorting_func( pow.nodes );
Where my_cool_sorting_func is a function that takes an array as input and returns a result (for example, int ), which will determine the sort order

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question