SUM

The SUM function computes the SUM based on a group of values. For the examples that follow, please reference the orders table within the order entry (oe) schema at livesql.oracle.com. The contents of this table can also be found below. This table contains one row for each order placed by a customer.

order_idorder_dateorder_modecustomer_idorder_statusorder_totalsales_rep_idpromotion_id
245816-AUG-07 03.34.12.234359 PMdirect101078279.6153–
239719-NOV-07 02.41.54.696211 PMdirect102142283.2154–
245402-OCT-07 05.49.34.678340 PMdirect10316653.4154–
235414-JUL-08 06.18.23.234567 PMdirect104046257155–
235808-JAN-08 05.03.12.654278 PMdirect10527826155–
238114-MAY-08 08.59.08.843679 PMdirect106323034.6156–
244031-AUG-07 09.53.06.008765 PMdirect107370576.9156–
235708-JAN-06 08.19.44.123456 PMdirect108559872.4158–
239410-FEB-08 09.22.35.564789 PMdirect109521863158–
243502-SEP-07 11.22.53.134567 PMdirect144662303159–
245520-SEP-07 11.34.11.456789 AMdirect145714087.5160–
237916-MAY-07 02.22.24.234567 AMdirect146817848.2161–
239602-FEB-06 01.34.56.345678 AMdirect147834930161–
240629-JUN-07 04.41.20.098765 AMdirect14882854.2161–
243413-SEP-07 05.49.30.647893 AMdirect1498268651.8161–
243602-SEP-07 06.18.04.378034 AMdirect11686394.8161–
244627-JUL-07 07.03.08.302945 AMdirect1178103679.3161–
244727-JUL-08 08.59.10.223344 AMdirect101833893.6161–
243214-SEP-07 09.53.40.223345 AMdirect1021010523163–
243313-SEP-07 10.19.00.654279 AMdirect1031078163–
235526-JAN-06 09.22.51.962632 AMonline104894513.5––
235626-JAN-08 09.22.41.934562 AMonline105529473.8––
235908-JAN-06 09.34.13.112233 PMonline10695543.1––
236014-NOV-07 12.22.31.223344 PMonline1074990.4––
236113-NOV-07 01.34.21.986210 PMonline1088120131.3––
236213-NOV-07 02.41.10.619477 PMonline109492829.4––
236323-OCT-07 05.49.56.346122 PMonline144010082.3––
236428-AUG-07 06.18.45.942399 PMonline14549500––
236528-AUG-07 07.03.34.003399 PMonline146927455.3––
236628-AUG-07 08.59.23.144778 PMonline147537319.4––
236727-JUN-08 09.53.32.335522 PMonline14810144054.8––
236826-JUN-08 10.19.43.190089 PMonline1491060065––
236926-JUN-07 11.22.54.009932 PMonline116011097.4––
237027-JUN-08 12.22.11.647398 AMonline1174126––
237116-MAY-07 01.34.56.113356 AMonline118679405.6––
237227-FEB-07 12.22.33.356789 AMonline119916447.2––
237327-FEB-08 01.34.51.220065 AMonline1204416––
237427-FEB-08 02.41.45.109654 AMonline12104797––
237526-FEB-07 03.49.50.459233 AMonline1222103834.4––
237607-JUN-07 06.18.08.883310 AMonline123611006.2––
237707-JUN-07 07.03.01.001100 AMonline141538017.8––
237824-MAY-07 08.59.10.010101 AMonline142525691.3––
238016-MAY-07 09.53.02.909090 AMonline143327132.6––
238214-MAY-08 10.19.03.828321 AMonline144871173––
238312-MAY-08 11.22.30.545103 AMonline145836374.7––
238412-MAY-08 12.22.34.525972 PMonline146329249.1––
238508-DEC-07 11.34.11.331392 AMonline1474295892––
238606-DEC-07 12.22.34.225609 PMonline1481021116.9––
238711-MAR-07 03.34.56.536966 PMonline149552758.9––
238804-JUN-07 04.41.12.554435 PMonline1504282694.3––
238904-JUN-08 05.49.43.546954 PMonline151417620––
239018-NOV-07 04.18.50.546851 PMonline15297616.8––
239127-FEB-06 05.03.03.828330 PMdirect153248070.6156–
239221-JUL-07 08.59.57.571057 PMdirect154926632161–
239310-FEB-08 07.53.19.528202 PMdirect155423431.9161–
239502-FEB-06 08.19.11.227550 PMdirect156368501163–
239819-NOV-07 09.22.53.224175 PMdirect15797110.3163–
239919-NOV-07 10.22.38.340990 PMdirect158025270.3161–
240010-JUL-07 01.34.29.559387 AMdirect159269286.4161–
240110-JUL-07 02.22.53.554822 AMdirect1603969.2163–
240202-JUL-07 03.34.44.665170 AMdirect1618600154–
240301-JUL-07 04.49.13.615512 PMdirect1620220154–
240401-JUL-07 04.49.13.664085 PMdirect1636510158–
240501-JUL-07 04.49.13.678123 PMdirect16451233159–
240729-JUN-07 07.03.21.526005 AMdirect16592519155–
240829-JUN-07 08.59.31.333617 AMdirect1661309158–
240929-JUN-07 09.53.41.984501 AMdirect167248154–
241024-MAY-08 10.19.51.985501 AMdirect168645175156–
241124-MAY-07 11.22.10.548639 AMdirect169815760.5156–
241229-MAR-06 10.22.09.509801 AMdirect170966816158–
241329-MAR-08 01.34.04.525934 PMdirect101548552161–
241429-MAR-07 02.22.40.536996 PMdirect102810794.6153–
241529-MAR-06 01.34.50.545196 PMdirect1036310161–
241629-MAR-07 04.41.20.945676 PMdirect1046384160–
241720-MAR-07 05.49.10.974352 PMdirect10551926.6163–
241820-MAR-04 04.18.21.862632 PMdirect10645546.6163–
241920-MAR-07 07.03.32.764632 PMdirect107331574160–
242013-MAR-07 08.59.43.666320 PMdirect108229750160–
242112-MAR-07 09.53.54.562432 PMdirect109172836––
242216-DEC-07 08.19.55.462332 PMdirect144211188.5153–
242321-NOV-07 10.22.33.362632 AMdirect145310367.7160–
242421-NOV-07 10.22.33.263332 AMdirect146413824153–
242516-NOV-06 11.34.22.162552 PMdirect14751500.8163–
242617-NOV-06 12.22.11.262552 AMdirect14867200––
242710-NOV-07 01.34.22.362124 AMdirect14979055163–
242810-NOV-07 02.41.34.463567 AMdirect116814685.8––
242910-NOV-07 03.49.25.526321 AMdirect117950125154–
243002-OCT-07 06.18.36.663332 AMdirect101829669.9159–
243114-SEP-06 07.03.04.763452 AMdirect10215610.6163–
243701-SEP-06 08.59.15.826132 AMdirect103413550163–
243801-SEP-07 09.53.26.934626 AMdirect10405451154–
243931-AUG-07 10.19.37.811132 AMdirect105122150.1159–
244101-AUG-08 11.22.48.734526 AMdirect10652075.2160–
244227-JUL-06 12.22.59.662632 PMdirect107952471.9154–
244327-JUL-06 01.34.16.562632 PMdirect10803646154–
244427-JUL-07 02.22.27.462632 PMdirect109177727.2155–
244527-JUL-06 03.34.38.362632 PMdirect14485537.8158–
244818-JUN-07 04.41.49.262632 PMdirect14551388158–
244913-JUN-07 05.49.07.162632 PMdirect146686155–
245011-APR-07 06.18.10.362632 PMdirect14731636159–
245117-DEC-07 05.03.52.562632 PMdirect148710474.6154–
245206-OCT-07 08.59.43.462632 PMdirect149512589159–
245304-OCT-07 09.53.34.362632 PMdirect1160129153–
245607-NOV-06 07.53.25.989889 PMdirect11703878.4163–
245731-OCT-07 11.22.16.162632 PMdirect118521586.2159–

