You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@jena.apache.org by Wetz Peter <pe...@tuwien.ac.at> on 2015/10/14 14:56:40 UTC

SUM() and AVG() aggregates?

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.

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

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.

Best regards,
Peter

AW: SUM() and AVG() aggregates?

Posted by Wetz Peter <pe...@tuwien.ac.at>.
> AVG and SUM should work - the problem is likely the data.

Correct.

> 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.

"AVG(COALESCE(xsd:double(?x),0))" yields results, finally. However, they are "0.0". So this implies, that something with the data is wrong.

And I think I know the reason: For some reason, the values, which I want to calculate the average over, are typed as an URI, even though the value of this URI is a number (e.g. "10"). Obviously, Jena is not capable of calculating the average over URIs. Since I have a whole pre-processing pipeline before I push the triples to Jena, I need to check that in more detail. If I cannot sort it out that way, I'd be happy to ask here again (possibly with a minimal example).

If you have some other hint for me in the meantime, please let me know.

Thanks for your help already.

Best,
Peter


Re: SUM() and AVG() aggregates?

Posted by Andy Seaborne <an...@apache.org>.
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
>


Re: SUM() and AVG() aggregates?

Posted by Rob Vesse <rv...@dotnetrdf.org>.
Peter

Jena has supported SUM() for a long time (not sure exactly when) and also
includes AVG() both of which are part of the SPARQL 1.1 specification.

I am not sure what jena-arq-Percentile-2.11.1.jar is (never seen that JAR
before) so are you getting the Jena downloads from some third party source?

The current Jena release is 3.0.0 which requires Java 8, if you need to
stick with Java 7 then you can use 2.13.0 but that is not being actively
maintained.  So I would first suggest you upgrade to a more recent version

If you are stuck with an old version then the issue is likely that older
versions did not default to SPARQL 1.1 syntax by default so you had to
explicitly tell ARQ that your query was a SPARQL 1.1 query e.g.

Query q = QueryFactory.create(query, Syntax.syntaxSPARQL_11);

However I would still strongly recommend you upgrade to a newer Jena
version if at all possible

Rob

On 14/10/2015 13:56, "Wetz Peter" <pe...@tuwien.ac.at> 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.
>
>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
>
>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.
>
>Best regards,
>Peter