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

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 .

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



Reply via email to