I have a table which models a hierarchical data using adjacency model.
|----------|--------|--------|--------| | ParentID | ID | Flag | Name | |----------|--------|--------|--------| | 0 | 1 | | x | |----------|--------|--------|--------| | 1 | 2 | | y | |----------|--------|--------|--------| | 2 | 3 | | z | |----------|--------|--------|--------|
I have a SELECT query which sets the Flag if name does not matches certain patterns.
SELECT r.id, r.pid, r.name, r.name NOT LIKE ALL(ARRAY[patterns to
match]) AS r.flag FROM TABLE1 AS r
Now i want to flag the parents also if the child was flagged. How can i acheive that?
A left self join
will allow you to find the parent fields. BOOL_OR
can be used to aggregate the children if there are more than one.
SELECT r.id, r.pid, r.name,
CASE WHEN r.name NOT LIKE ALL(ARRAY[patterns to match]) THEN True ELSE
BOOL_OR(child.name NOT LIKE ALL(ARRAY[patterns to match])) END AS r.flag
FROM TABLE1 AS r
LEFT JOIN TABLE1 as child on child.pid=r.id
GROUP BY r.id, r.pid, r.name, r.name NOT LIKE ALL(ARRAY[patterns to match])
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.