SUM

The SUM function adds the values in the specified expression.

SUM Syntax

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

SUM(expression)

Where expression evaluates to a numeric value.

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

SUM 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 most unpopular events by returning the 10 events with the least number of ticket sales. The query uses the SUM aggregate function to calculate the total tickets for each event.

SELECT ?event ?category (SUM(?qty) AS ?total_tickets)
FROM <tickit>
WHERE {
  ?sales <qtysold> ?qty .
  ?sales <eventid> ?eventid .
  ?eventid <eventname> ?event .
  ?eventid <catid> ?catid .
  ?catid <catname> ?category .
} 
GROUP BY ?event ?category
ORDER BY ?total_tickets
LIMIT 10
event           | category | total_tickets
----------------+----------+---------------
White Christmas | Musicals |            35
Joshua Radin    | Pop      |            75
Martina McBride | Pop      |           101
Beach Boys      | Pop      |           112
Linda Ronstadt  | Pop      |           116
Teena Marie     | Pop      |           124
Indigo Girls    | Pop      |           125
Billy Idol      | Pop      |           141
Mogwai          | Pop      |           146
Stephenie Meyer | Pop      |           151
10 rows