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_id | order_date | order_mode | customer_id | order_status | order_total | sales_rep_id | promotion_id |
|---|---|---|---|---|---|---|---|
| 2458 | 16-AUG-07 03.34.12.234359 PM | direct | 101 | 0 | 78279.6 | 153 | – |
| 2397 | 19-NOV-07 02.41.54.696211 PM | direct | 102 | 1 | 42283.2 | 154 | – |
| 2454 | 02-OCT-07 05.49.34.678340 PM | direct | 103 | 1 | 6653.4 | 154 | – |
| 2354 | 14-JUL-08 06.18.23.234567 PM | direct | 104 | 0 | 46257 | 155 | – |
| 2358 | 08-JAN-08 05.03.12.654278 PM | direct | 105 | 2 | 7826 | 155 | – |
| 2381 | 14-MAY-08 08.59.08.843679 PM | direct | 106 | 3 | 23034.6 | 156 | – |
| 2440 | 31-AUG-07 09.53.06.008765 PM | direct | 107 | 3 | 70576.9 | 156 | – |
| 2357 | 08-JAN-06 08.19.44.123456 PM | direct | 108 | 5 | 59872.4 | 158 | – |
| 2394 | 10-FEB-08 09.22.35.564789 PM | direct | 109 | 5 | 21863 | 158 | – |
| 2435 | 02-SEP-07 11.22.53.134567 PM | direct | 144 | 6 | 62303 | 159 | – |
| 2455 | 20-SEP-07 11.34.11.456789 AM | direct | 145 | 7 | 14087.5 | 160 | – |
| 2379 | 16-MAY-07 02.22.24.234567 AM | direct | 146 | 8 | 17848.2 | 161 | – |
| 2396 | 02-FEB-06 01.34.56.345678 AM | direct | 147 | 8 | 34930 | 161 | – |
| 2406 | 29-JUN-07 04.41.20.098765 AM | direct | 148 | 8 | 2854.2 | 161 | – |
| 2434 | 13-SEP-07 05.49.30.647893 AM | direct | 149 | 8 | 268651.8 | 161 | – |
| 2436 | 02-SEP-07 06.18.04.378034 AM | direct | 116 | 8 | 6394.8 | 161 | – |
| 2446 | 27-JUL-07 07.03.08.302945 AM | direct | 117 | 8 | 103679.3 | 161 | – |
| 2447 | 27-JUL-08 08.59.10.223344 AM | direct | 101 | 8 | 33893.6 | 161 | – |
| 2432 | 14-SEP-07 09.53.40.223345 AM | direct | 102 | 10 | 10523 | 163 | – |
| 2433 | 13-SEP-07 10.19.00.654279 AM | direct | 103 | 10 | 78 | 163 | – |
| 2355 | 26-JAN-06 09.22.51.962632 AM | online | 104 | 8 | 94513.5 | – | – |
| 2356 | 26-JAN-08 09.22.41.934562 AM | online | 105 | 5 | 29473.8 | – | – |
| 2359 | 08-JAN-06 09.34.13.112233 PM | online | 106 | 9 | 5543.1 | – | – |
| 2360 | 14-NOV-07 12.22.31.223344 PM | online | 107 | 4 | 990.4 | – | – |
| 2361 | 13-NOV-07 01.34.21.986210 PM | online | 108 | 8 | 120131.3 | – | – |
| 2362 | 13-NOV-07 02.41.10.619477 PM | online | 109 | 4 | 92829.4 | – | – |
| 2363 | 23-OCT-07 05.49.56.346122 PM | online | 144 | 0 | 10082.3 | – | – |
| 2364 | 28-AUG-07 06.18.45.942399 PM | online | 145 | 4 | 9500 | – | – |
| 2365 | 28-AUG-07 07.03.34.003399 PM | online | 146 | 9 | 27455.3 | – | – |
| 2366 | 28-AUG-07 08.59.23.144778 PM | online | 147 | 5 | 37319.4 | – | – |
| 2367 | 27-JUN-08 09.53.32.335522 PM | online | 148 | 10 | 144054.8 | – | – |
| 2368 | 26-JUN-08 10.19.43.190089 PM | online | 149 | 10 | 60065 | – | – |
| 2369 | 26-JUN-07 11.22.54.009932 PM | online | 116 | 0 | 11097.4 | – | – |
| 2370 | 27-JUN-08 12.22.11.647398 AM | online | 117 | 4 | 126 | – | – |
| 2371 | 16-MAY-07 01.34.56.113356 AM | online | 118 | 6 | 79405.6 | – | – |
| 2372 | 27-FEB-07 12.22.33.356789 AM | online | 119 | 9 | 16447.2 | – | – |
| 2373 | 27-FEB-08 01.34.51.220065 AM | online | 120 | 4 | 416 | – | – |
| 2374 | 27-FEB-08 02.41.45.109654 AM | online | 121 | 0 | 4797 | – | – |
| 2375 | 26-FEB-07 03.49.50.459233 AM | online | 122 | 2 | 103834.4 | – | – |
| 2376 | 07-JUN-07 06.18.08.883310 AM | online | 123 | 6 | 11006.2 | – | – |
| 2377 | 07-JUN-07 07.03.01.001100 AM | online | 141 | 5 | 38017.8 | – | – |
| 2378 | 24-MAY-07 08.59.10.010101 AM | online | 142 | 5 | 25691.3 | – | – |
| 2380 | 16-MAY-07 09.53.02.909090 AM | online | 143 | 3 | 27132.6 | – | – |
| 2382 | 14-MAY-08 10.19.03.828321 AM | online | 144 | 8 | 71173 | – | – |
| 2383 | 12-MAY-08 11.22.30.545103 AM | online | 145 | 8 | 36374.7 | – | – |
| 2384 | 12-MAY-08 12.22.34.525972 PM | online | 146 | 3 | 29249.1 | – | – |
| 2385 | 08-DEC-07 11.34.11.331392 AM | online | 147 | 4 | 295892 | – | – |
| 2386 | 06-DEC-07 12.22.34.225609 PM | online | 148 | 10 | 21116.9 | – | – |
| 2387 | 11-MAR-07 03.34.56.536966 PM | online | 149 | 5 | 52758.9 | – | – |
| 2388 | 04-JUN-07 04.41.12.554435 PM | online | 150 | 4 | 282694.3 | – | – |
| 2389 | 04-JUN-08 05.49.43.546954 PM | online | 151 | 4 | 17620 | – | – |
| 2390 | 18-NOV-07 04.18.50.546851 PM | online | 152 | 9 | 7616.8 | – | – |
| 2391 | 27-FEB-06 05.03.03.828330 PM | direct | 153 | 2 | 48070.6 | 156 | – |
| 2392 | 21-JUL-07 08.59.57.571057 PM | direct | 154 | 9 | 26632 | 161 | – |
| 2393 | 10-FEB-08 07.53.19.528202 PM | direct | 155 | 4 | 23431.9 | 161 | – |
| 2395 | 02-FEB-06 08.19.11.227550 PM | direct | 156 | 3 | 68501 | 163 | – |
| 2398 | 19-NOV-07 09.22.53.224175 PM | direct | 157 | 9 | 7110.3 | 163 | – |
| 2399 | 19-NOV-07 10.22.38.340990 PM | direct | 158 | 0 | 25270.3 | 161 | – |
| 2400 | 10-JUL-07 01.34.29.559387 AM | direct | 159 | 2 | 69286.4 | 161 | – |
| 2401 | 10-JUL-07 02.22.53.554822 AM | direct | 160 | 3 | 969.2 | 163 | – |
| 2402 | 02-JUL-07 03.34.44.665170 AM | direct | 161 | 8 | 600 | 154 | – |
| 2403 | 01-JUL-07 04.49.13.615512 PM | direct | 162 | 0 | 220 | 154 | – |
| 2404 | 01-JUL-07 04.49.13.664085 PM | direct | 163 | 6 | 510 | 158 | – |
| 2405 | 01-JUL-07 04.49.13.678123 PM | direct | 164 | 5 | 1233 | 159 | – |
| 2407 | 29-JUN-07 07.03.21.526005 AM | direct | 165 | 9 | 2519 | 155 | – |
| 2408 | 29-JUN-07 08.59.31.333617 AM | direct | 166 | 1 | 309 | 158 | – |
| 2409 | 29-JUN-07 09.53.41.984501 AM | direct | 167 | 2 | 48 | 154 | – |
| 2410 | 24-MAY-08 10.19.51.985501 AM | direct | 168 | 6 | 45175 | 156 | – |
| 2411 | 24-MAY-07 11.22.10.548639 AM | direct | 169 | 8 | 15760.5 | 156 | – |
| 2412 | 29-MAR-06 10.22.09.509801 AM | direct | 170 | 9 | 66816 | 158 | – |
| 2413 | 29-MAR-08 01.34.04.525934 PM | direct | 101 | 5 | 48552 | 161 | – |
| 2414 | 29-MAR-07 02.22.40.536996 PM | direct | 102 | 8 | 10794.6 | 153 | – |
| 2415 | 29-MAR-06 01.34.50.545196 PM | direct | 103 | 6 | 310 | 161 | – |
| 2416 | 29-MAR-07 04.41.20.945676 PM | direct | 104 | 6 | 384 | 160 | – |
| 2417 | 20-MAR-07 05.49.10.974352 PM | direct | 105 | 5 | 1926.6 | 163 | – |
| 2418 | 20-MAR-04 04.18.21.862632 PM | direct | 106 | 4 | 5546.6 | 163 | – |
| 2419 | 20-MAR-07 07.03.32.764632 PM | direct | 107 | 3 | 31574 | 160 | – |
| 2420 | 13-MAR-07 08.59.43.666320 PM | direct | 108 | 2 | 29750 | 160 | – |
| 2421 | 12-MAR-07 09.53.54.562432 PM | direct | 109 | 1 | 72836 | – | – |
| 2422 | 16-DEC-07 08.19.55.462332 PM | direct | 144 | 2 | 11188.5 | 153 | – |
| 2423 | 21-NOV-07 10.22.33.362632 AM | direct | 145 | 3 | 10367.7 | 160 | – |
| 2424 | 21-NOV-07 10.22.33.263332 AM | direct | 146 | 4 | 13824 | 153 | – |
| 2425 | 16-NOV-06 11.34.22.162552 PM | direct | 147 | 5 | 1500.8 | 163 | – |
| 2426 | 17-NOV-06 12.22.11.262552 AM | direct | 148 | 6 | 7200 | – | – |
| 2427 | 10-NOV-07 01.34.22.362124 AM | direct | 149 | 7 | 9055 | 163 | – |
| 2428 | 10-NOV-07 02.41.34.463567 AM | direct | 116 | 8 | 14685.8 | – | – |
| 2429 | 10-NOV-07 03.49.25.526321 AM | direct | 117 | 9 | 50125 | 154 | – |
| 2430 | 02-OCT-07 06.18.36.663332 AM | direct | 101 | 8 | 29669.9 | 159 | – |
| 2431 | 14-SEP-06 07.03.04.763452 AM | direct | 102 | 1 | 5610.6 | 163 | – |
| 2437 | 01-SEP-06 08.59.15.826132 AM | direct | 103 | 4 | 13550 | 163 | – |
| 2438 | 01-SEP-07 09.53.26.934626 AM | direct | 104 | 0 | 5451 | 154 | – |
| 2439 | 31-AUG-07 10.19.37.811132 AM | direct | 105 | 1 | 22150.1 | 159 | – |
| 2441 | 01-AUG-08 11.22.48.734526 AM | direct | 106 | 5 | 2075.2 | 160 | – |
| 2442 | 27-JUL-06 12.22.59.662632 PM | direct | 107 | 9 | 52471.9 | 154 | – |
| 2443 | 27-JUL-06 01.34.16.562632 PM | direct | 108 | 0 | 3646 | 154 | – |
| 2444 | 27-JUL-07 02.22.27.462632 PM | direct | 109 | 1 | 77727.2 | 155 | – |
| 2445 | 27-JUL-06 03.34.38.362632 PM | direct | 144 | 8 | 5537.8 | 158 | – |
| 2448 | 18-JUN-07 04.41.49.262632 PM | direct | 145 | 5 | 1388 | 158 | – |
| 2449 | 13-JUN-07 05.49.07.162632 PM | direct | 146 | 6 | 86 | 155 | – |
| 2450 | 11-APR-07 06.18.10.362632 PM | direct | 147 | 3 | 1636 | 159 | – |
| 2451 | 17-DEC-07 05.03.52.562632 PM | direct | 148 | 7 | 10474.6 | 154 | – |
| 2452 | 06-OCT-07 08.59.43.462632 PM | direct | 149 | 5 | 12589 | 159 | – |
| 2453 | 04-OCT-07 09.53.34.362632 PM | direct | 116 | 0 | 129 | 153 | – |
| 2456 | 07-NOV-06 07.53.25.989889 PM | direct | 117 | 0 | 3878.4 | 163 | – |
| 2457 | 31-OCT-07 11.22.16.162632 PM | direct | 118 | 5 | 21586.2 | 159 | – |
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_id | average_order_total |
|---|---|
| 107 | 38903.3 |
| 108 | 53349.925 |
| 158 | 25270.3 |
| 161 | 600 |
| 166 | 309 |
| 105 | 15344.125 |
| 109 | 66313.9 |
| 143 | 27132.6 |
| 159 | 69286.4 |
| 162 | 220 |
| 163 | 510 |
| 168 | 45175 |
| 104 | 36651.375 |
| 118 | 50495.9 |
| 145 | 14343.58 |
| 121 | 4797 |
| 141 | 38017.8 |
| 155 | 23431.9 |
| 101 | 47598.775 |
| 103 | 5147.85 |
| 116 | 8076.75 |
| 120 | 416 |
| 142 | 25691.3 |
| 151 | 17620 |
| 156 | 68501 |
| 157 | 7110.3 |
| 169 | 15760.5 |
| 146 | 17692.52 |
| 148 | 37140.1 |
| 149 | 80623.94 |
| 122 | 103834.4 |
| 152 | 7616.8 |
| 170 | 66816 |
| 144 | 32056.92 |
| 119 | 16447.2 |
| 153 | 48070.6 |
| 164 | 1233 |
| 167 | 48 |
| 102 | 17302.85 |
| 106 | 9049.875 |
| 147 | 74255.64 |
| 117 | 39452.175 |
| 123 | 11006.2 |
| 150 | 282694.3 |
| 154 | 26632 |
| 160 | 969.2 |
| 165 | 2519 |
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_date | current_order | end_date | next_order |
|---|---|---|---|
| 20-MAR-04 04.18.21.862632 PM | 1 | 08-JAN-06 08.19.44.123456 PM | 2 |
| 08-JAN-06 08.19.44.123456 PM | 2 | 08-JAN-06 09.34.13.112233 PM | 3 |
| 08-JAN-06 09.34.13.112233 PM | 3 | 26-JAN-06 09.22.51.962632 AM | 4 |
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_id | average_time_elapsed |
|---|---|
| 101 | 115.2418904 |
| 102 | 143.7728781 |
| 103 | 184.058966 |
| 104 | 300.123966 |
| 105 | 103.8827585 |
| 106 | 531.5982523 |
| 107 | 158.3332253 |
| 108 | 224.5728279 |
| 109 | 111.6594174 |
| 116 | 45.37932099 |
| 117 | 199.0622145 |
| 118 | 168.9078704 |
| 119 | – |
| 120 | – |
| 121 | – |
| 122 | – |
| 123 | – |
| 141 | – |
| 142 | – |
| 143 | – |
| 144 | 164.1952112 |
| 145 | 82.19456308 |
| 146 | 90.6041956 |
| 147 | 168.6040365 |
| 148 | 147.2241927 |
| 149 | 118.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_id | order_date | amount |
|---|---|---|
| 101 | 16-AUG-07 03.34.12.234359 PM | 46.61416667 |
| 101 | 02-OCT-07 06.18.36.663332 AM | 179.3024074 |
| 101 | 29-MAR-08 01.34.04.525934 PM | 119.8090972 |
| 101 | 27-JUL-08 08.59.10.223344 AM | – |
| 102 | 14-SEP-06 07.03.04.763452 AM | 196.3052778 |
| 102 | 29-MAR-07 02.22.40.536996 PM | 168.8131944 |
| 102 | 14-SEP-07 09.53.40.223345 AM | 66.20016204 |
| 102 | 19-NOV-07 02.41.54.696211 PM | – |
| 103 | 29-MAR-06 01.34.50.545196 PM | 155.8086227 |
| 103 | 01-SEP-06 08.59.15.826132 AM | 377.0553819 |
| 103 | 13-SEP-07 10.19.00.654279 AM | 19.31289352 |
| 103 | 02-OCT-07 05.49.34.678340 PM | – |
| 104 | 26-JAN-06 09.22.51.962632 AM | 427.3045023 |
| 104 | 29-MAR-07 04.41.20.945676 PM | 155.7167361 |
| 104 | 01-SEP-07 09.53.26.934626 AM | 317.3506597 |
| 104 | 14-JUL-08 06.18.23.234567 PM | – |
| 105 | 20-MAR-07 05.49.10.974352 PM | 163.6878125 |
| 105 | 31-AUG-07 10.19.37.811132 AM | 130.2802662 |
| 105 | 08-JAN-08 05.03.12.654278 PM | 17.68019676 |
| 105 | 26-JAN-08 09.22.41.934562 AM | – |
| 106 | 20-MAR-04 04.18.21.862632 PM | 659.2193519 |
| 106 | 08-JAN-06 09.34.13.112233 PM | 856.9756366 |
| 106 | 14-MAY-08 08.59.08.843679 PM | 78.59976852 |
| 106 | 01-AUG-08 11.22.48.734526 AM | – |
| 107 | 27-JUL-06 12.22.59.662632 PM | 236.2781597 |
| 107 | 20-MAR-07 07.03.32.764632 PM | 164.1177546 |
| 107 | 31-AUG-07 09.53.06.008765 PM | 74.60376157 |
| 107 | 14-NOV-07 12.22.31.223344 PM | – |
| 108 | 08-JAN-06 08.19.44.123456 PM | 199.7184259 |
| 108 | 27-JUL-06 01.34.16.562632 PM | 229.3093403 |
| 108 | 13-MAR-07 08.59.43.666320 PM | 244.6907176 |
| 108 | 13-NOV-07 01.34.21.986210 PM | – |
| 109 | 12-MAR-07 09.53.54.562432 PM | 136.6864931 |
| 109 | 27-JUL-07 02.22.27.462632 PM | 109.0129977 |
| 109 | 13-NOV-07 02.41.10.619477 PM | 89.27876157 |
| 109 | 10-FEB-08 09.22.35.564789 PM | – |
| 116 | 26-JUN-07 11.22.54.009932 PM | 67.28831019 |
| 116 | 02-SEP-07 06.18.04.378034 AM | 32.64965278 |
| 116 | 04-OCT-07 09.53.34.362632 PM | 36.2 |
| 116 | 10-NOV-07 02.41.34.463567 AM | – |
| 117 | 07-NOV-06 07.53.25.989889 PM | 261.465081 |
| 117 | 27-JUL-07 07.03.08.302945 AM | 105.8654745 |
| 117 | 10-NOV-07 03.49.25.526321 AM | 229.856088 |
| 117 | 27-JUN-08 12.22.11.647398 AM | – |
| 118 | 16-MAY-07 01.34.56.113356 AM | 168.9078704 |
| 118 | 31-OCT-07 11.22.16.162632 PM | – |
| 119 | 27-FEB-07 12.22.33.356789 AM | – |
| 120 | 27-FEB-08 01.34.51.220065 AM | – |
| 121 | 27-FEB-08 02.41.45.109654 AM | – |
| 122 | 26-FEB-07 03.49.50.459233 AM | – |
| 123 | 07-JUN-07 06.18.08.883310 AM | – |
| 141 | 07-JUN-07 07.03.01.001100 AM | – |
| 142 | 24-MAY-07 08.59.10.010101 AM | – |
| 143 | 16-MAY-07 09.53.02.909090 AM | – |
| 144 | 27-JUL-06 03.34.38.362632 PM | 402.3251736 |
| 144 | 02-SEP-07 11.22.53.134567 PM | 50.76878472 |
| 144 | 23-OCT-07 05.49.56.346122 PM | 54.10415509 |
| 144 | 16-DEC-07 08.19.55.462332 PM | 149.5827315 |
| 144 | 14-MAY-08 10.19.03.828321 AM | – |
| 145 | 18-JUN-07 04.41.49.262632 PM | 71.06731481 |
| 145 | 28-AUG-07 06.18.45.942399 PM | 22.71905093 |
| 145 | 20-SEP-07 11.34.11.456789 AM | 61.95025463 |
| 145 | 21-NOV-07 10.22.33.362632 AM | 173.0416319 |
| 145 | 12-MAY-08 11.22.30.545103 AM | – |
| 146 | 16-MAY-07 02.22.24.234567 AM | 28.64355324 |
| 146 | 13-JUN-07 05.49.07.162632 PM | 76.05170139 |
| 146 | 28-AUG-07 07.03.34.003399 PM | 84.63818287 |
| 146 | 21-NOV-07 10.22.33.263332 AM | 173.0833449 |
| 146 | 12-MAY-08 12.22.34.525972 PM | – |
| 147 | 02-FEB-06 01.34.56.345678 AM | 287.9162731 |
| 147 | 16-NOV-06 11.34.22.162552 PM | 145.7804167 |
| 147 | 11-APR-07 06.18.10.362632 PM | 139.111956 |
| 147 | 28-AUG-07 08.59.23.144778 PM | 101.6075 |
| 147 | 08-DEC-07 11.34.11.331392 AM | – |
| 148 | 17-NOV-06 12.22.11.262552 AM | 224.1799653 |
| 148 | 29-JUN-07 04.41.20.098765 AM | 160.3203009 |
| 148 | 06-DEC-07 12.22.34.225609 PM | 11.19534722 |
| 148 | 17-DEC-07 05.03.52.562632 PM | 193.2011574 |
| 148 | 27-JUN-08 09.53.32.335522 PM | – |
| 149 | 11-MAR-07 03.34.56.536966 PM | 185.5934491 |
| 149 | 13-SEP-07 05.49.30.647893 AM | 23.63209491 |
| 149 | 06-OCT-07 08.59.43.462632 PM | 34.19072917 |
| 149 | 10-NOV-07 01.34.22.362124 AM | 229.8648264 |
| 149 | 26-JUN-08 10.19.43.190089 PM | – |
| 150 | 04-JUN-07 04.41.12.554435 PM | – |
| 151 | 04-JUN-08 05.49.43.546954 PM | – |
| 152 | 18-NOV-07 04.18.50.546851 PM | – |
| 153 | 27-FEB-06 05.03.03.828330 PM | – |
| 154 | 21-JUL-07 08.59.57.571057 PM | – |
| 155 | 10-FEB-08 07.53.19.528202 PM | – |
| 156 | 02-FEB-06 08.19.11.227550 PM | – |
| 157 | 19-NOV-07 09.22.53.224175 PM | – |
| 158 | 19-NOV-07 10.22.38.340990 PM | – |
| 159 | 10-JUL-07 01.34.29.559387 AM | – |
| 160 | 10-JUL-07 02.22.53.554822 AM | – |
| 161 | 02-JUL-07 03.34.44.665170 AM | – |
| 162 | 01-JUL-07 04.49.13.615512 PM | – |
| 163 | 01-JUL-07 04.49.13.664085 PM | – |
| 164 | 01-JUL-07 04.49.13.678123 PM | – |
| 165 | 29-JUN-07 07.03.21.526005 AM | – |
| 166 | 29-JUN-07 08.59.31.333617 AM | – |
| 167 | 29-JUN-07 09.53.41.984501 AM | – |
| 168 | 24-MAY-08 10.19.51.985501 AM | – |
| 169 | 24-MAY-07 11.22.10.548639 AM | – |
| 170 | 29-MAR-06 10.22.09.509801 AM | – |
| customer_id | start_date | end_date | time_elapsed |
|---|---|---|---|
| 101 | 16-Aug-07 | 2-Oct-07 | 46.61416667 |
| 101 | 2-Oct-07 | 29-Mar-08 | 179.3024074 |
| 101 | 29-Mar-08 | 27-Jul-08 | 119.8090972 |
| 102 | 14-Sep-06 | 29-Mar-07 | 196.3052778 |
| 102 | 29-Mar-07 | 14-Sep-07 | 168.8131944 |
| 102 | 14-Sep-07 | 19-Nov-07 | 66.20016204 |
| 103 | 29-Mar-06 | 1-Sep-06 | 155.8086227 |
| 103 | 1-Sep-06 | 13-Sep-07 | 377.0553819 |
| 103 | 13-Sep-07 | 2-Oct-07 | 19.31289352 |
| 104 | 26-Jan-06 | 29-Mar-07 | 427.3045023 |
| 104 | 29-Mar-07 | 1-Sep-07 | 155.7167361 |
| 104 | 1-Sep-07 | 14-Jul-08 | 317.3506597 |
| 105 | 20-Mar-07 | 31-Aug-07 | 163.6878125 |
| 105 | 31-Aug-07 | 8-Jan-08 | 130.2802662 |
| 105 | 8-Jan-08 | 26-Jan-08 | 17.68019676 |
| 106 | 20-Mar-04 | 8-Jan-06 | 659.2193519 |
| 106 | 8-Jan-06 | 14-May-08 | 856.9756366 |
| 106 | 14-May-08 | 1-Aug-08 | 78.59976852 |
| 107 | 27-Jul-06 | 20-Mar-07 | 236.2781597 |
| 107 | 20-Mar-07 | 31-Aug-07 | 164.1177546 |
| 107 | 31-Aug-07 | 14-Nov-07 | 74.60376157 |
| 108 | 8-Jan-06 | 27-Jul-06 | 199.7184259 |
| 108 | 27-Jul-06 | 13-Mar-07 | 229.3093403 |
| 108 | 13-Mar-07 | 13-Nov-07 | 244.6907176 |
| 109 | 12-Mar-07 | 27-Jul-07 | 136.6864931 |
| 109 | 27-Jul-07 | 13-Nov-07 | 109.0129977 |
| 109 | 13-Nov-07 | 10-Feb-08 | 89.27876157 |
| 116 | 26-Jun-07 | 2-Sep-07 | 67.28831019 |
| 116 | 2-Sep-07 | 4-Oct-07 | 32.64965278 |
| 116 | 4-Oct-07 | 10-Nov-07 | 36.2 |
| 117 | 7-Nov-06 | 27-Jul-07 | 261.465081 |
| 117 | 27-Jul-07 | 10-Nov-07 | 105.8654745 |
| 117 | 10-Nov-07 | 27-Jun-08 | 229.856088 |
| 118 | 16-May-07 | 31-Oct-07 | 168.9078704 |
| 144 | 27-Jul-06 | 2-Sep-07 | 402.3251736 |
| 144 | 2-Sep-07 | 23-Oct-07 | 50.76878472 |
| 144 | 23-Oct-07 | 16-Dec-07 | 54.10415509 |
| 144 | 16-Dec-07 | 14-May-08 | 149.5827315 |
| 145 | 18-Jun-07 | 28-Aug-07 | 71.06731481 |
| 145 | 28-Aug-07 | 20-Sep-07 | 22.71905093 |
| 145 | 20-Sep-07 | 21-Nov-07 | 61.95025463 |
| 145 | 21-Nov-07 | 12-May-08 | 173.0416319 |
| 146 | 16-May-07 | 13-Jun-07 | 28.64355324 |
| 146 | 13-Jun-07 | 28-Aug-07 | 76.05170139 |
| 146 | 28-Aug-07 | 21-Nov-07 | 84.63818287 |
| 146 | 21-Nov-07 | 12-May-08 | 173.0833449 |
| 147 | 2-Feb-06 | 16-Nov-06 | 287.9162731 |
| 147 | 16-Nov-06 | 11-Apr-07 | 145.7804167 |
| 147 | 11-Apr-07 | 28-Aug-07 | 139.111956 |
| 147 | 28-Aug-07 | 8-Dec-07 | 101.6075 |
| 148 | 17-Nov-06 | 29-Jun-07 | 224.1799653 |
| 148 | 29-Jun-07 | 6-Dec-07 | 160.3203009 |
| 148 | 6-Dec-07 | 17-Dec-07 | 11.19534722 |
| 148 | 17-Dec-07 | 27-Jun-08 | 193.2011574 |
| 149 | 11-Mar-07 | 13-Sep-07 | 185.5934491 |
| 149 | 13-Sep-07 | 6-Oct-07 | 23.63209491 |
| 149 | 6-Oct-07 | 10-Nov-07 | 34.19072917 |
| 149 | 10-Nov-07 | 26-Jun-08 | 229.8648264 |
| 152 | 18-Nov-07 | – | – |
| 156 | 2-Feb-06 | – | – |
| 151 | 4-Jun-08 | – | – |
| 148 | 27-Jun-08 | – | – |
| 101 | 27-Jul-08 | – | – |
| 123 | 7-Jun-07 | – | – |
| 108 | 13-Nov-07 | – | – |
| 158 | 19-Nov-07 | – | – |
| 161 | 2-Jul-07 | – | – |
| 166 | 29-Jun-07 | – | – |
| 168 | 24-May-08 | – | – |
| 165 | 29-Jun-07 | – | – |
| 145 | 12-May-08 | – | – |
| 169 | 24-May-07 | – | – |
| 116 | 10-Nov-07 | – | – |
| 157 | 19-Nov-07 | – | – |
| 102 | 19-Nov-07 | – | – |
| 103 | 2-Oct-07 | – | – |
| 118 | 31-Oct-07 | – | – |
| 105 | 26-Jan-08 | – | – |
| 153 | 27-Feb-06 | – | – |
| 154 | 21-Jul-07 | – | – |
| 106 | 1-Aug-08 | – | – |
| 120 | 27-Feb-08 | – | – |
| 150 | 4-Jun-07 | – | – |
| 107 | 14-Nov-07 | – | – |
| 170 | 29-Mar-06 | – | – |
| 147 | 8-Dec-07 | – | – |
| 149 | 26-Jun-08 | – | – |
| 117 | 27-Jun-08 | – | – |
| 122 | 26-Feb-07 | – | – |
| 141 | 7-Jun-07 | – | – |
| 155 | 10-Feb-08 | – | – |
| 160 | 10-Jul-07 | – | – |
| 164 | 1-Jul-07 | – | – |
| 104 | 14-Jul-08 | – | – |
| 142 | 24-May-07 | – | – |
| 144 | 14-May-08 | – | – |
| 109 | 10-Feb-08 | – | – |
| 163 | 1-Jul-07 | – | – |
| 167 | 29-Jun-07 | – | – |
| 143 | 16-May-07 | – | – |
| 119 | 27-Feb-07 | – | – |
| 146 | 12-May-08 | – | – |
| 121 | 27-Feb-08 | – | – |
| 159 | 10-Jul-07 | – | – |
| 162 | 1-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.