I want to select rows from a table (products), but that table is linked to another one (stock) using JOIN statement.
In products table there are 2 important fields, SKU and minimum_stock.
In stock table there are SKU (relation with products) and qty
Now, I want to select rows from products only if in table stock exists SKU, and qty is > minimum_stock from products.
Like this:
SELECT *
FROM products
INNER JOIN stock ON products.SKU = stock.SKU
WHERE products.minimum_stock.value > stock.qty.value;
Obviously this code not works.
Can somebody help me?
Thanks in advance.
If your tables look like:
products:
SKU
minimum_stock
(... other columns ...)
stock
SKU
qty
(... other columns ...)
Your query doesn't have to have the .value at the end of the columns. In fact, that's what would make this not work.
Do something like:
SELECT *
FROM products
INNER JOIN stock ON products.SKU = stock.SKU
WHERE products.minimum_stock > stock.qty;
SQL Fiddle Demo
Related
I have 3 tables, for the sake of this exercise we'll call them: Products, Price, and Discount. I'm trying to join Products and Price tables, only if the ProductID is found in Discount.ProductID (ProductID column within the Discount table).
Products:
ProductID
Size
Color
Ref#
A1234
Small
Blu
0C94
B5678
Med
Red
1D96
Price:
Ref#
Base
Tax
0C94
3.48
0.96
Discount:
ProductID
List
Site
A1234
Two
Three
I'm familiar with joins, so my code starts off as:
SELECT * FROM Product as a
left join Price as b
on a.Ref# = b.Ref#
but I've never nested a constraints within a where clause (if that's even the correct approach) based on a third table. Any advice would be greatly appreciated. The end result would be a new products table that only shows the one product, because ProductID B5678 is not in the Discount table.
Just do a 3-table join.
SELECT DISTINCT a.*, b.*
FROM Product AS a
JOIN Price AS b ON a.`Ref#` = b.`Ref#`
JOIN Discount AS c ON a.ProductID = c.ProductID
If you don't need any of the contents of the Discount table, use the exists() funtion to execute a sub query in the where clause. This will give you the fastest results.
SELECT *
FROM Product as a
left join Price as b on a.Ref# = b.Ref#
WHERE EXISTS (
SELECT *
FROM Discount as c
WHERE c.ProductID = a.ProductID
)
If however you do need one or more of the columns of Discount, do an inner join between Product and Discount, joining them on the ProductID. This will result in only the products that have discount, and then do another left join to Price to get the columns from Price into the resultset too. Do be aware though that in case multiple rows exist in Discount for the one Product row, this will result in the same product shown on multiple rows.
SELECT *
FROM Product as a
inner join Discount as c on c.ProductID = a.ProductID
left join Price as b on a.Ref# = b.Ref#
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
I have two table in the PostgreSQL DB which looks like
Select Productid,productname,quantity,availabilty from public.product
And other table is
Select sampleid,samplename,Productid from public.sample
So here I need to select the Products and their sample from the product table and sample table where Productid in the Product table is equal to the productid in the sample table. Should I be using the Joins here.I am confused with the Left and right join
I need to select the Products and their sample
I think you can use LEFT JOIN here to get all products and matching samples if any:
select p.*,
s.sampleid,
s.samplename
from public.product p
left join public.sample s on s.Productid = p.Productid;
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
i need to create a trigger that multiply two fields from two tables, but i have no idea of how to do it, so let's see if you can help me.
Two tables,
Products (product_name, price)
Orders (product_name (foreign key),units_ordered)
I need to add another field on table the Orders that multiply price from Products and units_ordered from Orders, so : total_price = price (from products) X units_ordered (from Orders)
Thanks in advance, and sorry for my bad english.
Regards
You don't need trigger at all. And also you don't need to add another column for the total price as it is already redundant.
If you want their total prices, just do it during the projection of records. Example
SELECT a.Product_Name,
a.Price,
b.units_ordered,
a.Price * b.units_ordered AS TotalPrice
FROM Products a
INNER JOIN Orders b
ON a.Product_name = b.Product_name
or you can create a VIEW out of your SELECT statement. Example,
CREATE VIEW ProductOrder
AS
SELECT a.Product_Name,
a.Price,
b.units_ordered,
a.Price * b.units_ordered AS TotalPrice
FROM Products a
INNER JOIN Orders b
ON a.Product_name = b.Product_name
and selecting from the view,
SELECT * FROM ProductOrder
But if you really want to add another column, still trigger is not an option. You only need to update the values for that column using UPDATE and joining of the two tables. Assuming your new column is called TotalPrice on table `Orders.
UPDATE Orders a
INNER JOIN Products b
ON a.Product_name = b.Product_name
SET a.TotalPrice = a.units_ordered * b.Price