AVG

The AVG or average function computes the average 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 Average Order Total Across All Orders

SELECT
    AVG(oe.orders.order_total) AS average_order_total
FROM
    oe.orders;
average_order_total
34933.85429

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

COMPUTE THE AVERAGE ORDER TOTAL FOR EACH CUSTOMER

SELECT
    oe.orders.customer_id,
    AVG(oe.orders.order_total) AS average_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 average order total is computed.

COMPUTE THE AVERAGE TIME ELAPSED BETWEEN EACH ORDER

-- Using an analytic/window function to solve.
WITH time_elapsed AS (
    SELECT
    	(LEAD(CAST(oe.orders.order_date AS DATE), 1) OVER (
    		ORDER BY oe.orders.order_date
        ) - CAST(oe.orders.order_date AS DATE)
    	) AS amount
    FROM
    	oe.orders
)
SELECT
    AVG(time_elapsed.amount) AS average_time_elapsed
FROM
    time_elapsed;

-- Using an analytic/window function with join logic to solve.
WITH order_duration_setup AS (
    SELECT
    	CAST(oe.orders.order_date AS DATE) AS order_date,
    	ROW_NUMBER() OVER (
    		ORDER BY oe.orders.order_date
    	) AS order_sequence
    FROM
    	oe.orders
)
SELECT
    AVG(duration_end.order_date
    	- duration_start.order_date
    ) AS average_time_elapsed
FROM
    order_duration_setup duration_start
    LEFT JOIN
    order_duration_setup duration_end
    	ON duration_start.order_sequence = duration_end.order_sequence - 1;
average_time_elapsed
15.33456497061965811965811965811965811969

Each solution above returns a one-by-one result set containing the average time elapsed between each order (15.33 days).

In the first solution, the analytic/window function, LEAD, is used to compute the difference between the order date value in the next row and the order date value in the current row when the order date values are sorted from earliest to most recent. The average of these individual differences is ultimately computed.

In the second solution, the analytic/window function, ROW_NUMBER, is first used to create a column containing a series of sequential integers. The integer value within each row corresponds to the order for which the orders were placed (i.e., based on the order date). These integer values are then used to ensure that each individual difference is computed using subsequent order date values. This is done by using a LEFT JOIN with the ON clause using the integer values to place the subsequent order date values within the same row. In short, the order date values for order 1 and order 2 will be placed in the same row. Order date values 2 and 3 in the same row and so forth. See the table below for clarification on how the result set would appear after the LEFT JOIN occurs and prior to computing the individual differences. My intent in displaying the table below is to show the result of the LEFT JOIN operation that allows the difference between subsequent order date values to be computed within each row. In the end, the average of each these individual differences is computed. It’s important to note that the additional complexity of ROW_NUMBER can be removed if the order date sequence can be inferred by the order ID of each order. That is, if the order ID values are ordered in ascending order, then the order date values would naturally be ordered as well. In this case, the ON clause would utilize the order ID value instead of our derived order date sequence values.

start_datecurrent_orderend_datenext_order
20-MAR-04 04.18.21.862632 PM108-JAN-06 08.19.44.123456 PM2
08-JAN-06 08.19.44.123456 PM208-JAN-06 09.34.13.112233 PM3
08-JAN-06 09.34.13.112233 PM326-JAN-06 09.22.51.962632 AM4

Compute the average TIME ELAPSED between each CUSTOMER’s ORDERS

-- Using an analytic/window function to solve.
WITH time_elapsed AS (
    SELECT
    	oe.orders.customer_id,
    	oe.orders.order_date,
    	(LEAD(CAST(oe.orders.order_date AS DATE), 1) OVER (
    		PARTITION BY oe.orders.customer_id
    		ORDER BY oe.orders.order_date
        ) - CAST(oe.orders.order_date AS DATE)
    	) AS amount
    FROM
    	oe.orders
)
SELECT
    time_elapsed.customer_id,
    AVG(time_elapsed.amount) AS average_time_elapsed
FROM
    time_elapsed
GROUP BY
    time_elapsed.customer_id;

