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.