v131313

make tree from tables linked with many to many ralation

Jun 25th, 2025 (edited)
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PostgreSQL 2.69 KB | Software | 0 0
  1. PG TREE
  2.  
  3. truncate tree_data_cache_new;  
  4. insert into tree_data_cache_new  
  5. select row_number() over (ORDER BY path) as index , * from (WITH Recursive brand_tree AS (  
  6. 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'))  
  7. from brands  
  8. where  
  9. ---brands.id in(2,3,4) and  
  10. brands.is_active = true  
  11. ),  
  12. brand_department_tree AS (  
  13. 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')  
  14. from  
  15. departments  
  16. left join departments_brands on departments.id = departments_brands.department_id  
  17. join brand_tree on "brand_tree"."brand_id" = departments_brands.brand_id  
  18. where key = 'brand'  
  19. and departments.is_active = true  
  20. ),  
  21. department_team_tree as (  
  22. 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')  
  23. from  
  24. teams  
  25. left join departments_teams on teams.id = departments_teams.team_id  
  26. join brand_department_tree on "brand_department_tree"."department_id" = departments_teams.department_id  
  27. where key = 'department'  
  28. and teams.is_active = true  
  29. ),  
  30. team_help_topic_tree as (  
  31. 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')  
  32. from  
  33. help_topics  
  34. left join teams_help_topics on help_topics.id = teams_help_topics.help_topic_id  
  35. join department_team_tree on "department_team_tree"."team_id" = teams_help_topics.team_id  
  36. where key = 'team'  
  37. and help_topics.is_active = true  
  38. and help_topics.parent_topic_id is null  
  39. union  
  40. 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')  
  41. from  
  42. team_help_topic_tree  
  43. join help_topics  
  44. on help_topics.parent_topic_id = team_help_topic_tree.id  
  45. where key = 'help_topic'  
  46. and help_topics.is_active = true  
  47. )  
  48. SELECT * from brand_tree  
  49. union SELECT * from brand_department_tree  
  50. union SELECT * from department_team_tree  
  51. union SELECT * from team_help_topic_tree  
  52. ) as fullTree order by path
  53.  
Tags: sql
Advertisement
Add Comment
Please, Sign In to add comment