# Aggregation on infinitely recursive query

Hi! I’m kinda new to dgraph so please excuse me if the description of my problem is a little vague and/or confusing.

Basically, I have a type that has an edge of that same type. So let’s say:

type Person {
name
pid
score
}

name: string @index(term) .
score: int .
pid: uid @reverse .

Those edges can go pretty much to any depth but they are not circular.

Basically what I’d like to do is compute the max value of score in the whole tree for a given person. So basically if I had a tree that looked something like this:

A -> B -> C -> D -> G
1     7      2       5     4

where the letters are the nodes and the numbers are their scores.

I would like to be able to get for example the node C with all it’s attributes (name, pid and score) and additionally the max score of the tree below it (5 in this case).
Or if I queried for node B then I would get all the attributes and the max score, which would be 7 (ideally the score of the node itself would be considered in the max aggregation there).

I’m a little lost on where to even start as I read that there’s no support for recursive aggregations, so that kinda threw me off, but I figure there might be some other way to achieve that that I’m missing?

1 Like

Welcome @tomsawyer
Please try the following query. It provides the information of “C” as well as its parents. The max score upto that point is also reported.

{
traverse(func: eq(name, "C")  ) @recurse(depth:10 , loop:false){
name
sc as score
~pid

}

result(  ) {
max(val(sc))
}
}

The response I get is:

"data": {
"traverse": [
{
"name": "C",
"score": 2,
"~pid": [
{
"name": "B",
"score": 7,
"~pid": [
{
"name": "A",
"score": 1
}
]
}
]
}
],
"result": [
{
"max(val(sc))": 7
}
]
}