This Online Tech Support page is based on examples to be easier to follow. The Oracle Sum Function sums the numbers and returns a result. The Sum function take account all not null values and ignores the null values. The Oracle SUM syntax of the aggregate function is following:

SUM(ALL <number>)

SUM(DISTINCT <number>)

SUM(<number>)

And the next syntax is SUM analytic function. This version does calculation on-fly and doesn’t need Group By clause.

SUM(ALL <number>) OVER ([<analytic_clause>])

SUM(DISTINCT <number>) OVER ([<analytic_clause>])

SUM(<number>) OVER ([<analytic_clause>])

The first example is the most simple SUM and there is only one row is number “**1**” and since there is no more numbers the output returns number “**1**“. The function can be quite simple without extra columns you don’t need the GROUP BY clause.

SELECT SUM(1) FROM DUAL;

The next Select statement will show us what happens when the query didn’t return any row. Take a look at the following SQL query there **WHERE** statement has restriction “**1 = 2**” and this equation will never be true so the query wouldn’t return a row. The query output return “**(null)**” because no numbers were to sum but Oracle SUM *never returns* an error **NO_DATA_FOUND** either Oracle COUNT, or MIN, or MAX and any other aggregate function.

SELECT SUM(1) FROM DUAL WHERE 1 = 2;

To not have any lines returned you are still able to do some calculation with the NULL value you have to only replace it using Oracle NVL or NVL2 functions with any option you may need and your function wouldn’t fail because of the NULL value. The following example has the NVL function replacing the **NULL** value with **zero** (**0**) and the SQL output has number “**0**” for no rows.

SELECT NVL(SUM(1),0) FROM DUAL WHERE 1 = 2;

To continue with more complex example we would need some data and to avoid making this examples to complicated we will use the Oracle **DUAL** table and the **CONNECT BY** clause to generate **10** lines.

SELECT ROWNUM FROM DUAL CONNECT BY ROWNUM < 11;

The sixth SQL query will sum all 10 rows above and the returned number is “**55**” (**1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 = 55**). The result has been returned on one line as you can see on the following picture.

SELECT SUM(ROWNUM) FROM DUAL CONNECT BY ROWNUM < 11;

The same query above is done using Oracle SUM analytic function in above and to make the purpose more clear we did add the Oracle ROWNUM column as the first one. The “**55**” sum value is repeating over all rows.

SELECT ROWNUM, SUM(ROWNUM) OVER () FROM DUAL CONNECT BY ROWNUM < 11;

To make the analytic function more complex we did apply clause “**order by rownum asc**” on the function and the result become now calculating the sum per line. To get the second column value add to the first column the previous row second column number and it makes the second column’s value. For example the 4th row has first column’s value “**4**” and add on it the 3rd’s row second column value what is “**6**“. The **4 + 6 = 10** and this is the 4th row second column’s number. The last value is “**55**” as on the last examples. This way written query let’s you to follow up the calculation when it’s very long one and not as this example or you can use this query to make your report look more detailed.

SELECT ROWNUM, SUM(ROWNUM) OVER (order by rownum asc) FROM DUAL CONNECT BY ROWNUM < 11;

You can use the analytic function to make the calculation appear reverse way as with clause “**ORDER BY rownum DESC**“. The rows are starting on the second column with “**55**” and the lowest row has the last number “**10**” left on it.

SELECT ROWNUM, SUM(ROWNUM) OVER (ORDER BY rownum DESC) FROM DUAL CONNECT BY ROWNUM < 11 ORDER BY 1 ASC;

**Important To Know:**

Let’s try the same example above with Oracle SUM aggregate function and as you see on the following output instead of value “**55**” we got a error “**ORA-00937: not a single-group group function**“. The reason is that all columns that are not using any aggregate function needs to be added into **GROUP BY** clause or the SQL statement will fail.

SELECT ROWNUM, SUM(ROWNUM) FROM DUAL CONNECT BY ROWNUM < 11;

The next query is almost the same as the last one with only one exception we do have the **ROWNUM** function in the **GROUP BY** clause and the output is following:

SELECT ROWNUM, SUM(ROWNUM) FROM DUAL CONNECT BY ROWNUM < 11 GROUP BY ROWNUM;

The query output didn’t make any sum because the **ROWNUM** value per line is unique and it didn’t create any value groups. To get the **ROWNUM** value summarised take a look at the following SQL query. There we are suing a **DUMMY** column which comes along with the **Oracle Dual** table. The only column that the table has. The query output character “**X**” in dummy column and the column has added to the **GROUP BY** clause and the sum is “**55**“.

SELECT DUMMY, SUM(ROWNUM) FROM DUAL CONNECT BY ROWNUM < 11 GROUP BY DUMMY;

To see what has the Oracle DUAL table we did add the following query so you can try it by yourself and the picture with the query result.

SELECT * FROM DUAL;

The star (“*”) character returns all column from the Dual table and we don’t have any restriction on the query so there isn’t any WHERE keyword with a statement. The SQL query returned one line with one column **DUMMY** and it has value “**X**” in it. Since there is no more values beside this “**X**” the last SUM example returned **only 1 row** with sum “**55**“.

**See Also:**

Oracle Select Oracle ROWNUM Oracle NVL Oracle NVL2 Online Tech Support Home