How to count grouped records that match multiple criteria - mysql

i have three tables
table 1; product table with product_name, product_id, product price
table 2; store table with store_id, store_name
table 3; stock table with id, store_id, product_id, sock, min_stock
i need to sum the stock of each product as total_stock and also sum the min_stock of each product as total_min_stock.
I should then count the number of products whose total_stock value is less than total_min_stock as low_stock_number and echo the value of low_stock_number

Assuming that each table's unique value is its id, what you need is to aum and group by, using HAVING to condition the GROUPed results
SELECT
p.product_id,
SUM(IFNULL(st.stock,0) as total_stock,
SUM(IFNULL(st.min_stock,0) as total_min_stock
FROM
product p
LEFT JOIN stock st ON st.product_id = st.product_id
GROUP BY
p.product_id
HAVING
total_stock < total_min_stock

Related

Sum Values that have refer to same column

My database looks like this after I do my query:
What I would like to get is the orderID with the most amount of quantities.
This is why I did to get the result from the picture:
SELECT orderid, quantity
From "Order"
JOIN orderitem
ON "Order".id = orderitem.orderid
JOIN product
ON orderitem.productid = product.id
I don't quite get how to add the values correctly to get the maximum. Happy for every help :)
You can use aggregated function sum and limit, following query will give only one orderId with most amount of quantities.
SELECT orderid, sum(quantity) as quantity
From "Order"
JOIN orderitem
ON "Order".id = orderitem.orderid
JOIN product
ON orderitem.productid = product.id
group by
orderid
order by
quantity desc
limit 1

Sql query to calculate total cost using 3 tables

Order details table contains catalog id and its quantity.
Catalog has the price of the particular item.
For one order he or she can select only 2 items. The selected items total cost has to be generated and stored in orders table for a particular order using order id.
The columns in my catalogs are id, name, price.
The columns in order details are id, order_id, catalog_id, quantity.
The columns in orders table are id, customer_id, total_cost.
How can I calculate the total cost using 3 tables in mysql?
Try this.
SET SQL_SAFE_UPDATES = 0;
UPDATE orders
INNER JOIN (
SELECT order_id, SUM(quantity*price) total_price
FROM order_details
INNER JOIN my_catalogs
ON order_details.catalog_id = my_catalogs.id
GROUP BY order_id
) d
ON order.id = d.order_id
SET orders.total_cost = d.total_price;
SET SQL_SAFE_UPDATES = 1;

MySQL Join with sum()

I have two tables; One contains for products stats and another one contains additional stats
StatsHourly:
id
product_id (can be multiple)
amount
cost
time
StatsValues:
id
product_id (can be multiple)
value (double)
I need to join those two tables and get something like this in the result:
product_id
sum (amount)
sum (cost)
sum (value)
I'm trying to do this:
"SELECT
SUM(s.amount) as amount,
SUM(s.cost) as cost
FROM StatsHourly s
LEFT JOIN (
SELECT
COALESCE(SUM(value), 0) as value
FROM StatsValues
GROUP BY product_id
) value v ON v.product_id = s.product_id
WHERE 1
AND s.product_id = :product_id";
This doesn't work. Could someone show me the right way to do it?
You have an extra comma after as cost:
SUM(s.cost) as cost, <-- here
You also use 2 aliases for the subquery, you should remove value from there:
) value v
You do not use any output from the subquery.
Coalesce() is unnecessary in the subquery.
This works (tested):
SELECT
s.product_id as product_id,
s.amount_s as amount,
s.cost_s as cost,
v.value_v as value
FROM
(SELECT
product_id,
SUM(amount) as amount_s,
SUM(cost) as cost_s
FROM StatsHourly
GROUP BY product_id) as s
LEFT JOIN
(SELECT
product_id,
SUM(value) as value_v
FROM StatsValues
GROUP BY product_id) as v
ON v.product_id = s.product_id;
WHERE s.product_id = 'product_id';
The point is:
As you have multiple equal product_id in BOTH table you have to make two aggregated tables through subqueries that makes the product_id unique and sum all appropriate rows.
After that you can join and you select the already aggregated values.
Regards

select multiple rows from tow tables by duplicates of one table

I want to list bestsellers
I have list of duplicates of sold products from table SALE and want to select those product properties from PRODUCT table
my query to find duplicates from table SALE is:
SELECT product, COUNT(*) AS COUNT
FROM sale
GROUP BY product
HAVING COUNT(*) > 1
ORDER BY COUNT DESC
this query lists my bestsellers products, how to list these products properties from PRODUCT table in this order
In MySQL you can just join in the product table:
SELECT p.product, COUNT(*) AS COUNT
FROM product p JOIN
sale s
ON p.product = s.product
GROUP BY p.product
HAVING COUNT(*) > 1
ORDER BY COUNT DESC ;
This assumes that p.product is the primary key on PRODUCT that links it to SALES. Also, are you just counting "orders" or do you have a "quantity" field that you also care about?

SQL List Products that are Not Already Ordered

I have a situation where a LEFT JOIN is not going far enough in what I need to accomplish. I have a product table (Products with columns ItemID, ProductName, Price) and an order table (Orders with columns OrderNumber, ItemID, Quantity).
I need the query to return all of the products from the Products table that are not currently a part of a specific order (for example, list all products that are not a part of OrderNumber 52).
My current query lists all of the products but excludes the products that are a part of ANY OrderNumber.
$query = "SELECT Products.ItemID, Products.ProductName
FROM Products
LEFT JOIN Orders
ON Orders.ItemID = Products.ItemID
WHERE Orders.ItemID IS NULL
ORDER BY Products.ProductName";
You can use a anti-join for this purpose, like so:
SELECT ItemID, ProductName
FROM Products
WHERE ItemID NOT IN (
SELECT ItemID
FROM Orders
WHERE OrderID = X
)
ORDER BY ProductName
SELECT Products.ItemID, Products.ProductName
FROM Products
WHERE Products.ItemID not in (select Orders.ItemID from Orders where Orders.OrderNumber = xxx)
ORDER BY Products.ProductName
What you need can be easily accomplished just by adding the order number in the join condition:
SELECT Products.ItemID, Products.ProductName
FROM Products
LEFT JOIN Orders
ON (Orders.ItemID = Products.ItemID AND OrderNumber = 52)
WHERE Orders.ItemID IS NULL
ORDER BY Products.ProductName