- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
SQL query describing usage of SUM aggregate function and GROUP-BY with HAVING
Problem: Write the DB2 SQL query to give the sum of ORDER_TOTAL for the orders placed on 29th July and 30th July individually. The result should come in a single table.
We can find the sum of ORDER_TOTAL for the orders placed on 29th and 30th July individually using aggregate function SUM, GROUP BY and HAVING.
For example, if we have an ORDER table as below.
Below is the query which will give the desired result.
SELECT ORDER_DATE, SUM(ORDER_TOTAL) FROM ORDERS GROUP BY ORDER_DATE HAVING ORDER_DATE IN (‘29-07-2020’, ‘30-07-2020’)
In this query, we have selected ORDER_DATE and ORDER_TOTAL with aggregate function SUM.
The GROUP BY will ensure that the sum of ORDER_TOTAL is taken date wise and HAVING clause will ensure that the result of only 29th and 30th July is displayed. The query will give the following result.
- What is the SQL query describing usage of MAX aggregate function and GROUP-BY with HAVING?
- Example of SQL query describing COUNT function and GROUP BY
- Explain SQL describing COUNT aggregate and CURRENT DATE function
- Example of SQL query describing the conditional processing
- HAVING with GROUP BY in MySQL
- How to display records having sum between a specific range using GROUP BY, HAVING and ORDER BY in a single MySQL query?
- How will GROUP BY clause perform without an aggregate function?
- Difference Between Group By and Order By in SQL
- What is the benefit of using MySQL SUM() function with GROUP BY clause?
- MongoDB query to implement aggregate function
- Explain aggregate functions with the help of SQL queries
- Group-by and Sum in Python Pandas
- Display the record with non-duplicate Id using MySQL GROUP BY and HAVING
- How can I aggregate collection and group by field count in MongoDB?
- Implement MongoDB Aggregate - unwind, group and project?