AVG

The AVG function calculates the average (arithmetic mean) value for a numeric expression.

AVG Syntax

Use the following syntax when incorporating the AVG function in queries:

AVG(expression)

Where expression evaluates to a numeric value.

Note: A GROUP BY statement is required for queries that contain AVG functions if the results clause lists non-aggregate variables. Include all non-aggregated variables in the GROUP BY statement.

AVG Examples

The example queries in this section run against the AnzoGraph sample Tickit data set, which captures sales activity for a fictional Tickit website where people buy and sell tickets for sporting events, shows, and concerts. You can load and explore this data set. For more information, see Working with the Tickit Data.

The following example queries the sample Tickit data set to determine the average number of seats in the venues in each state. Since the results clause contains a non-aggregated variable (?state), a GROUP BY clause is required for grouping on ?state.

SELECT ?state (ROUND(AVG(?seats)) AS ?avg_seats)
FROM <tickit>
WHERE {
  ?s <venuestate> ?state .
  ?s <venueseats> ?seats .
}
GROUP BY ?state
ORDER BY ?state
state | avg_seats
------+--------------
CA    | 50309.000000
CO    | 63285.000000
DC    | 41888.000000
FL    | 62603.000000
GA    | 60620.000000
IL    | 48245.000000
IN    | 63000.000000
LA    | 72000.000000
MA    | 54342.000000
MD    | 70229.000000
MI    | 53391.000000
...

The query below calculates the average total price for all of the listings in the sample Tickit data set:

SELECT (AVG(?numtickets*?priceperticket) AS ?avg_total_price)
FROM <tickit>
WHERE {
  ?listing <priceperticket> ?priceperticket . 
  ?listing <numtickets> ?numtickets .
}
avg_total_price
-----------------
3034.417913
1 rows