%META:TOPICPARENT{name="VirtTipsAndTricksGuide"}%
---+ How can I include aggregate function values as objects in SPARQL CONSTRUCT
queries?
For a CONSTRUCT
query, the aggregate function must be called as part of a subquery in the CONSTRUCT
's WHERE
clause.
For example, starting with this SPARQL SELECT
query --
SELECT DISTINCT ?category
COUNT(?category) AS ?count
WHERE
{
?s ?category
FILTER (
?s =
||
?s =
)
}
The CONSTRUCT
query would be --
CONSTRUCT
{
?category a .
?category ?count
}
WHERE
{
{
SELECT DISTINCT ?category
COUNT(?category) AS ?count
WHERE
{
?s ?category
FILTER (
?s =
||
?s =
)
}
}
}
[[http://lod.openlinksw.com/sparql?default-graph-uri=&query=CONSTRUCT+%0D%0A++%7B%0D%0A++++%3Fcategory++a+++++++++++++++++++++++++++++++++++++++++++++++++++++%3Chttp%3A%2F%2Fwww.w3.org%2F2004%2F02%2Fskos%2Fcore%23Concept%3E+.%0D%0A++++%3Fcategory++%3Chttp%3A%2F%2Fwww.turnguard.com%2Fvirtuoso%2Faggregates%23count%3E++%3Fcount%0D%0A++%7D%0D%0AWHERE+%0D%0A++%7B%0D%0A++++%7B+%0D%0A++++++SELECT+++++++++++++DISTINCT+%3Fcategory+%0D%0A++++++++++++++COUNT%28%3Fcategory%29+AS+%3Fcount%0D%0A++++++WHERE%0D%0A++++++++%7B%0D%0A++++++++++%3Fs+++%3Chttp%3A%2F%2Fpurl.org%2Fdc%2Fterms%2Fsubject%3E+%3Fcategory%0D%0A++++++++++FILTER+%28%0D%0A+++++++++++++++++++%3Fs++%3D%3Chttp%3A%2F%2Fdbpedia.org%2Fresource%2FHiggs_boson%3E+%0D%0A+++++++++++++++++++||+%0D%0A+++++++++++++++++++%3Fs++%3D%3Chttp%3A%2F%2Fdbpedia.org%2Fresource%2FGluon%3E%0D%0A+++++++++++++++++%29%0D%0A++++++++%7D+%0D%0A++++%7D+%0D%0A++%7D&should-sponge=&format=application%2Frdf%2Bxml&CXML_redir_for_subjs=121&CXML_redir_for_hrefs=&timeout=15000&debug=on][View the results]] of the query execution on the [[http://lod.openlinksw.com][LOD]] instance.
---++Related
* [[VirtTipsAndTricksGuide][Virtuoso Tips and Tricks Collection]]