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.
Related
I have this records in products table with product_id and its price
product_id
price
1
150
1
190
2
20
2
12
3
123
4
513
5
157
5
147
and I want to get the top 3 products and arrange it by average price something like this
product_id
price
avg_price
4
513
513
1
150
170
1
190
170
5
157
152
5
147
152
how to write/code it in sql query or laravel eloquent query?
WITH AverageCTE AS
(
SELECT product_id, AVG(avg_price) as avg_price
FROM products
GROUP BY product_id
)
SELECT p.product_id, price, avg_price
FROM product p JOIN
(SELECT * FROM AverageCTE ORDER BY avg_price DESC LIMIT 3) a
on p.product_id = a.product_id
ORDER BY avg_price DESC
In order to display data (orders or bids) in a excel-like grid, i fetch from my database with the help of this query. In this case, the order_bid table store the lines that compose an order (a line for each item), the item table defines the price of an item, the current sale the item is displayed on, and the lot table defines the item itself (name, description_ect...)
Here is the request i make, orders are another table but the important part is that an order_bid is linked to an order, and orders are linked to a unique sale, but it seems to ignore the WHERE close, so i get a lot more results than expected (for example if a client placed a bid for lot 4 of sale 1, i would see a bid for sale 2 lot 4, and so on for all sales that have a lot number 4 (so all of them basically))
SELECT o.id, o.order_id, o.lot_num, o.bid, i.lot_id, i.sale, i.price, l.description_fr
FROM order_bid o
INNER JOIN item i
ON o.lot_num = i.lot_num
INNER JOIN lot l
ON l.id = i.lot_id
WHERE o.order_id = 38
For example i expect for a request a result like:
id
order_id
lot_num
bid
lot_id
sale
price
description_fr
110
38
17
135
19
sale1
135
description1
111
38
21
83
23
sale1
183
description2
109
38
10
100
790
sale1
100
description3
but i get:
id
order_id
lot_num
bid
lot_id
sale
price
description_fr
110
38
17
135
19
sale1
135
description1
111
38
21
83
23
sale1
183
description2
111
38
21
183
11968
sale11
150
description4
109
38
10
100
790
sale1
100
description3
and a lot of other lots that i shouldn't be getting from other sales
and i verified, when i request all the order_bid with order_id = 38 (SELECT * FROM order_bid WHERE order_id = 38) i get:
id
order_id
lot_num
bid
109
38
10
100
110
38
17
135
111
38
21
183
summarized: i get info from multiple sales when i'm supposed to get only from one (done using the order_id filter)
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)
)
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.
My two tables are
Entry
event_id competitor_id place
101 101 1
101 102 2
101 201 3
101 301 4
102 201 2
103 201 3
second table lists prizes on offer for the events
Prize
event_id place money
101 1 120
101 2 60
101 3 30
102 1 10
102 2 5
102 3 2
103 1 100
103 2 60
103 3 40
From this I am looking to show all the information from the Entry table alongside the amount of money they won for their respected placing. If they failed to place in the money then a 0 will be displayed.
Any help would be appreciated.
try this:
SELECT a.Event_ID,
a.Competitor_ID,
a.Place,
COALESCE(b.money, 0) as `Money`
FROM entry a left join prize b
on (a.event_id = b.event_ID) AND
(a.place = b.Place)
hope this helps.
EVENT_ID COMPETITOR_ID PLACE MONEY
101 101 1 120
101 102 2 60
101 201 3 30
101 301 4 0 -- << this is what you're looking for
102 201 2 5
103 201 3 40
Try this:
select e.*, coalesce(p.money, 0) money from entry e
left join prize p
on e.event_id = p.event_id and e.place = p.place
You can play with the fiddle here.
SELECT * FROM Entry NATURAL LEFT JOIN Prize;
If you absolutely want 0 instead of NULL for the "money" when no prize was won (but how can you differentiate between a prize of 0 and no prize?):
SELECT Entry.*, COALESCE(money, 0) AS money FROM Entry NATURAL LEFT JOIN Prize;
See them both on sqlfiddle.