COMPUTE THE SUM OF ORDER TOTALS ACROSS ALL ORDERS

SELECT
    SUM(oe.orders.order_total) AS sum_order_total
FROM
    oe.orders;
sum_order_total
3668054.7

In the example code above, note that a GROUP BY clause is not present because the code returns a single row representing the sum of order total values across all orders (3668054.7).

COMPUTE THE SUM OF ORDER TOTALS FOR EACH CUSTOMER

SELECT
    oe.orders.customer_id,
    SUM(oe.orders.order_total) AS sum_order_total
FROM
    oe.orders
GROUP BY
    oe.orders.customer_id;
customer_idaverage_order_total
10738903.3
10853349.925
15825270.3
161600
166309
10515344.125
10966313.9
14327132.6
15969286.4
162220
163510
16845175
10436651.375
11850495.9
14514343.58
1214797
14138017.8
15523431.9
10147598.775
1035147.85
1168076.75
120416
14225691.3
15117620
15668501
1577110.3
16915760.5
14617692.52
14837140.1
14980623.94
122103834.4
1527616.8
17066816
14432056.92
11916447.2
15348070.6
1641233
16748
10217302.85
1069049.875
14774255.64
11739452.175
12311006.2
150282694.3
15426632
160969.2
1652519

When an aggregate must be computed across and for each set of grouped rows, a GROUP BY clause is necessary. Here, the rows have been grouped by customer ID and for each of those sets of grouped rows, the sum of order totals is computed.

