w3resource

SQL Challenges-1: Compare the purchasing status of the average purchase quantity of products of a category to the average pruchase quantity of the distributor

SQL Challenges-1: Exercise-52 with Solution

From the following tables write a query in SQL to compare the purchasing status of the average purchase quantity of products of a category to the average pruchase quantity of the distributor. Return purchase month, category_id and purchase status.

Input:

Table: product

Structure:

FieldTypeNullKeyDefaultExtra
product_idintNOPRI
category_idintNO

Data:

product_idcategory_id
8001150
8002160
8003160
8004150
8005160

Table: purchase

Structure:

FieldTypeNullKeyDefaultExtra
purchase_nointNOPRI
item_codeintNOMUL
purchase_qtyintYES
purchase_datedateYES

Data:

purchase_noitem_codepurchase_qtypurchase_date
100180012402019-12-17
100280021502019-12-17
100380031752020-11-15
100480041502019-12-17
100580051452019-12-05
100680011502020-01-05
100780022002020-01-15
100880031502020-12-17
100980012002020-01-28
101080021802020-02-07
101180013002020-02-25
101280051002020-01-27

Sample Solution:

SQL Code(MySQL):

CREATE TABLE  product(
product_id INTEGER(5) NOT NULL unique,
category_id   INTEGER(4) NOT NULL);

 
insert into product values(8001,150);
insert into product values(8002,160);
insert into product values(8003,160);
insert into product values(8004,150);
insert into product values(8005,160);	 

CREATE TABLE purchase (
purchase_no INTEGER(5) NOT NULL unique,
item_code   INTEGER(4) NOT NULL,
purchase_qty  integer(5),
purchase_date  date,
foreign key (item_code) references product(product_id));

insert into purchase values(1001,8001,240,'2019-12-17');
insert into purchase values(1002,8002,150,'2019-12-17');
insert into purchase values(1003,8003,175,'2020-11-15');
insert into purchase values(1004,8004,150,'2019-12-17');
insert into purchase values(1005,8005,145,'2019-12-05');
insert into purchase values(1006,8001,150,'2020-01-05');
insert into purchase values(1007,8002,200,'2020-01-15');
insert into purchase values(1008,8003,150,'2020-12-17');
insert into purchase values(1009,8001,200,'2020-01-28');
insert into purchase values(1010,8002,180,'2020-02-07');
insert into purchase values(1011,8001,300,'2020-02-25');
insert into purchase values(1012,8005,100,'2020-01-27');


SELECT pur.month purchase_month,category_id, 
CASE WHEN category_average > distributor_average then 'increase'
     WHEN category_average < distributor_average then 'decrease'
     WHEN category_average = distributor_average then 'remain same'
END AS purchase_status
FROM 
    (SELECT SUBSTR(purchase_date,1,7) month,category_id,avg(purchase_qty) category_average
    FROM purchase JOIN product ON purchase.item_code = product.product_id
    group by 1,2) pur JOIN 
    (SELECT SUBSTR(purchase_date,1,7) month,avg(purchase_qty) distributor_average
    FROM purchase group by 1) dis 
    ON pur.month = dis.month;

Sample Output:

purchase_month|category_id|purchase_status|
--------------+-----------+---------------+
2019-12       |        150|increase       |
2020-01       |        150|increase       |
2020-02       |        150|increase       |
2019-12       |        160|decrease       |
2020-01       |        160|decrease       |
2020-02       |        160|decrease       |
2020-11       |        160|remain same    |
2020-12       |        160|remain same    |

SQL Code Editor:


Contribute your code and comments through Disqus.

Previous: Salesman who makes Largest Number of transactions.
Next: Highest difference in total sale of all quarters on a product of many companies.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

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-52.php