-- Using an analytic/window function with join logic to solve.
WITH order_duration_setup AS (
    SELECT
    	oe.orders.customer_id,
    	CAST(oe.orders.order_date AS DATE) AS order_date,
    	DENSE_RANK() OVER (
    		PARTITION BY oe.orders.customer_id
    		ORDER BY oe.orders.order_date
    	) AS order_sequence
    FROM
    	oe.orders
)
SELECT
    duration_start.customer_id,
    AVG(duration_end.order_date
    	- duration_start.order_date
    ) AS average_time_elapsed
FROM
    order_duration_setup duration_start
    LEFT JOIN
    order_duration_setup duration_end
        ON duration_start.customer_id = duration_end.customer_id
    	AND duration_start.order_sequence = duration_end.order_sequence - 1
GROUP BY
    duration_start.customer_id;
customer_idaverage_time_elapsed
101115.2418904
102143.7728781
103184.058966
104300.123966
105103.8827585
106531.5982523
107158.3332253
108224.5728279
109111.6594174
11645.37932099
117199.0622145
118168.9078704
119
120
121
122
123
141
142
143
144164.1952112
14582.19456308
14690.6041956
147168.6040365
148147.2241927
149118.3202749
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170

Computing the average time elapsed between each order for each customer requires that we simply build onto our previous solution. In both solutions, a PARTITION BY clause is added to ensure each individual difference between subsequent order date values is calculated using the same customer (e.g., difference is computed using the first and second order date values for customer ID 101). I find that as the solution complexity increases, it becomes increasingly important to understand the intermediate result sets or steps returned or taken to fully understand the means to produce the final result set. See the intermediate result sets below (one for each solution), highlighting the appearance of the data prior to aggregation (i.e., before the average time elapsed between subsequent orders is computed for each customer). Additionally, consider taking a further step back by executing the SQL statements within the common table expression for each solution by itself and ensure the output of these statements are understood.