COMPUTE TOTAL SALES FOR EACH SALES REPRESENTATIVE AND MONTH

SELECT
    oe.orders.sales_rep_id,
    TRUNC(oe.orders.order_date, 'MONTH') AS order_month,
    SUM(oe.orders.order_total) AS order_total
FROM
    oe.orders
WHERE
    oe.orders.sales_rep_id IS NOT NULL
GROUP BY
    oe.orders.sales_rep_id,
    TRUNC(oe.orders.order_date, 'MONTH');
sales_rep_idorder_monthorder_total
1611-May-0717848.2
1611-Jul-0833893.6
1631-Jul-07969.2
1631-Nov-065379.2
1551-Jul-0777727.2
1581-Jun-071697
1541-Jun-0748
1561-May-0715760.5
1631-Mar-071926.6
1601-Aug-082075.2
1531-Aug-0778279.6
1541-Oct-076653.4
1551-Jan-087826
1611-Jun-072854.2
1611-Feb-0823431.9
1601-Nov-0710367.7
1631-Sep-0619160.6
1591-Aug-0722150.1
1531-Oct-07129
1561-Feb-0648070.6
1631-Feb-0668501
1541-Jul-07820
1531-Nov-0713824
1581-Jan-0659872.4
1611-Jul-07199597.7
1611-Nov-0725270.3
1531-Mar-0710794.6
1601-Mar-0761708
1591-Oct-0763845.1
1541-Jul-0656117.9
1541-Nov-0792408.2
1551-Jul-0846257
1611-Sep-07275046.6
1591-Jul-071233
1581-Mar-0666816
1631-Mar-045546.6
1591-Apr-071636
1561-May-0868209.6
1561-Aug-0770576.9
1581-Feb-0821863
1591-Sep-0762303
1601-Sep-0714087.5
1631-Sep-0710601
1551-Jun-072605
1611-Mar-06310
1531-Dec-0711188.5
1541-Sep-075451
1541-Dec-0710474.6
1611-Feb-0634930
1631-Nov-0716165.3
1581-Jul-07510
1611-Mar-0848552
1581-Jul-065537.8

This problem is similar to the previous one where the sum of order totals was computed for each customer ID. Instead of computing this sum for each customer ID, we’ll compute the sum of order totals for each sales representative ID and order month combination. Think of this as a means to generate a monthly report on sales representative performance. The TRUNC function, short for truncate, is used to strip each order date down to the first day of the corresponding month and year represented in the order date value (e.g., 2022/04/16 truncated to 2022/04/01). This function, and other flavors performing the same operation, are very useful for reporting and analytics purposes and worth remembering.

COMPUTE A DAILY RUNNING SUM OF ORDER TOTALS

-- Using common table expression and SUM as an analytical function.
WITH daily_order_total AS ( 
    SELECT
    	TRUNC(oe.orders.order_date) order_date,
    	SUM(oe.orders.order_total) AS order_total
    FROM
    	oe.orders
    GROUP BY
    	TRUNC(oe.orders.order_date)
)
SELECT
    daily_order_total.order_date,
    daily_order_total.order_total,
    SUM(daily_order_total.order_total) OVER (
    	ORDER BY daily_order_total.order_date
    ) AS running_sum
FROM
    daily_order_total
ORDER BY
    daily_order_total.order_date;

-- Using SUM as an analytical function with a nested SUM.
SELECT
    TRUNC(oe.orders.order_date) AS order_date,
    SUM(SUM(oe.orders.order_total)) OVER (
    	ORDER BY TRUNC(oe.orders.order_date)
    ) AS order_total
FROM
    oe.orders
GROUP BY
    TRUNC(oe.orders.order_date);
order_dateorder_totalrunning_sum
20-Mar-045546.65546.6
8-Jan-0665415.570962.1
26-Jan-0694513.5165475.6
2-Feb-06103431268906.6
27-Feb-0648070.6316977.2
29-Mar-0667126384103.2
27-Jul-0661655.7445758.9
1-Sep-0613550459308.9
14-Sep-065610.6464919.5
7-Nov-063878.4468797.9
16-Nov-061500.8470298.7
17-Nov-067200477498.7
26-Feb-07103834.4581333.1
27-Feb-0716447.2597780.3
11-Mar-0752758.9650539.2
12-Mar-0772836723375.2
13-Mar-0729750753125.2
20-Mar-0733500.6786625.8
29-Mar-0711178.6797804.4
11-Apr-071636799440.4
16-May-07124386.4923826.8
24-May-0741451.8965278.6
4-Jun-07282694.31247972.9
7-Jun-07490241296996.9
13-Jun-07861297082.9
18-Jun-0713881298470.9
26-Jun-0711097.41309568.3
29-Jun-075730.21315298.5
1-Jul-0719631317261.5
2-Jul-076001317861.5
10-Jul-0770255.61388117.1
21-Jul-07266321414749.1
27-Jul-07181406.51596155.6
16-Aug-0778279.61674435.2
28-Aug-0774274.71748709.9
31-Aug-07927271841436.9
1-Sep-0754511846887.9
2-Sep-0768697.81915585.7
13-Sep-07268729.82184315.5
14-Sep-07105232194838.5
20-Sep-0714087.52208926
2-Oct-0736323.32245249.3
4-Oct-071292245378.3
6-Oct-07125892257967.3
23-Oct-0710082.32268049.6
31-Oct-0721586.22289635.8
10-Nov-0773865.82363501.6
13-Nov-07212960.72576462.3
14-Nov-07990.42577452.7
18-Nov-077616.82585069.5
19-Nov-0774663.82659733.3
21-Nov-0724191.72683925
6-Dec-0721116.92705041.9
8-Dec-072958923000933.9
16-Dec-0711188.53012122.4
17-Dec-0710474.63022597
8-Jan-0878263030423
26-Jan-0829473.83059896.8
10-Feb-0845294.93105191.7
27-Feb-0852133110404.7
29-Mar-08485523158956.7
12-May-0865623.83224580.5
14-May-0894207.63318788.1
24-May-08451753363963.1
4-Jun-08176203381583.1
26-Jun-08600653441648.1
27-Jun-08144180.83585828.9
14-Jul-08462573632085.9
27-Jul-0833893.63665979.5
1-Aug-082075.23668054.7

While I cover the use of analytical functions more thoroughly here, I think it would be a shame to not provide a simple example here covering a common task of computing a running sum or total. In the first approach, a common table expression (CTE) is used to first sum up every order total value by order date. As you can see, there is nothing new or exciting in this step when compared to the previous problems above. The magic happens when the CTE is used to subsequently take the daily order total sums, sort the values by the corresponding truncated order date value, and output the sum of order totals for the current row’s date and those that preceded it. Keep in mind the sorting of order date values in the ORDER BY clause of the OVER clause is sorting the values for the computation only. Sorting in the OVER clause does not guarantee the result-set returned will be sorted in the specified manner, unless otherwise specified by the vendor-specific documentation. Consequently, an additional ORDER BY clause is used at the end of the query to sort the final result.

Knowledge Check
Need more practice or simply wish to test your understanding? Give a few of the practice problems I’ve provided in the SQL Practice Problems section a try.