← every query · class 09 · queries/aggregation.rq

Products per manufacturer, and their average price

The same answer in both regimes: this query asks only for what was written.

?manufacturer?total?average
manufacturer:9"3"^^xsd:integer"33.23333333333333333333333333"^^xsd:decimal
manufacturer:12"2"^^xsd:integer"130.50"^^xsd:decimal

The query

# Aggregation: how many products per manufacturer, and at what average price
# (slide "Aggregation and paths"). The HAVING threshold is set by the graph:
# with five products, HAVING (COUNT(?p) > 5) would return nothing, so here — and
# on the slide — the threshold is 1.
PREFIX schema: <https://schema.org/>
PREFIX onto:   <https://data.csoares.me/onto#>

# (COUNT(?p) AS ?total) computes a value per group and names it ?total.
SELECT ?manufacturer (COUNT(?p) AS ?total) (AVG(?price) AS ?average)
WHERE {
  # the "/" is a sequence path: follow schema:offers, then schema:price.
  ?p onto:manufacturedBy ?manufacturer ;
     schema:offers/schema:price ?price .
}
# one group per manufacturer; COUNT and AVG are computed inside each group
GROUP BY ?manufacturer
# HAVING is a FILTER applied to the groups, after they are formed
HAVING (COUNT(?p) > 1)
ORDER BY DESC(?total)

Edit it in the query box →