Advertisement
azveruk

get trees for review query

Sep 15th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.         WITH recursive children_nodes as (select concat(service_name, connect_id) as tree_id,
  2.                                          connect_id,
  3.                                          service_name,
  4.                                          parent_id,
  5.                                          status,
  6.                                          level,
  7.                                          cost,
  8.                                          document_title,
  9.                                          approve_url,
  10.                                          approved_by_level,
  11.                                          ctime,
  12.                                          mtime,
  13.                                          cast(data as jsonb)
  14.                                   from delivery
  15.                                   where service_name = :service_name
  16.                                     and status = :status
  17.                                     and level = 1
  18.                                     and (d2.approve_url is null or (d2.approved_by_level = :approved_by_level))
  19.     union
  20.     select d1.tree_id,
  21.            d2.connect_id,
  22.            d2.service_name,
  23.            d2.parent_id,
  24.            d2.status,
  25.            d2.level,
  26.            d2.cost,
  27.            d2.document_title,
  28.            d2.approve_url,
  29.            d2.approved_by_level,
  30.            d2.ctime,
  31.            d2.mtime,
  32.            cast(d2.data as jsonb)
  33.     from delivery d2,
  34.          children_nodes d1
  35.     where d2.parent_id = d1.connect_id
  36.       and d2.service_name = :service_name
  37.       and d2.status =  :status
  38.       and (d2.approve_url is null or (d2.approved_by_level = :approved_by_level)))
  39. select paginated_tree_ids.trees_total, t1.*
  40. from children_nodes t1
  41.        right join (WITH all_trees_ids AS (select tree_set.tree_id
  42.                                 from (select t1.tree_id, max(t1.ctime) as max_time
  43.                                       from children_nodes t1
  44.                                              join children_nodes t2
  45.                                                on (exists(select * from children_nodes
  46.                                                            where children_nodes.tree_id = t1.tree_id
  47.                                                              and children_nodes.approve_url is not null) AND
  48.                                                 exists (
  49.             select * from children_nodes
  50.             where children_nodes.tree_id = t1.tree_id and cast(children_nodes.data ->> ':target_field' as :column_type) :operator ':target_value':::column_type
  51.         )                                                  
  52.                                       group by t1.tree_id
  53.                                       order by max_time desc) as tree_set)
  54. SELECT *
  55. FROM (TABLE all_trees_ids
  56. limit :limit offset :offset) sub
  57.        RIGHT JOIN (SELECT count(*) as trees_total FROM all_trees_ids) c ON true) as paginated_tree_ids
  58.          on t1.tree_id = paginated_tree_ids.tree_id
  59. order by t1.ctime desc;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement