I have a table foo
with columns id
, number_of_foos
, and parent_foo
. number_of_foos
is an integer and parent_foo
is a reference to another row's id
. Each row will have an integer in parent_foo
or null
if it has no parent.
id | number_of_foos | parent_foo
---+----------------+-----------
1 | 10 | null
2 | 7 | null
3 | 6 | null
4 | 13 | 1
5 | 9 | 3
6 | 1 | 4
Given an id
number, I want to find the total amount of "foo"s in the "foo chain", i.e. the count of foos for that id and its parent (and its parent, and its parent...). So for example, the total amount of foos WHEN id = 6
is 1 + 13 + 10 = 24.
Okay.
BUT, I also want to subtract 1 for each parent. So WHEN id = 6
is actually (1 + 13 + 10) - 2 = 22.
Is this possible?