On 14/10/15 13:56, Wetz Peter wrote:
Dear all,

I am using an app, which uses Jena under the hood.

In the app, I can create SPARQL queries. I want to create a SPARQL query which 
uses the AVG() aggregate function. However, when I include AVG() in my query, 
it yields no results.

Presumably you mean you get an empty item in the row, not no rows.

No rows means there are no groups.


I can use COUNT() and it works. But I cannot use SUM(). If SUM() would work, I 
could calculate AVG() on my own.

There are two solutions in my opinion:
1) Since I read somewhere, that Jena should typically support SUM(), I would 
like to find out why it seems not to work for me. Maybe someone here can help 
to identify the reason?
The app, uses the following libraries from jena (when did Jena ARQ start to 
support SUM()?):
jena-arq-Percentile-2.11.1.jar
jena-core-2.11.1.jar
jena-iri-1.0.1.jar
jena-tdb-1.0.1.jar

Upgrade to 3.0.0


2) Implement my own aggregate, that is, AVG(): Since I think, I am not the 
first person in the world which needs AVG() in Jena, I think there should 
already be the solution out there somewhere? Help is appreciated.

AVG and SUM should work - the problem is likely the data.

Note that AVG and SUM are sensitive to errors: if any one item in the group is not a number, then AVG/SUM is undefined. This is the spec definition.

You can use COALESCE, and other ways, to default non-numbers.

AVG(COALESCE(xsd:double(?x),0))

for example.  Cast to double (includes strings) and if not use the value 0.

Without a complete, minimal example, that's about all I can say.

        Andy


Best regards,
Peter


Reply via email to