ANTON KADACH

Generation

text to sql syntaxWed, 18 Jan 2023

Таблицы geo_objects(id, parent_id) и geo_object_attributes(geo_objects_id, name). geo_objects связана с таблицей geo_object_attributes geo_objects_id= id запрос, который рекурсивно находит все гео-объекты по дереву и выводит наименование всех гео-объектов дерева в одну строку postgresql

with recursive cte as ( select id, parent_id, array[id] as path from geo_objects where id = 11 union all select t.id, t.parent_id, c.path || t.id from geo_objects as t join cte as c on t.parent_id = c.id ) select array_agg(id) as path, array_agg(name) as name from cte join geo_object_attributes on geo_objects_id = id group by 1, 2;

Questions about programming?Chat with your personal AI assistant