ANTON KADACH

Generation

code to explanationWed, 18 Jan 2023

with recursive r as ( select 1 as i,go2.parent_id , goa.id,goa."name",goa."type",goa.postal_code,goa.source_id,goa.external_id,goa.external_ids_history,goa.partition_code,goa.is_deleted,goa.timezone from geo_objects_attributes goa join geo_objects go2 on go2.id = goa.id where goa."type" ='Аобл' union select i+1 as i,go2.parent_id, goa.id,goa."name",goa."type",goa.postal_code,goa.source_id,goa.external_id,goa.external_ids_history,goa.partition_code,goa.is_deleted,goa.timezone from geo_objects_attributes goa join geo_objects go2 on go2.id = goa.id join r on r.id = go2.parent_id ) SELECT* from r JOIN geo_polygons gp ON gp.geo_id = r.id;

The recursive part of the query builds a table r that has all objects at all levels. This is done by iteratively joining the table itself with the geo_objects_attributes and geo_objects tables. The top level region is added in the first part of the union (select 1) and then the geo_objects_attributes and geo_objects tables are joined with the table r itself. This is done for all levels of the hierarchy. In the end, the result is simply joined with the geo_polygons table.

Questions about programming?Chat with your personal AI assistant