Pandaaaa906

Untitled

May 6th, 2022 (edited)
504
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- leaf_id为末端分类的id,用于group by区分分支, distance为记录离leaf的距离
  2. with RECURSIVE t(id, parent_id, en_name, cn_name, handler, leaf_id, distance)as(
  3.     SELECT id, parent_id, en_name, cn_name, handler, id, 0
  4.     FROM product_catalog end_point
  5.     where end_point.cn_name ilike '维生素'  -- 末端分类筛选条件,值一定要非空
  6.    
  7.     union all
  8.     SELECT parent.id, parent.parent_id, parent.en_name, parent.cn_name, parent.handler, t.leaf_id, t.distance +1
  9.     FROM t, product_catalog parent
  10.     where parent.id = t.parent_id
  11. ), tree as (
  12.     SELECT
  13.     row_number() over(partition by t.leaf_id ORDER BY t.distance desc) dept,
  14.     *
  15.     from t
  16. )
  17.  
  18. SELECT
  19. tree.leaf_id,
  20. "string_agg"(tree.cn_name, '-'  ORDER BY tree.distance desc) crumbs,  -- 连接树层级关系
  21. (array_remove("array_agg"(tree.handler ORDER BY tree.distance asc), NULL))[1] "handler"  -- handler排序组成数组,去掉null值, 取第一个
  22. FROM tree
  23. GROUP BY tree.leaf_id
  24. HAVING true
  25. and (array_agg(array[tree.dept::citext, tree.cn_name::citext])) @> array['2', '非药物类产品'::citext] -- 二级分类名称筛选条件
  26. and (array_agg(array[tree.dept::citext, tree.cn_name::citext])) @> array['1', 'cato'::citext] -- 一级分类名称筛选条件
  27.  
Add Comment
Please, Sign In to add comment