Error: Only variables allowed in aggregate functions


(Calum Moore) #1

If I copy and paste the SUM aggregation example from the documentation. I get the following error:

Only variables allowed in aggregate functions. Got: age

Documentation:
https://docs.dgraph.io/v0.7.5/query-language/#sum-avg

I’ve tried a couple of other attempts at making SUM work and not been successful so far. Not sure if I should be raising this here, or directly onto GitHub issues?


(Manish R Jain) #2

You’ll have to pass a var(x) inside sum, so it’s sum(var(x)). I suppose you’re trying to do something like sum(x) directly, which won’t work.

Can you list your query here, so we can have a look at it?


(Calum Moore) #3

I tried the following query from the documentation:

mutation {
 set {
	<0x01> <name> "Alice"^^<xs:string> .
	<0x02> <name> "Tom"^^<xs:string> .
	<0x03> <name> "Jerry"^^<xs:string> .
	<0x04> <name> "Teddy"^^<xs:string> .
	# friend of 0x01 Alice
	<0x01> <friend> <0x02> .
	<0x01> <friend> <0x03> .
	<0x01> <friend> <0x04> .
	# set age
	<0x02> <age> "99"^^<xs:int> .
	<0x03> <age> "100"^^<xs:int>	.
	<0x04> <age> "101"^^<xs:int>	.
 }
}
query {
	me(id:0x01) {
		friend {
			name
			age
			sum(age)
			avg(age)			
		}
	}
}

It would be nice to be able to query on a field rather than having to create a variable.


(Manish R Jain) #5

I’ll let @ashwin95r speak here.


(Ashwin) #6

The reason we went with the current approach is because the aggregate should be at the parent level. In this example that would be with the root node. It’s better represented by using variables than directly using the predicate.


(system) #7

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.