Skip to content
Advertisement

Recursive select that selects rows based own plus childrens values

I need to select rows in a table like this:

table a

Select all rows in the table where both conditions are met:

  • Condition 1: the value column should not match with any value in table v

  • Condition 2: no decendent (on any level, ie: child or sub child, sub- sub- child etc) has a value that matches with any value in table v

Table v looks like this:

table v

Expected result from example table. Should [row] be selected/returned?

  • a1: No – condition 2
  • a2: No – condition 2
  • a3: No – condition 1
  • a4: No – condition 1
  • a5: Yes – (value does not match in v and no decendents that match in v)
  • a6: Yes – (value does not match in v and no decendents that match in v)
  • a7: Yes – (value does not match in v and no decendents that match in v)
  • a8: Yes – (value does not match in v and no decendents that match in v)

Here is an sqlfiddle where the tables are set up together with a recursive function that shows all rows and their level in the tree, but that I don’t know how to procede with:

http://sqlfiddle.com/#!18/736a28/15/0

Advertisement

Answer

Check this solution:

Result Set (fits requested)):

enter image description here

For the sake of the discussion let’s add another row which lead rows with id a8 and a9 to be “NO” since it is child of a9 and has value from the second table

test 2 Result set (fits expected)

enter image description here

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement