Question: Create Table Part ( Part_id int NOT NULL AUTO_INCREMENT, PartType Varchar(50), PartDescription VARCHAR(50), PartAmountAvailable int, CONSTRAINT Part_id_PK PRIMARY KEY (Part_id) ); Create Table Product (



Create Table Part ( Part_id int NOT NULL AUTO_INCREMENT, PartType Varchar(50), PartDescription VARCHAR(50), PartAmountAvailable int, CONSTRAINT Part_id_PK PRIMARY KEY (Part_id) );

Create Table Product ( Product_id int NOT NULL AUTO_INCREMENT, ProductType varchar (50), ProductDescription VARCHAR (50), CONSTRAINT Product_id_PK PRIMARY KEY (Product_id) );

Create Table PartsNeeded ( Part_id int NOT NULL, Product_id int NOT NULL, AmountNeeded int, CONSTRAINT Part_id_FK FOREIGN KEY (Part_id) REFERENCES Part(Part_id), CONSTRAINT Product_id_FK FOREIGN KEY (Product_id) REFERENCES Product(Product_id) );


And here are the queries to populate the database:


INSERT INTO Part (PartType, PartDescription, PartAmountAvailable) Values ('Wooden Screw', 'Made by Hebei Chengyi', 100000);

INSERT INTO Part (PartType, PartDescription, PartAmountAvailable) Values ('Wheel Bolt', 'Imported From Spain', 50000);


INSERT INTO Product (ProductType, ProductDescription) VALUES ('Metal Cabinet', 'Made In Ontatrio');

INSERT INTO Product (ProductType, ProductDescription) VALUES ('Wooden Storage Box on Wheels', 'By IKEA');



INSERT INTO PartsNeeded (Product_id, Part_id, AmountNeeded) VALUES (1, 2, 8);

INSERT INTO PartsNeeded (Product_id, Part_id, AmountNeeded) VALUES (2, 2, 8);

INSERT INTO PartsNeeded (Product_id, Part_id, AmountNeeded) VALUES (2, 1, 12);


I'm trying to make a select statement to group all Part_id needed by a specific Product entity, but only if the amount needed is under a specific amount. I tried:


SELECT COUNT(Part_id), AmountNeeded FROM PartsNeeded Group BY Product_id = 2 Having AmountNeeded <= 12;

This didn't work, I'm not entirely sure what to do here, any ideas?


Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!