----- "Ivan Mikhailov" <imikhai...@openlinksw.com> wrote: > From: "Ivan Mikhailov" <imikhai...@openlinksw.com> > To: "Peter Ansell" <p_ans...@yahoo.com> > Cc: "Virtuoso Users List" <virtuoso-users@lists.sourceforge.net> > Sent: Tuesday, 21 October, 2008 3:26:07 PM GMT +10:00 Brisbane > Subject: Re: [Virtuoso-users] Aggregate queries using CONSTRUCT > > Hello Peter, > > First of all, there's a syntax error in construct pattern. Expressions > should be backquoted when they appear in triple patterns as graphs, > subjecs, predicates or objects. Construct pattern is a pattern, too, so > it should be > > CONSTRUCT { <http://bio2rdf.org/interpro:IPR000181> > <http://bio2rdf.org/ns/bio2rdf#hasLinkCount> `COUNT(?s)` . } > WHERE { ?s ?p <http://bio2rdf.org/interpro:IPR000181> . } > > Next problem is that aggregates are not allowed to be direct arguments > of aggregates, and CONSTRUCT is an aggregate by itself. One can't write > COUNT(COUNT(x)), that is true for CONSTRUCT(...COUNT(...)) as well.
I wasn't aware that this was the reason it wasn't going to work, but thanks for explaining it! > I've tried the following workaround: > > CONSTRUCT { <http://bio2rdf.org/interpro:IPR000181> > <http://bio2rdf.org/ns/bio2rdf#hasLinkCount> ?countS . } > WHERE { > { select (count(?s)) as ?countS > where { ?s ?p <http://bio2rdf.org/interpro:IPR000181> . } } } > > That is a valid SPARQL and it is compiled to SQL that looks good for me. > Unfortunately, SQL optimizer do not agree with me, because it realizes > that I want to fool it. > > So the proper version is > > CONSTRUCT { > <http://bio2rdf.org/interpro:IPR000181> > <http://bio2rdf.org/ns/bio2rdf#hasLinkCount> > `(select (count(?s)) as ?countS > where { ?s ?p <http://bio2rdf.org/interpro:IPR000181> })` } > where { ?s1 ?p1 ?o1 } limit 1 Just to check I understand... The real query is inside the CONSTRUCT and the LIMIT 1 with ?s1 ?p1 ?o1 is just to ensure it returns results and is syntactically valid SPARQL? > Note backquotes _and_ parentheses around the scalar subquery. Backquotes > are to delimit expression in pattern, parentheses are to delimit scalar > subquery. > > Unfortunately, this query does not work on VOS versions earlier than > 5.0.9-rc6 . Thanks for looking into it! It is not time-critical at the moment to have this query. I will put it into my package when I upgrade after 5.0.9 is released. Cheers, Peter > Best Regards, > > Ivan Mikhailov, > OpenLink Software > http://virtuoso.openlinksw.com > > On Sun, 2008-10-19 at 23:20 -0700, Peter Ansell wrote: > > Hi all, > > > > The documentation for the aggregation queries doesn't specifically > say that it is only supported for SELECT queries [1] [2], but I have > had no success in getting COUNT(?variable) to work inside of > CONSTRUCT, where it works for a similar SELECT query. Any ideas about > whether this is or will be supported in the future? > > > > For reference the two queries are as follows on > http://quebec.bio2rdf.org/sparql > > > > SELECT COUNT(?s) WHERE { ?s ?p > <http://bio2rdf.org/interpro:IPR000181> . } > > > > * http://tinyurl.com/5wrtcc > > > > CONSTRUCT { <http://bio2rdf.org/interpro:IPR000181> > <http://bio2rdf.org/ns/bio2rdf#hasLinkCount> COUNT(?s) . } WHERE { > ?s ?p <http://bio2rdf.org/interpro:IPR000181> . } > > > > * http://tinyurl.com/662vnh > > > > Cheers, > > > > Peter > > > > [1] http://docs.openlinksw.com/virtuoso/sparqlbi.html > > [2] http://docs.openlinksw.com/virtuoso/rdfsparqlaggregate.html Send instant messages to your online friends http://au.messenger.yahoo.com