Nested aggregations

I am just starting out with cypher but I must say I could use more examples in the documentation but that's just how I learn :smiley:

I have a "tree-like" database :

A->B1/B2 (several) -> C(several) -> D(several). I am trying to aggregate several properties at different levels. For example, how many B1 and B2 does A have, how many Cs does A have and so on. I would ideally expand this later on to show what is the average count of Cs for all B1s in A.

The output would ideally look like:, #B1s, #C, average(#C) per B1 for A

My cypher-fu is still quite limited but I can for example get the first level counts and averages, ie, how many B1s or B2s I have per A. Where I start to struggle is how to count total number of Cs and average number of Cs per B1 in A, or both B1s, B2s, etc.

I hope this makes sense!

I imagine you can query it directly, for specific limited questions that you have started to describe. In order to help with that it would be very helpful if you could provide a simple example graph (e.g. CREATE statements), and describe some specific query(s). For clarity, providing the right answer (hand calculated) would be ideal.

1 Like

One typical kind of aggregation is the calculation of cost up a bill of material
see Bill of Materials in Neo4j | Max De Marzi

Actually not exactly a tree because some components can be built into different sub assembly.

1 Like

Thanks everyone. I ended up going with something very simple, basically doing several subqueries/CALL {} for the time being. It actually works well because the "tree" is stereotyped enough I don't need to think recurrently!