SPARQLAggregate["var"agg]
is a query operator that yields a solution containing the variable "var" whose value is computed from the aggregate agg.
SPARQLAggregate[{"var1"agg1,"var2"agg2,…}]
yields a solution containing multiple aggregates.
SPARQLAggregate[aggs,groupby]
aggregates solutions grouped by the value of groupby.
SPARQLAggregate[aggs,groupby,having]
includes only groups for which having evaluates to True.
SPARQLAggregate[aggs,groupby,having,orderby]
orders groups by the value of orderby.
SPARQLAggregate
SPARQLAggregate["var"agg]
is a query operator that yields a solution containing the variable "var" whose value is computed from the aggregate agg.
SPARQLAggregate[{"var1"agg1,"var2"agg2,…}]
yields a solution containing multiple aggregates.
SPARQLAggregate[aggs,groupby]
aggregates solutions grouped by the value of groupby.
SPARQLAggregate[aggs,groupby,having]
includes only groups for which having evaluates to True.
SPARQLAggregate[aggs,groupby,having,orderby]
orders groups by the value of orderby.
Details and Options
- A RightComposition of operators SPARQLSelect[…]/*…/*SPARQLAggregate[…]/*… can be used in SPARQLExecute to query a SPARQL endpoint. Composition is also supported.
- SPARQLAggregate[…] can be applied to the result of a SPARQLSelect query.
- In SPARQLAggregate["var"agg], agg is usually of the form SPARQLEvaluation["f"][u1,u2,…], where "f" is the name of a SPARQL aggregate function, and each ui is a SPARQLVariable whose associated values withing a solution group are supplied to the aggregate function.
- Named SPARQL aggregate functions include:
-
"COUNT" counts the number of elements in a group "SUM" totals the values of a group "MIN" returns the minimum value in a group "MAX" returns the maximum value in a group "AVG" returns the averag of the values in a group "GROUP_CONCAT" joins String values of a group "SAMPLE" returns an arbitrary value from the group - SPARQLEvaluation["GROUP_CONCAT"][var, "Separator" -> "sep"] can be used to join strings using separator "sep". The default separator is " " (space).
Examples
open all close allBasic Examples (1)
Needs["GraphStore`"]fruitData = GraphStore`RDFStore[{GraphStore`RDFTriple[URL["http://example.org/banana"],
URL["http://www.w3.org/1999/02/22-rdf-syntax-ns#type"], URL["http://example.org/Fruit"]],
GraphStore`RDFTriple[URL["http://example.org/banana"], URL["http://schema.o ... ttp://example.org/strawberry"],
URL["http://www.w3.org/1999/02/22-rdf-syntax-ns#type"], URL["http://example.org/Fruit"]],
GraphStore`RDFTriple[URL["http://example.org/strawberry"], URL["http://schema.org/color"],
"red"]}, Association[]];rdf[s_] := URL["http://www.w3.org/1999/02/22-rdf-syntax-ns#" <> s];
schema[s_] := URL["http://schema.org/" <> s];
ex[s_] := URL["http://example.org/" <> s];fruitData//SPARQLQuery[SPARQLSelect[
RDFTriple[SPARQLVariable["fruit"], rdf["type"], ex["Fruit"]]
] /* SPARQLAggregate["count" -> SPARQLEvaluation["count"][]]]Find the number of fruits per color:
fruitData//SPARQLQuery[SPARQLSelect[{
RDFTriple[SPARQLVariable["fruit"], rdf["type"], ex["Fruit"]],
RDFTriple[SPARQLVariable["fruit"], schema["color"], SPARQLVariable["color"]]
}] /* SPARQLAggregate[
{"color", "count" -> SPARQLEvaluation["count"][]},
SPARQLVariable["color"]
]]Scope (4)
Needs["GraphStore`"]Compute the total of the values associated with key "x":
{<|"x" -> 1|>, <|"x" -> 2|>}//SPARQLAggregate[
"total" -> SPARQLEvaluation["sum"][SPARQLVariable["x"]]
]Aggregate after grouping by the value of key "y":
data = {
<|"x" -> 1, "y" -> "a"|>,
<|"x" -> 5, "y" -> "a"|>,
<|"x" -> 100, "y" -> "b"|>,
<|"x" -> 50, "y" -> "c"|>,
<|"x" -> 80, "y" -> "c"|>
};data//SPARQLAggregate[
{"y", "total" -> SPARQLEvaluation["sum"][SPARQLVariable["x"]]},
SPARQLVariable["y"]
]Filter groups by the mean of the values of key "x":
data//SPARQLAggregate[
"avg" -> SPARQLEvaluation["avg"][SPARQLVariable["x"]],
SPARQLVariable["y"],
SPARQLEvaluation["avg"][SPARQLVariable["x"]] > 20
]Order groups by a sample of the values of key "y" in descending order:
data//SPARQLAggregate[
"avg" -> SPARQLEvaluation["avg"][SPARQLVariable["x"]],
SPARQLVariable["y"],
SPARQLEvaluation["avg"][SPARQLVariable["x"]] > 20,
SPARQLEvaluation["sample"][SPARQLVariable["y"]] -> "Descending"
]Needs["GraphStore`"]Data about works and composers:
musicGraph = ImportString["
prefix : <http://example.org/>
:LaCumparsita :composer :GerardoMatosRodriguez.
:LaMorocha :composer :EnriqueSaborido.
:Felicia :composer :EnriqueSaborido.
", "Turtle"]Count the number of works by composer:
ex[s_] := URL["http://example.org/" <> s];musicGraph//SPARQLQuery[
SPARQLSelect[RDFTriple[SPARQLVariable["work"], ex["composer"], SPARQLVariable["composer"]]] /* SPARQLAggregate[
{"count" -> SPARQLEvaluation["COUNT"][], "composer"},
SPARQLVariable["composer"],
True,
SPARQLVariable["count"] -> "Descending"
]
]The same query using a SPARQL query string:
musicGraph//SPARQLQuery["
prefix ex: <http://example.org/>
select (count(*) as ?count) ?composer where {
?work ex:composer ?composer .
} group by ?composer order by desc(?count)
"]Needs["GraphStore`"]Specify a graph which contains multiple values per entity for property p1:
ex[s_] := URL["http://example.org/" <> s];graph = RDFStore[{
RDFTriple[ex["e1"], ex["p1"], 11],
RDFTriple[ex["e1"], ex["p1"], 111],
RDFTriple[ex["e1"], ex["p2"], "a"],
RDFTriple[ex["e2"], ex["p1"], 21],
RDFTriple[ex["e2"], ex["p2"], "b"]
}];Obtain the data, taking for each entity the maximum value of property p1:
graph//SPARQLQuery[
SPARQLSelect[{
SPARQLSelect[RDFTriple[SPARQLVariable["e"], ex["p1"], SPARQLVariable["p1"]]] /* SPARQLAggregate[{"e", "p1max" -> SPARQLEvaluation["max"][SPARQLVariable["p1"]]}, SPARQLVariable["e"]],
RDFTriple[SPARQLVariable["e"], ex["p2"], SPARQLVariable["p2"]]
} -> {"p1max", "p2"}]
]Needs["GraphStore`"]Specify a separator for the "GROUP_CONCAT" aggregate:
{<|"x" -> "a"|>, <|"x" -> "b"|>, <|"x" -> "c"|>, <|"x" -> "d"|>}//SPARQLAggregate["s" -> SPARQLEvaluation["GROUP_CONCAT"][SPARQLVariable["x"], "Separator" -> "~"]]Generalizations & Extensions (1)
Needs["GraphStore`"]The aggregate can be an arbitrary function:
{<|"x" -> 1|>, <|"x" -> 20|>, <|"x" -> 300|>}//SPARQLAggregate["agg" -> SPARQLEvaluation[f][SPARQLVariable["x"]]]Multiple arguments can be given:
{
<|"x" -> 1, "y" -> 20|>,
<|"x" -> 1, "y" -> 40|>,
<|"x" -> 2, "y" -> 80|>
}//SPARQLAggregate["agg" -> SPARQLEvaluation[f][SPARQLVariable["x"], SPARQLVariable["y"]]]Applications (1)
Needs["GraphStore`"]Compute the mean and the weighted mean of some data:
data = {<|"x" -> 1, "w" -> 1|>, <|"x" -> 2, "w" -> 10|>, <|"x" -> 3, "w" -> 100|>};data//SPARQLAggregate[{
"mean" -> SPARQLEvaluation["AVG"][SPARQLVariable["x"]],
"weightedMean" -> SPARQLEvaluation[Transpose /* Apply[WeightedData] /* Mean][SPARQLVariable["x"], SPARQLVariable["w"]]
}]Related Guides
Text
Wolfram Research (2019), SPARQLAggregate, Wolfram Language function, https://reference.wolfram.com/language/GraphStore/ref/SPARQLAggregate.html.
CMS
Wolfram Language. 2019. "SPARQLAggregate." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/GraphStore/ref/SPARQLAggregate.html.
APA
Wolfram Language. (2019). SPARQLAggregate. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/GraphStore/ref/SPARQLAggregate.html
BibTeX
@misc{reference.wolfram_2026_sparqlaggregate, author="Wolfram Research", title="{SPARQLAggregate}", year="2019", howpublished="\url{https://reference.wolfram.com/language/GraphStore/ref/SPARQLAggregate.html}", note=[Accessed: 13-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_sparqlaggregate, organization={Wolfram Research}, title={SPARQLAggregate}, year={2019}, url={https://reference.wolfram.com/language/GraphStore/ref/SPARQLAggregate.html}, note=[Accessed: 13-June-2026]}