customer_idorder_dateamount
10116-AUG-07 03.34.12.234359 PM46.61416667
10102-OCT-07 06.18.36.663332 AM179.3024074
10129-MAR-08 01.34.04.525934 PM119.8090972
10127-JUL-08 08.59.10.223344 AM
10214-SEP-06 07.03.04.763452 AM196.3052778
10229-MAR-07 02.22.40.536996 PM168.8131944
10214-SEP-07 09.53.40.223345 AM66.20016204
10219-NOV-07 02.41.54.696211 PM
10329-MAR-06 01.34.50.545196 PM155.8086227
10301-SEP-06 08.59.15.826132 AM377.0553819
10313-SEP-07 10.19.00.654279 AM19.31289352
10302-OCT-07 05.49.34.678340 PM
10426-JAN-06 09.22.51.962632 AM427.3045023
10429-MAR-07 04.41.20.945676 PM155.7167361
10401-SEP-07 09.53.26.934626 AM317.3506597
10414-JUL-08 06.18.23.234567 PM
10520-MAR-07 05.49.10.974352 PM163.6878125
10531-AUG-07 10.19.37.811132 AM130.2802662
10508-JAN-08 05.03.12.654278 PM17.68019676
10526-JAN-08 09.22.41.934562 AM
10620-MAR-04 04.18.21.862632 PM659.2193519
10608-JAN-06 09.34.13.112233 PM856.9756366
10614-MAY-08 08.59.08.843679 PM78.59976852
10601-AUG-08 11.22.48.734526 AM
10727-JUL-06 12.22.59.662632 PM236.2781597
10720-MAR-07 07.03.32.764632 PM164.1177546
10731-AUG-07 09.53.06.008765 PM74.60376157
10714-NOV-07 12.22.31.223344 PM
10808-JAN-06 08.19.44.123456 PM199.7184259
10827-JUL-06 01.34.16.562632 PM229.3093403
10813-MAR-07 08.59.43.666320 PM244.6907176
10813-NOV-07 01.34.21.986210 PM
10912-MAR-07 09.53.54.562432 PM136.6864931
10927-JUL-07 02.22.27.462632 PM109.0129977
10913-NOV-07 02.41.10.619477 PM89.27876157
10910-FEB-08 09.22.35.564789 PM
11626-JUN-07 11.22.54.009932 PM67.28831019
11602-SEP-07 06.18.04.378034 AM32.64965278
11604-OCT-07 09.53.34.362632 PM36.2
11610-NOV-07 02.41.34.463567 AM
11707-NOV-06 07.53.25.989889 PM261.465081
11727-JUL-07 07.03.08.302945 AM105.8654745
11710-NOV-07 03.49.25.526321 AM229.856088
11727-JUN-08 12.22.11.647398 AM
11816-MAY-07 01.34.56.113356 AM168.9078704
11831-OCT-07 11.22.16.162632 PM
11927-FEB-07 12.22.33.356789 AM
12027-FEB-08 01.34.51.220065 AM
12127-FEB-08 02.41.45.109654 AM
12226-FEB-07 03.49.50.459233 AM
12307-JUN-07 06.18.08.883310 AM
14107-JUN-07 07.03.01.001100 AM
14224-MAY-07 08.59.10.010101 AM
14316-MAY-07 09.53.02.909090 AM
14427-JUL-06 03.34.38.362632 PM402.3251736
14402-SEP-07 11.22.53.134567 PM50.76878472
14423-OCT-07 05.49.56.346122 PM54.10415509
14416-DEC-07 08.19.55.462332 PM149.5827315
14414-MAY-08 10.19.03.828321 AM
14518-JUN-07 04.41.49.262632 PM71.06731481
14528-AUG-07 06.18.45.942399 PM22.71905093
14520-SEP-07 11.34.11.456789 AM61.95025463
14521-NOV-07 10.22.33.362632 AM173.0416319
14512-MAY-08 11.22.30.545103 AM
14616-MAY-07 02.22.24.234567 AM28.64355324
14613-JUN-07 05.49.07.162632 PM76.05170139
14628-AUG-07 07.03.34.003399 PM84.63818287
14621-NOV-07 10.22.33.263332 AM173.0833449
14612-MAY-08 12.22.34.525972 PM
14702-FEB-06 01.34.56.345678 AM287.9162731
14716-NOV-06 11.34.22.162552 PM145.7804167
14711-APR-07 06.18.10.362632 PM139.111956
14728-AUG-07 08.59.23.144778 PM101.6075
14708-DEC-07 11.34.11.331392 AM
14817-NOV-06 12.22.11.262552 AM224.1799653
14829-JUN-07 04.41.20.098765 AM160.3203009
14806-DEC-07 12.22.34.225609 PM11.19534722
14817-DEC-07 05.03.52.562632 PM193.2011574
14827-JUN-08 09.53.32.335522 PM
14911-MAR-07 03.34.56.536966 PM185.5934491
14913-SEP-07 05.49.30.647893 AM23.63209491
14906-OCT-07 08.59.43.462632 PM34.19072917
14910-NOV-07 01.34.22.362124 AM229.8648264
14926-JUN-08 10.19.43.190089 PM
15004-JUN-07 04.41.12.554435 PM
15104-JUN-08 05.49.43.546954 PM
15218-NOV-07 04.18.50.546851 PM
15327-FEB-06 05.03.03.828330 PM
15421-JUL-07 08.59.57.571057 PM
15510-FEB-08 07.53.19.528202 PM
15602-FEB-06 08.19.11.227550 PM
15719-NOV-07 09.22.53.224175 PM
15819-NOV-07 10.22.38.340990 PM
15910-JUL-07 01.34.29.559387 AM
16010-JUL-07 02.22.53.554822 AM
16102-JUL-07 03.34.44.665170 AM
16201-JUL-07 04.49.13.615512 PM
16301-JUL-07 04.49.13.664085 PM
16401-JUL-07 04.49.13.678123 PM
16529-JUN-07 07.03.21.526005 AM
16629-JUN-07 08.59.31.333617 AM
16729-JUN-07 09.53.41.984501 AM
16824-MAY-08 10.19.51.985501 AM
16924-MAY-07 11.22.10.548639 AM
17029-MAR-06 10.22.09.509801 AM
customer_idstart_dateend_datetime_elapsed
10116-Aug-072-Oct-0746.61416667
1012-Oct-0729-Mar-08179.3024074
10129-Mar-0827-Jul-08119.8090972
10214-Sep-0629-Mar-07196.3052778
10229-Mar-0714-Sep-07168.8131944
10214-Sep-0719-Nov-0766.20016204
10329-Mar-061-Sep-06155.8086227
1031-Sep-0613-Sep-07377.0553819
10313-Sep-072-Oct-0719.31289352
10426-Jan-0629-Mar-07427.3045023
10429-Mar-071-Sep-07155.7167361
1041-Sep-0714-Jul-08317.3506597
10520-Mar-0731-Aug-07163.6878125
10531-Aug-078-Jan-08130.2802662
1058-Jan-0826-Jan-0817.68019676
10620-Mar-048-Jan-06659.2193519
1068-Jan-0614-May-08856.9756366
10614-May-081-Aug-0878.59976852
10727-Jul-0620-Mar-07236.2781597
10720-Mar-0731-Aug-07164.1177546
10731-Aug-0714-Nov-0774.60376157
1088-Jan-0627-Jul-06199.7184259
10827-Jul-0613-Mar-07229.3093403
10813-Mar-0713-Nov-07244.6907176
10912-Mar-0727-Jul-07136.6864931
10927-Jul-0713-Nov-07109.0129977
10913-Nov-0710-Feb-0889.27876157
11626-Jun-072-Sep-0767.28831019
1162-Sep-074-Oct-0732.64965278
1164-Oct-0710-Nov-0736.2
1177-Nov-0627-Jul-07261.465081
11727-Jul-0710-Nov-07105.8654745
11710-Nov-0727-Jun-08229.856088
11816-May-0731-Oct-07168.9078704
14427-Jul-062-Sep-07402.3251736
1442-Sep-0723-Oct-0750.76878472
14423-Oct-0716-Dec-0754.10415509
14416-Dec-0714-May-08149.5827315
14518-Jun-0728-Aug-0771.06731481
14528-Aug-0720-Sep-0722.71905093
14520-Sep-0721-Nov-0761.95025463
14521-Nov-0712-May-08173.0416319
14616-May-0713-Jun-0728.64355324
14613-Jun-0728-Aug-0776.05170139
14628-Aug-0721-Nov-0784.63818287
14621-Nov-0712-May-08173.0833449
1472-Feb-0616-Nov-06287.9162731
14716-Nov-0611-Apr-07145.7804167
14711-Apr-0728-Aug-07139.111956
14728-Aug-078-Dec-07101.6075
14817-Nov-0629-Jun-07224.1799653
14829-Jun-076-Dec-07160.3203009
1486-Dec-0717-Dec-0711.19534722
14817-Dec-0727-Jun-08193.2011574
14911-Mar-0713-Sep-07185.5934491
14913-Sep-076-Oct-0723.63209491
1496-Oct-0710-Nov-0734.19072917
14910-Nov-0726-Jun-08229.8648264
15218-Nov-07
1562-Feb-06
1514-Jun-08
14827-Jun-08
10127-Jul-08
1237-Jun-07
10813-Nov-07
15819-Nov-07
1612-Jul-07
16629-Jun-07
16824-May-08
16529-Jun-07
14512-May-08
16924-May-07
11610-Nov-07
15719-Nov-07
10219-Nov-07
1032-Oct-07
11831-Oct-07
10526-Jan-08
15327-Feb-06
15421-Jul-07
1061-Aug-08
12027-Feb-08
1504-Jun-07
10714-Nov-07
17029-Mar-06
1478-Dec-07
14926-Jun-08
11727-Jun-08
12226-Feb-07
1417-Jun-07
15510-Feb-08
16010-Jul-07
1641-Jul-07
10414-Jul-08
14224-May-07
14414-May-08
10910-Feb-08
1631-Jul-07
16729-Jun-07
14316-May-07
11927-Feb-07
14612-May-08
12127-Feb-08
15910-Jul-07
1621-Jul-07

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.