Open Source RDBMS - Seamless, Scalable, Stable and Free

한국어 | Login |Register

Versions available for this page: CUBRID 8.2.1 |  CUBRID 8.3.0 |  CUBRID 8.3.1 |  CUBRID 8.4.0 |  CUBRID 8.4.1 |  CUBRID 8.4.3 |  CUBRID 9.0.0 | 

SUM Function

Description

The SUM function returns the sum of expressions of all rows. Only one expression is specified as a parameter. You can get the sum without duplicates by inserting the DISTINCT or UNIQUE keyword in front of the expression, or get the sum of all values by omitting the keyword or by using ALL.

Syntax

SUM ( [ { DISTINCT | DISTINCTROW } | UNIQUE | ALL ] expression )

  • expression: Specifies an expression that returns a numeric value.
  • ALL: Gets the sum for all data (default).
  • DISTINCT or UNIQUE: Gets the sum of unique values without duplicates
Example

The following example shows how to retrieve the top 10 countries and the total number of gold (gold) medals based on the sum of gold medals won in the Olympics in the demodb database.

SELECT nation_code, SUM(gold) FROM participant GROUP BY nation_code

ORDER BY SUM(gold) DESC

FOR ORDERBY_NUM() BETWEEN 1 AND 10 ;

 

=== <Result of SELECT Command in Line 1> ===

 

  nation_code             sum(gold)

===================================

  'USA'                         190

  'CHN'                          97

  'RUS'                          85

  'GER'                          79

  'URS'                          55

  'FRA'                          53

  'AUS'                          52

  'ITA'                          48

  'KOR'                          48

  'EUN'                          45