Write an SQL statement to show the warehouse information for warehouses which do not have all SKU items in inventory
I tried this query but results: NULL
SELECT SKU
FROM SKU_DATA
WHERE NOT EXISTS
(SELECT 1 FROM INVENTORY
JOIN WAREHOUSE ON INVENTORY.WAREHOUSEID = WAREHOUSE.WAREHOUSEID
WHERE SKU_DATA.SKU =INVENTORY.SKU);
Here the different tables available
Inventory (WarehouseID,SKU,SKU_Description,QuantityOnHand,QuantityOnOrder)
Order_Item(OrderNumber,SKU,Quantity,Price,ExtendedPrice)
Retail_order(OrderNumber,StoreNumber,StoreZip,OrderMonth,OrderYear,OrderTotal)
SKU_Data(SKU,SKU_Description,Department,Buyer)
Warehouse(WarehouseID,WarehouseCity,WarehouseState,Manager,SquareFeet)
enter image description here
Probably the easiest way will be just to count the number of unique SKUs in the SKU_Data, then select all the WarehouseID from the Inventory that have less than that number of unique SKUs.
Critically, this assumes a warehouse doesn't have a SKU that is missing from the SKU table.
Also assuming Inventory(WarehouseID,SKU) & SKU_Data(SKU) are unique
select WarehouseID from Inventory group by SKU
having count(*) < (select count(*) from SKU_Data)
I've not tested this, but I think it's about right :)
If this doesn't work, you can always select the count(*) into a local variable, something like
select #skunum := count(*) from SKU_Data
select WarehouseID from Inventory group by SKU having count(*) > #skunum
If you want to also check the warehouse has the item in stock, just add where QuantityOnHand > 0, after from Inventory
Related
I have a table of inventory items (holds description, details of item etc.), a table of stock (physical items that we have - items of inventory), and a suppliers table (who supply the stock, but may differ from time to time).
Suppliers -- Stock -- Inventory
Inventory has many stock. Suppliers have many stock. Stock has one supplier, and one inventory
I'm trying to run a query to get all data from inventory, and count how many suppliers it has through a sub query. However, I need to use SELECT *
What I have at the moment:
SELECT
( SELECT COUNT(DISTINCT SupplierID)
FROM Stock
WHERE Stock.InventoryID = Inventory.ID
) AS Suppliers
, *
FROM `Inventory`;
I've tried variations on this, swapping the field order (seen this elsewhere on this site), changing the sub-query etc.
However, it tells me there's an error near '* FROM'. Can anyone suggest a way to do this query please?
Use table aliases:
SELECT (SELECT COUNT(DISTINCT s.SupplierID)
FROM Stock s
WHERE s.InventoryID = i.ID
) AS Suppliers, i.*
FROM `Inventory` i;
The need for a qualification on * is described in the documentation:
Use of an unqualified * with other items in the select list may
produce a parse error. To avoid this problem, use a qualified
tbl_name.* reference
SELECT AVG(score), t1.* FROM t1 ...
I have two tables named order and orderdetail with a relation on key order_id
I want to query the database to fetch a grand total per order.
The order table has fields order_id, customer_name etc.
The order_detail table has fields id (primary key), order_number (foreign key), product, qty, price
I would like a result with fields like this:
order_id, grand_total.
I created a view for order_detail like so:
CREATE OR REPLACE VIEW orderTotal AS
SELECT
order_number,
Round(sum(qty * price),2) as grandTotal
FROM order_detail t group by order_number;
It works pretty well, but there is an issue. Some orders has no reference data in order_detail, and then that record is not listed by this view. How can I solve this? Please give me a sample query.
It may need multiple SQL queries, but can we build it as a view?
As an example, I need data like this:
order_id grandTotal
1 200
2 0 // when data is missing
3 150.35
If you have parent records and no children, it could help you:
SELECT
order.order_id,
coalesce(Round(sum(order_detail.qty * order_detail.price),2),0) as grandTotal
FROM order
LEFT join order_detail on order_detail.order_number = order.order_id
GROUP BY order.order_id
Pretty new to sql statements here so this one is a bit tricky for me.
I have two tables: sold and product. Sold(SID, UPC, Date) Product(UPC, Name, Brand)
I need to find in how many stores(sid) does one brand outsell another brand.
I was thinking it was something like:
select count(*) from sold natural join product
where count(brand = 'sony') > count(brand = 'samsung');
Clearly that isn't valid however...
SELECT COUNT(*)
FROM (
SELECT SID
FROM Sold
JOIN product
ON product.UPC = Sold.UPC -- otherwise we have a cartesian product
GROUP BY SID -- if we need totals _by store_, we need to group on that.
HAVING SUM(brand='sony') > SUM(brand='samsung')
) Totals.
I have a database in phpmyadmin for all my suppliers. I have created a php file that exports to csv on a daily basis the cheapest stock and quantity of each unique sku in the database. At the moment however, it is excluding any item that has no stock anywhere, however, i would like that... if there is no stock anywhere to print 0 for those items.
The original sql statement is (where name=SKU):
SELECT name,MIN(price) AS minPrice,quantity FROM products WHERE quantity > 0 GROUP BY name
I tried already the following, but this does not work as intended:
SELECT name,MIN(price) AS minPrice,IF(quantity > 0,quantity,'0') AS quantity FROM products GROUP BY name
You want conditional aggregation:
SELECT name, MIN(price) AS minPrice, coalesce(sum(quantity), 0) AS quantity
FROM products
GROUP BY name;
The problem with your query is the where clause which filters out all products where quantity is 0. You seem to actually want these.
I've included the coalesce() just in case quantity can be NULL. It may not be needed for your actual data.
If you're looking for the quantity of the product with the lowest price, try:
SELECT name
, price
, coalesce(quantity,0) as quantity
FROM products p1
WHERE price =
(
SELECT min(price)
FROM products p2
WHERE p1.name = p2.name
)
I have 3 tables:
products (sku, price, priceOffer, etc)
stock (sku, branch, items)
sales_provider (sku, items, date)
The table products holds all the information about a product, except for stock or sales. Current stock is stored in the table stock, and has information about how many items are available on each branch. The table "sales_provider" stores how many items were sold each day for every product. The product ID is "sku".
Now, I'm trying to get with one query the product that:
Has generated the best profit (number of sales * offered price)
Is still available on stock
And, of course, I want to know how many items are still on stock and how many items were sold.
I'm trying a query like this:
select
*
from
(
select
p.*,
sum(s.items) stock,
sum(sp.items) sales,
case when
p.priceOffer < p.price
and
p.priceOffer > 0
then
p.priceOffer
else
p.price
end finalPrice
from
products p
join
stock s
on
s.sku = p.sku
join
sales_provider sp
on
sp.sku = p.sku
group by
sku
) temp
where
stock > 0
order by
(finalPrice * sales) desc
limit 1;
But I'm having problems with that. Basically, I'm getting a huge sum of stock items ans sales_provider items, not the real amounts. Also, it's a slow query (it's taking about half a second with only 9,500 products).
I've been trying to modify it and I'm having doubts about the subquery being necessary, but I just can't nail it.
If someone can help me improve it and get the correct result, I'll really appreciate it.
Thanks in advance for any helpful comment.
Francisco
For this type of query, you want to do the aggregations separately on stock and sales_provider. Otherwise, you will generate a cartesian product between the two tables for a given item.
Try this:
select p.sku, (salesitems*offeredprice) as profit, stockitems, salesitems
from products p left join
(select sku, SUM(items) as stockitems
from stock
group by sku
) s
on p.sku = s.sku left join
(select sku, SUM(items) as salesitems
from sales_provider sp
group by sku
) sp
on p.sku = sp.sku
where p.stockitems > 0
order by profit desc
This assumes that product(sku) is unique.