Last active
January 1, 2024 04:00
-
-
Save firstDismay/953a3fdb3746bad388182f35fdf2b54b to your computer and use it in GitHub Desktop.
Работа с массивом пути для иерархических сущностей в PostgreSQL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#С учетом того факта, что массивы в PostgreSQL это упорядоченный список для формирования строкового представления пути | |
#можно использовать упорядоченный массив идентификаторов пути. Что бы извлечь их с сохранением нужного порядка я использую этот запрос: | |
SELECT p.id, | |
( SELECT string_agg(pp.name::text, '\'::text ORDER BY ps.ord) AS string_agg | |
FROM unnest(p.path_array) WITH ORDINALITY ps(id, ord) | |
JOIN entity pp ON pp.id = ps.id) AS path | |
FROM entity p | |
GROUP BY p.id; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment