Sub-query Sales and inventory - mysql

I have three tables: Products, Inventory and Ingredients
Product Table
ID[PK] Name Type
1 OrdinaryBurger Burger
2 CheeseBurger Burger
Inventory table
ID[PK] Item_name Stocks
100 Buns 5
101 Patties 5
103 Cheese 0
Ingredients table
ID[PK] ProductID[FK] InventoryID[FK] Quantity
1001 1 100 1
1002 1 101 1
1003 2 100 1
1004 2 101 1
1005 2 103 1
I want to write a query that can filter all the products to not display if the connected inventory stock is 0, for example this will not display Cheeseburger because the stock of cheese is 0. THanks

The following will return only products that have at least enough ingredients to make.
SELECT * FROM Products WHERE ProductID NOT IN (
SELECT i.ProductID
FROM Ingredients i
JOIN Inventory iv ON i.InventoryID = iv.InventoryId
GROUP BY i.ProductID, i.InventoryId
HAVING SUM(iv.Stocks) < SUM(i.Quantity)
)

Related

Count inventory quantity

I need to count the quantity of item left in the inventory, but I don't know how to make it happen.
Items
ITEM_ID ITEM_BARCODE ITEM_NAME ITEM_ROP
-------------------------------------------------
101 76164231 Marlboro 20 20
102 76164217 Marlboro B 20 10
103 9555192501305 Juicy Fruit 5
104 9300682016278 M & M Crispy 5
Stocks
STOCK_ID ITEM_ID STOCK_QUANTITY STOCK_DATE
----------------------------------------------
001 101 20 01-JUL-18
002 104 30 02-JUL-18
003 101 50 02-JUL-18
004 104 20 05-JUL-18
005 103 45 06-JUL-18
Sales
SALES_ID ITEM_ID SALES_QUANTITY SALES_DATE
----------------------------------------------
001 101 20 02-JUL-18
002 104 30 02-JUL-18
003 101 35 03-JUL-18
004 103 20 09-JUL-18
And there should be two result, the first one as shown below where it will produce item quantity left based from table Stocks and Sales. Table Stocks will keep the record of item quantity. Meanwhile, table Sales is where it will keep the record of quantity item that has been sold (STOCK_QUANTITY - SALES_QUANTITY).
ITEM_BARCODE ITEM_NAME QUANTITY
------------------------------------------
76164231 Marlboro 20 15
76164217 Marlboro B 20 0
9555192501305 Juicy Fruit 25
9300682016278 M & M Crispy 20
The second output is where it will display based on which item is lower than the reorder point (ROP) as in table Items
ITEM_BARCODE ITEM_NAME QUANTITY ROP
----------------------------------------------
76164231 Marlboro 20 15 20
76164217 Marlboro B 20 0 10
SELECT T.STOCK_DATE,
I.ITEM_BARCODE,
I.ITEM_NAME,
SUM(T.STOCK_QUANTITY) - SUM(S.SALES_QUANTITY) AS QUANTITY
FROM ITEMS I
INNER
JOIN STOCKS T
ON I.ITEM_ID = T.ITEM_ID
LEFT
JOIN SALES S
ON I.ITEM_ID = S.ITEM_ID
GROUP
BY I.ITEM_BARCODE,
T.STOCK_DATE,
I.ITEM_NAME
ORDER
BY T.STOCK_DATE;
Anyone could help me with this? Thank you so much.

SQL query that finds all the column, that is in multiple rows, but have different value in another column?

If I have a "SALES" table, with columns of SaleID, Product#, and CustomerName. and a PRODUCTS table with two columns product_ID and Name. The contains 5 differnt products. In the SALES table populates when a sale is made.
How would I query customer_name with only Product_ID of 1 and 2?
sales table
SALES_ID PRODUCT_ID CUSTOMER_NAME
1 1 DAVE
2 2 DAVE
3 3 DAVE
4 1 TOM
5 2 TOM
6 1 JANE
7 1 MIKE
8 1 MIKE
9 3 MIKE
10 4 MARY
I would like a table result to be
SALES_ID PRODUCT_ID CUSTOMER_NAME
1 1 TOM
2 2 TOM
Select s.CustomerName from SALES s
INNER JOIN PRODUCTS p ON s.Product#=p.Product#
WHERE p.Product# =1
INTERSECT
Select s.CustomerName from SALES s
INNER JOIN PRODUCTS p ON s.Product#=p.Product#
WHERE p.Product# =2

Add total of 3 rows for specific id

I have three tables:
Students
-------------------------------------------------------------
studentId first last gender weight
-------------------------------------------------------------
1 John Doe m 185
2 John Doe2 m 130
3 John Doe3 m 250
Lifts
-------------------
liftId name
-------------------
1 Bench Press
2 Power Clean
3 Parallel Squat
4 Deadlift
5 Shoulder Press
StudentLifts
------------------------------------------------
studentLiftId studentId liftId weight
------------------------------------------------
1 1 1 185
2 2 3 130
3 3 1 190
4 1 2 120
5 2 1 155
6 3 2 145
7 1 1 135
8 1 1 205
9 2 3 200
10 1 3 150
11 2 2 110
12 3 3 250
I would like to have four top lists:
Bench Press
Parallel Squat
Power Clean
Total of the above 3
I can successfully grab a top list for each specific lift using the following query:
SELECT s.studentId, s.first, s.last, s.gender, s.weight, l.name, sl.weight
FROM Students s
LEFT JOIN (
SELECT *
FROM StudentLifts
ORDER BY weight DESC
) sl ON sl.studentId = s.studentId
LEFT JOIN Lifts l ON l.liftId = sl.liftId
WHERE l.name = 'Bench Press'
AND s.gender = 'm'
AND s.weight > 170
GROUP BY s.studentId
ORDER BY sl.weight DESC
However, I am stuck on how to add the highest total of each lift for each student. How can I first find the highest total for each student in each lift, and then add them up to get a total of all three lifts?
Edit
The result set that I am looking for would be something like:
-------------------------------------------------
studentId first last weight
-------------------------------------------------
3 John Doe3 585
1 John Doe 475
2 John Doe2 465
I also forgot to mention that I would actually like two lists, one for students above 170 and one for students below 170.
SELECT -- join student a total weight to the student table
A.studentId,
A.first,
A.last,
C.totalWeight
FROM
Student A,
(
SELECT -- for each studet add the max weights
sum(B.maxWeight) as totalWeight,
B.studentID
FROM (
SELECT -- for each (student,lift) select the max weight
max(weight) as maxWeight,
studentId,
liftID
FROM
StudentLifts
GROUP BY
studentId,
liftID
) B
GROUP BY
studentId
) C
WHERE
A.studentID = C.studentId
-- AND A.weight >= 170
-- AND A.weight < 170
-- pick one here to generate on of the two lists.

table joins with multiple group_concat

I have a problem regarding joining tables with group_concat. Here are the details.
table_orders:
item_cd order_id descs quantity status seq_no
1 100 coca-cola 2 A 232
2 100 pizza 1 A 233
3 101 cheeseburger 5 A 234
4 102 pepsi 4 A 235
4
table_instructions:
item_cd instruction
3 more cheese
3 less vegetable
cancelled_item_table:
quantity seq_no
1 234
1 234
1 235
Now what I want to achieve is like this:
item_cd descs quantity instructions cancelled_item
1 coca-cola 2 - -
2 pizza 1 - -
3 cheeseburger 2 more cheese, less vegetable 1,1
4 pepsi 4 - 1
This is my current query:
SELECT
ord.item_cd,
ord.order_id,
ord.descs,
ord.quantity,
GROUP_CONCAT(x.quantity) as cancelled,
GROUP_CONCAT(i.instruction) as instruct
FROM table_orders ord
LEFT JOIN cancelled_item_table x ON ord.seq_no = x.seq_no
LEFT JOIN table_instructions i ON ord.item_cd = i.item_cd
WHERE ord.status = 'A'
GROUP BY ord.order_id
and here is the output:
item_cd descs quantity instructions cancelled_item
1 coca-cola 2 - 1
2 pizza 1 - 1
3 cheeseburger 2 more cheese, more cheese,
less vegetable, less vegetable 1,1,1,1
4 pepsi 4 - 1
If you notice, cheeseburger has 2 cancelled item and 2 instruction, but the output is 4, looks like it's multiplying.
Since the join with cancelled_item_table multiplies rows, you have to join to an already grouped subquery, like this:
SELECT
ord.item_cd,
ord.order_id,
ord.descs,
ord.quantity - coalesce(x.tot,0) as quantity,
GROUP_CONCAT(i.instruction) as instruct,
x.cancelled
FROM
table_orders ord LEFT JOIN table_instructions i
ON ord.item_cd = i.item_cd LEFT JOIN
(select seq_no, count(*) as tot, GROUP_CONCAT(quantity) as cancelled
from cancelled_item_table
group by seq_no) x ON ord.seq_no = x.seq_no
WHERE ord.status = 'A'
GROUP BY ord.item_cd, ord.order_id, ord.descs, quantity

MySQL get top average entries

I am trying to write a mysql query to return the top 3 courses that have the highest average course rating. I have two tables, Ratings and Courses.
The Ratings table:
courseId rating
1 6
2 2
1 4
2 5
3 3
4 0
6 0
The Courses Table:
courseId cnum cname
1 100 name1
2 112 name2
3 230 name3
4 319 name4
5 122 name5
6 320 name6
I need to return the top 3 courses that have the highest average rating. Any ideas how I could do this? Thanks
SELECT Courses.*
FROM Courses NATURAL JOIN (
SELECT courseId, AVG(rating) avg_rating
FROM Ratings
GROUP BY courseId
ORDER BY avg_rating DESC
LIMIT 3
) t
See it on sqlfiddle.