w3resource

SQL Exercises: Sums of all order amounts, grouped by date

SQL SUBQUERY: Exercise-14 with Solution

14. Write a query to find the sums of the amounts from the orders table, grouped by date, and eliminate all dates where the sum was not at least 1000.00 above the maximum order amount for that date.

Sample table: Orders
ord_no      purch_amt   ord_date    customer_id  salesman_id
----------  ----------  ----------  -----------  -----------
70001       150.5       2012-10-05  3005         5002
70009       270.65      2012-09-10  3001         5005
70002       65.26       2012-10-05  3002         5001
70004       110.5       2012-08-17  3009         5003
70007       948.5       2012-09-10  3005         5002
70005       2400.6      2012-07-27  3007         5001
70008       5760        2012-09-10  3002         5001
70010       1983.43     2012-10-10  3004         5006
70003       2480.4      2012-10-10  3009         5003
70012       250.45      2012-06-27  3008         5002
70011       75.29       2012-08-17  3003         5007
70013       3045.6      2012-04-25  3002         5001

Sample Solution:

-- Selecting 'ord_date' and the sum of 'purch_amt' from the 'orders' table (aliased as 'a')
SELECT ord_date, SUM(purch_amt)
-- Specifying the table to retrieve data from ('orders' as 'a')
FROM orders a
-- Grouping the results by 'ord_date'
GROUP BY ord_date
-- Filtering the grouped results based on the condition that the sum of 'purch_amt' is greater than a calculated threshold
HAVING SUM(purch_amt) >
    -- Subquery: Calculating the threshold as 1000.00 plus the maximum 'purch_amt' for the same 'ord_date'
    (SELECT 1000.00 + MAX(purch_amt) 
     FROM orders b 
     WHERE a.ord_date = b.ord_date);

Output of the Query:

ord_date	sum
2012-09-10	6979.15
2012-10-10	4463.83

Explanation:

The said SQL query that selects the "ord_date" and the sum of the "purch_amt" from the 'orders' table (aliased as 'a') and groups them by the "ord_date" column. The query also includes a "HAVING" clause which filters the groups of records based on a condition. The condition is that the sum of the "purch_amt" for each group must be greater than a value determined by the subquery, which selects the result of the arithmetic operation of adding 1000.00 to the maximum value of the "purch_amt" column from the 'orders' table (aliased as 'b') where the "ord_date" in the outer query (aliased as 'a') is equal to the "ord_date" in the subquery. In other words, it will only return the "ord_date" and sum of "purch_amt" for each date where the total purchase amount made on that date is more than the max purchase amount made on that date + 1000.

Visual Explanation:

SQL Subqueries:  Find the sums of the order amounts, grouped by date, eliminating all those dates where the sum was not at least 1000.00 above the MAX amount for that date.

Practice Online


Sample Database: inventory

Inventory database model

Query Visualization:

Duration:

Query visualization of Find the sums of the order amounts, grouped by date, eliminating all those dates where the sum was not at least 1000.00 above the MAX amount for that date - Duration

Rows:

Query visualization of Find the sums of the order amounts, grouped by date, eliminating all those dates where the sum was not at least 1000.00 above the MAX amount for that date - Rows

Cost:

Query visualization of Find the sums of the order amounts, grouped by date, eliminating all those dates where the sum was not at least 1000.00 above the MAX amount for that date - Cost

Contribute your code and comments through Disqus.

Previous SQL Exercise: Customer order amounts that are on or above average.
Next SQL Exercise: Find out if any of the customers are located in London.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.