SQL Challenges-1: Execution of orders: Overall execution Rate
SQL Challenges-1: Exercise-19 with Solution
From the following tables, write a SQL query to find the overall rate of execution of orders, which is the number of orders execution divided by the number of orders quote. Return rate_of_execution rounded to 2 decimals places.
Input:
Table: orders_issued
Structure:
Field | Type | Null | Key | Default | Extra |
---|---|---|---|---|---|
distributor_id | int(11) | YES | |||
company_id | int(11) | YES | |||
quotation_date | date | YES |
Data:
distributor_id | company_id | quotation_date |
---|---|---|
101 | 202 | 2019-11-15 |
101 | 203 | 2019-11-15 |
101 | 204 | 2019-11-15 |
102 | 202 | 2019-11-16 |
102 | 201 | 2019-11-15 |
103 | 203 | 2019-11-17 |
103 | 202 | 2019-11-17 |
104 | 203 | 2019-11-18 |
104 | 204 | 2019-11-18 |
Table: orders_executed
Structure:
Field | Type | Null | Key | Default | Extra |
---|---|---|---|---|---|
orders_from | int(11) | YES | |||
executed_from | int(11) | YES | |||
executed_date | date | YES |
Data:
orders_from | executed_from | executed_date |
---|---|---|
101 | 202 | 2019-11-17 |
101 | 203 | 2019-11-17 |
102 | 202 | 2019-11-17 |
103 | 203 | 2019-11-18 |
103 | 202 | 2019-11-19 |
104 | 203 | 2019-11-20 |
Sample Solution:
SQL Code(MySQL):
CREATE TABLE orders_issued (distributor_id int, company_id int, quotation_date date);
INSERT INTO orders_issued VALUES (101, 202, '2019-11-15');
INSERT INTO orders_issued VALUES (101, 203, '2019-11-15');
INSERT INTO orders_issued VALUES (101, 204, '2019-11-15');
INSERT INTO orders_issued VALUES (102, 202, '2019-11-16');
INSERT INTO orders_issued VALUES (102, 201, '2019-11-15');
INSERT INTO orders_issued VALUES (103, 203, '2019-11-17');
INSERT INTO orders_issued VALUES (103, 202, '2019-11-17');
INSERT INTO orders_issued VALUES (104, 203, '2019-11-18');
INSERT INTO orders_issued VALUES (104, 204, '2019-11-18');
CREATE TABLE orders_executed (orders_from int, executed_from int, executed_date date);
INSERT INTO orders_executed VALUES (101, 202, '2019-11-17');
INSERT INTO orders_executed VALUES (101, 203, '2019-11-17');
INSERT INTO orders_executed VALUES (102, 202, '2019-11-17');
INSERT INTO orders_executed VALUES (103, 203, '2019-11-18');
INSERT INTO orders_executed VALUES (103, 202, '2019-11-19');
INSERT INTO orders_executed VALUES (104, 203, '2019-11-20');
SELECT
ROUND(
IFNULL(
(SELECT COUNT(*) FROM (SELECT DISTINCT orders_from, executed_from FROM orders_executed) AS A)
/
(SELECT COUNT(*) FROM (SELECT DISTINCT distributor_id, company_id FROM orders_issued) AS B),
0)
, 2) AS rate_of_execution;
Sample Output:
rate_of_execution| -----------------| 0.67|
SQL Code Editor:
Contribute your code and comments through Disqus.
Previous: Orders items 5 or more times.
Next: Audience in the match.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/sql-exercises/challenges-1/sql-challenges-1-exercise-19.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics