WOLFRAM

Enable JavaScript to interact with content and submit forms on Wolfram websites. Learn how
Wolfram Language & System Documentation Center

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
Details and Options Details and Options
Examples  
Basic Examples  
Scope  
Generalizations & Extensions  
Applications  
See Also
Related Guides
Cite this Page

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 all

Basic Examples  (1)

Wolfram Language code: Needs["GraphStore`"]

Data about fruits:

Wolfram Language code: 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[]];

The vocabulary:

Wolfram Language code: 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];

Count the fruits:

Wolfram Language code: fruitData//SPARQLQuery[SPARQLSelect[ RDFTriple[SPARQLVariable["fruit"], rdf["type"], ex["Fruit"]] ] /* SPARQLAggregate["count" -> SPARQLEvaluation["count"][]]]

Find the number of fruits per color:

Wolfram Language code: 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)

Wolfram Language code: Needs["GraphStore`"]

Compute the total of the values associated with key "x":

Wolfram Language code: {<|"x" -> 1|>, <|"x" -> 2|>}//SPARQLAggregate[ "total" -> SPARQLEvaluation["sum"][SPARQLVariable["x"]] ]

Aggregate after grouping by the value of key "y":

Wolfram Language code: data = { <|"x" -> 1, "y" -> "a"|>, <|"x" -> 5, "y" -> "a"|>, <|"x" -> 100, "y" -> "b"|>, <|"x" -> 50, "y" -> "c"|>, <|"x" -> 80, "y" -> "c"|> };
Wolfram Language code: data//SPARQLAggregate[ {"y", "total" -> SPARQLEvaluation["sum"][SPARQLVariable["x"]]}, SPARQLVariable["y"] ]

Filter groups by the mean of the values of key "x":

Wolfram Language code: 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:

Wolfram Language code: data//SPARQLAggregate[ "avg" -> SPARQLEvaluation["avg"][SPARQLVariable["x"]], SPARQLVariable["y"], SPARQLEvaluation["avg"][SPARQLVariable["x"]] > 20, SPARQLEvaluation["sample"][SPARQLVariable["y"]] -> "Descending" ]
Wolfram Language code: Needs["GraphStore`"]

Data about works and composers:

Wolfram Language code: musicGraph = ImportString[" prefix : <http://example.org/> :LaCumparsita :composer :GerardoMatosRodriguez. :LaMorocha :composer :EnriqueSaborido. :Felicia :composer :EnriqueSaborido. ", "Turtle"]

Count the number of works by composer:

Wolfram Language code: ex[s_] := URL["http://example.org/" <> s];
Wolfram Language code: 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:

Wolfram Language code: musicGraph//SPARQLQuery[" prefix ex: <http://example.org/> select (count(*) as ?count) ?composer where { ?work ex:composer ?composer . } group by ?composer order by desc(?count) "]
Wolfram Language code: Needs["GraphStore`"]

Specify a graph which contains multiple values per entity for property p1:

Wolfram Language code: ex[s_] := URL["http://example.org/" <> s];
Wolfram Language code: 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:

Wolfram Language code: 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"}] ]
Wolfram Language code: Needs["GraphStore`"]

Specify a separator for the "GROUP_CONCAT" aggregate:

Wolfram Language code: {<|"x" -> "a"|>, <|"x" -> "b"|>, <|"x" -> "c"|>, <|"x" -> "d"|>}//SPARQLAggregate["s" -> SPARQLEvaluation["GROUP_CONCAT"][SPARQLVariable["x"], "Separator" -> "~"]]

Generalizations & Extensions  (1)

Wolfram Language code: Needs["GraphStore`"]

The aggregate can be an arbitrary function:

Wolfram Language code: {<|"x" -> 1|>, <|"x" -> 20|>, <|"x" -> 300|>}//SPARQLAggregate["agg" -> SPARQLEvaluation[f][SPARQLVariable["x"]]]

Multiple arguments can be given:

Wolfram Language code: { <|"x" -> 1, "y" -> 20|>, <|"x" -> 1, "y" -> 40|>, <|"x" -> 2, "y" -> 80|> }//SPARQLAggregate["agg" -> SPARQLEvaluation[f][SPARQLVariable["x"], SPARQLVariable["y"]]]

Applications  (1)

Wolfram Language code: Needs["GraphStore`"]

Compute the mean and the weighted mean of some data:

Wolfram Language code: data = {<|"x" -> 1, "w" -> 1|>, <|"x" -> 2, "w" -> 10|>, <|"x" -> 3, "w" -> 100|>};
Wolfram Language code: data//SPARQLAggregate[{ "mean" -> SPARQLEvaluation["AVG"][SPARQLVariable["x"]], "weightedMean" -> SPARQLEvaluation[Transpose /* Apply[WeightedData] /* Mean][SPARQLVariable["x"], SPARQLVariable["w"]] }]
Wolfram Research (2019), SPARQLAggregate, Wolfram Language function, https://reference.wolfram.com/language/GraphStore/ref/SPARQLAggregate.html.

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: 20-August-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: 20-August-2026]}

Top [フレーム]

AltStyle によって変換されたページ (->オリジナル) /