SQL Challenges-1: Average Selling Price
From the following tables write a SQL query to find the average selling price for each item. Return item code and average_selling_price. average_selling_price should be rounded to 2 decimal places.
Input:
Table: item_price
Structure:
Field | Type | Null | Key | Default | Extra |
---|---|---|---|---|---|
item_code | int(11) | YES | |||
date_from | date | YES | |||
date_to | date | YES | |||
item_cost | int(11) | YES |
Data:
item_code | date_from | date_to | item_cost |
---|---|---|---|
101 | 2018-04-07 | 2018-06-28 | 8 |
102 | 2018-02-15 | 2018-04-17 | 13 |
103 | 2018-03-12 | 2018-04-30 | 10 |
101 | 2018-06-29 | 2018-10-31 | 15 |
103 | 2018-05-01 | 2019-08-24 | 14 |
102 | 2018-04-18 | 2018-07-10 | 25 |
104 | 2018-06-11 | 2018-10-10 | 25 |
101 | 2018-11-01 | 2019-01-15 | 20 |
Table: sale
Structure:
Field | Type | Null | Key | Default | Extra |
---|---|---|---|---|---|
sale_date | date | YES | |||
item_code | int(11) | YES | |||
sale_qty | int(11) | YES |
Data:
sale_date | item_code | sale_qty |
---|---|---|
2018-05-15 | 101 | 120 |
2018-04-27 | 103 | 80 |
2018-04-10 | 102 | 200 |
2018-07-12 | 101 | 100 |
2018-07-07 | 103 | 50 |
2018-09-17 | 104 | 100 |
2018-06-25 | 102 | 100 |
Sample Solution:
SQL Code(MySQL):
Sample Output:
item_code|average_selling_price| ---------|---------------------| 101| 11.18| 102| 17.00| 103| 11.54| 104| 25.00|
SQL Code Editor:
Contribute your code and comments through Disqus.
Previous: Order status report for each month for each company to find booked and cancelled order number and quantity.
Next: All People Report to the Given Manager.