SQL Challenges-1: Order status report for each month for each company to find booked and cancelled order number and quantity
From the following table write a SQL query to find for each month and company, the number of orders issued and their total quantity, the number of orders cancelled and their total quantity. Return month, name of the company, number of orders booked, number of booked quantity, number of cancelled order, and number of cancelled quantity.
Input:
Table: order_stat
Structure:
Field | Type | Null | Key | Default | Extra |
---|---|---|---|---|---|
order_id | int(11) | NO | PRI | ||
com_name | varchar(25) | YES | |||
ord_qty | int(11) | YES | |||
ord_stat | varchar(25) | YES | |||
stat_date | date | YES |
Data:
order_id | com_name | ord_qty | ord_stat | stat_date |
---|---|---|---|---|
151 | MMS INC | 500 | Booked | 2020-08-15 |
152 | BCT LTD | 300 | Cancelled | 2020-08-15 |
153 | MMS INC | 400 | Cancelled | 2020-08-26 |
154 | XYZ COR | 500 | Booked | 2020-08-15 |
155 | MMS INC | 500 | Cancelled | 2020-10-11 |
156 | BWD PRO LTD | 250 | Cancelled | 2020-11-15 |
157 | BCT LTD | 600 | Booked | 2020-10-07 |
158 | MMS INC | 300 | Booked | 2020-12-11 |
159 | XYZ COR | 300 | Booked | 2020-08-26 |
160 | BCT LTD | 400 | Booked | 2020-11-15 |
Table: order_return
Structure:
Field | Type | Null | Key | Default | Extra |
---|---|---|---|---|---|
order_id | int(11) | NO | MUL | ||
return_date | date | YES |
Data:
order_id | return_date |
---|---|
153 | 2020-10-12 |
154 | 2020-11-07 |
156 | 2020-12-05 |
159 | 2020-09-17 |
Sample Solution:
SQL Code(MySQL):
Sample Output:
month |com_name |booked_count|booked_qty|cancelled_count|cancelled_qty| -------|-----------|------------|----------|---------------|-------------| 2020-08|BCT LTD | 0| 0.0| 1| 300.0| 2020-08|MMS INC | 1| 500.0| 1| 400.0| 2020-08|XYZ COR | 2| 800.0| 0| 0.0| 2020-10|BCT LTD | 1| 600.0| 0| 0.0| 2020-10|MMS INC | 0| 0.0| 1| 500.0| 2020-11|BCT LTD | 1| 400.0| 0| 0.0| 2020-11|BWD PRO LTD| 0| 0.0| 1| 250.0| 2020-12|MMS INC | 1| 300.0| 0| 0.0|
SQL Code Editor:
Contribute your code and comments through Disqus.
Previous: Order status report for each month for each company.
Next: Average Selling Price.