I´m trying to determine the average progress of each task group in a tasks tree. The problem is that only the leaf nodes (which are in a diferent table ‒ a child table ‒) actually have the progress field.
I have a Tasks (parent) table and an Activities (child) table. Both of them share IDs. I should point out that I´m using the ltree extension in my DB.
I´m trying to make a view showing the entire tree and a column with the average progress per node level. I have to start the calculations from the inner-most level and build my way up in the tree structure, but I can´t seem to get it right.
I´ve tried to approach this using recursive CTEs and window functions to no avail. Postgres doesn´t allow to use aggregate functions in the recursive term of a recursive cte, so that didn´t work.
These are my tables (simplified):
tasks:
=======
id -- SERIAL
parent_id -- INTEGER
project_id -- INTEGER
name -- VARCHAR
task_group -- BOOL
path -- LTREE
activities:
===========
id -- INTEGER
progress -- INTEGER
EDIT
This is a screenshot of the view as of right now. I´m able to get the average progress of all leaf nodes, but that´s not exactly what I need. The average progress should be calculated for each level in the tree, and then build it´s way up in the node hierarchy.
Any insight on this will be much appreciated. Thanks in advance.