Aggregate Clause

From RDF Stream Processing Community Group

The AGGREGATE clause is supported in C-SPARQL and the group discussed whether to support it in RSP. However, the group concluded on Jan 31th 2015 not to support this instance of syntactic sugar (for now) because it can be expressed using subqueries.

Purpose

The AGGREGATE clause suports maintaining several aggregates in one query (e.g. supporting more than one groupings which are usually global and unique for an entire query).

Example

The LarKC Deliverable D9.7, Section 3.2.6. shows the following example of an AGGREGATE clause with an equivalent rewriting using only subqueries:

Example AGGREGATE clause:

SELECT ?name ?book ?numberOfBooks ?numberOfAuthors  
WHERE { 
     ?auth :name ?name . 
     ?auth :wrote ?book .  
}  
AGGREGATE { (?numberOfBooks, COUNT, {?auth} ) }  
AGGREGATE { (?numberOfAuthors, COUNT, {?book} ) } 

Rewriting using SQL subqueries:

SELECT ?name ?surname ?book ?numberOfBooks ?numberOfAuthors  
WHERE { 
      ?auth :hasSurname ?surname . 
      ?auth :hasName ?name . 
      { 
              SELECT ?auth (COUNT(?book) AS ?numberOfBooks) 
              WHERE { 
                      ?auth :wrote ?book . 
              } 
              GROUP BY ?auth 
      } 
      { 
              SELECT  ?auth (COUNT(?auth) AS ?numberOfAuthors) 
              WHERE { 
                     ?auth :wrote ?book . 
              } 
              GROUP BY ?book 
      }  
}