Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- WITH recursive children_nodes as (select concat(service_name, connect_id) as tree_id,
- connect_id,
- service_name,
- parent_id,
- status,
- level,
- cost,
- document_title,
- approve_url,
- approved_by_level,
- ctime,
- mtime,
- cast(data as jsonb)
- from delivery
- where service_name = :service_name
- and status = :status
- and level = 1
- and (d2.approve_url is null or (d2.approved_by_level = :approved_by_level))
- union
- select d1.tree_id,
- d2.connect_id,
- d2.service_name,
- d2.parent_id,
- d2.status,
- d2.level,
- d2.cost,
- d2.document_title,
- d2.approve_url,
- d2.approved_by_level,
- d2.ctime,
- d2.mtime,
- cast(d2.data as jsonb)
- from delivery d2,
- children_nodes d1
- where d2.parent_id = d1.connect_id
- and d2.service_name = :service_name
- and d2.status = :status
- and (d2.approve_url is null or (d2.approved_by_level = :approved_by_level)))
- select paginated_tree_ids.trees_total, t1.*
- from children_nodes t1
- right join (WITH all_trees_ids AS (select tree_set.tree_id
- from (select t1.tree_id, max(t1.ctime) as max_time
- from children_nodes t1
- join children_nodes t2
- on (exists(select * from children_nodes
- where children_nodes.tree_id = t1.tree_id
- and children_nodes.approve_url is not null) AND
- exists (
- select * from children_nodes
- where children_nodes.tree_id = t1.tree_id and cast(children_nodes.data ->> ':target_field' as :column_type) :operator ':target_value':::column_type
- )
- group by t1.tree_id
- order by max_time desc) as tree_set)
- SELECT *
- FROM (TABLE all_trees_ids
- limit :limit offset :offset) sub
- RIGHT JOIN (SELECT count(*) as trees_total FROM all_trees_ids) c ON true) as paginated_tree_ids
- on t1.tree_id = paginated_tree_ids.tree_id
- order by t1.ctime desc;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement