Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- PG TREE
- truncate tree_data_cache_new;
- insert into tree_data_cache_new
- select row_number() over (ORDER BY path) as index , * from (WITH Recursive brand_tree AS (
- select "brands"."id" as "brand_id",concat('b-',LPAD(row_number() over (ORDER BY brands.name )::text, 7, '0')) as path ,'brand' as key,brands.name,concat('b-',LPAD(row_number() over (ORDER BY brands.name )::text, 7, '0'))
- from brands
- where
- ---brands.id in(2,3,4) and
- brands.is_active = true
- ),
- brand_department_tree AS (
- select "departments"."id" as "department_id",path || ',d-'::text|| LPAD(row_number() over (ORDER BY departments.name)::text, 7, '0') as path ,'department'as key,departments.name,'d-'::text|| LPAD(row_number() over (ORDER BY departments.name)::text, 7, '0')
- from
- departments
- left join departments_brands on departments.id = departments_brands.department_id
- join brand_tree on "brand_tree"."brand_id" = departments_brands.brand_id
- where key = 'brand'
- and departments.is_active = true
- ),
- department_team_tree as (
- select "teams"."id" as "team_id",path || ',t-'::text|| LPAD(row_number() over (ORDER BY teams.name)::text, 7, '0') as path ,'team' as key,teams.name,'t-'::text|| LPAD(row_number() over (ORDER BY teams.name)::text, 7, '0')
- from
- teams
- left join departments_teams on teams.id = departments_teams.team_id
- join brand_department_tree on "brand_department_tree"."department_id" = departments_teams.department_id
- where key = 'department'
- and teams.is_active = true
- ),
- team_help_topic_tree as (
- select "help_topics"."id" as "id",path || ',h-'::text|| LPAD(row_number() over (ORDER BY help_topics.name)::text, 7, '0') as path ,'help_topic' as key,help_topics.name,'h-'::text|| LPAD(row_number() over (ORDER BY help_topics.name)::text, 7, '0')
- from
- help_topics
- left join teams_help_topics on help_topics.id = teams_help_topics.help_topic_id
- join department_team_tree on "department_team_tree"."team_id" = teams_help_topics.team_id
- where key = 'team'
- and help_topics.is_active = true
- and help_topics.parent_topic_id is null
- union
- select "help_topics"."id" as "help_topic_id",path || ',s-'::text|| LPAD(row_number() over (ORDER BY help_topics.name)::text, 7, '0') as path ,'sub_topic' as key,help_topics.name,'s-'::text|| LPAD(row_number() over (ORDER BY help_topics.name)::text, 7, '0')
- from
- team_help_topic_tree
- join help_topics
- on help_topics.parent_topic_id = team_help_topic_tree.id
- where key = 'help_topic'
- and help_topics.is_active = true
- )
- SELECT * from brand_tree
- union SELECT * from brand_department_tree
- union SELECT * from department_team_tree
- union SELECT * from team_help_topic_tree
- ) as fullTree order by path
Advertisement
Add Comment
Please, Sign In to add comment