Update column with a subquery - sql

ALTER TABLE order_t ADD Totalfixed DECIMAL(7,2);
UPDATE Order_t
SET Totalfixed = (
SELECT orderid, SUM(price * quantity) AS tf
FROM
orderline ol,
product p
WHERE
ol.productid = p.productid
AND ol.orderid = orderid
GROUP BY orderid
);
Everything works fine separately but I get:
operand should contain 1 column
And if I remove orderid from the subquery, I get:
subquery returns more than 1 row
Is there anyway to make this work without a join?

Regardless of the database, the context requires a scalar subquery. This means avoid the group by and return only one column:
UPDATE Order_t
SET Totalfixed = (
SELECT SUM(price * quantity) AS tf
FROM orderline ol JOIN
product p
ON ol.productid = p.productid
WHERE ol.orderid = Order_t.orderid
);
I also fixed the JOIN syntax (always use explicit joins) and the correlation clause so it refers to the outer query.

UPDATE A
SET Totalfixed = SUM(price * quantity)
FROM Order_t A
INNER JOIN orderline ol ON ol.orderid = A.orderid
INNER JOIN product p ON ol.productid = p.productid

Related

Update Inventory table using INNER JOIN sql

I want to update my Inventory table using INNER JOIN
what need to be done is get [JoinedTable.NumOrder] with current time.
tables :
Inventory(ProductID, ProductName, price, OnHand)
Orders(OrderID, OrderDate, CustomerID)
OrderLine(OrderID, ProductID, NumOrdered)
UPDATE Inventory
INNER JOIN (SELECT OrderLine.ProductID, SUM(OrderLine.NumOrdered) AS NumOrdered
FROM Orders, OrderLine
WHERE Orders.OrderID = OrderLine.OrderID
AND Orders.OrderDate = Date()
GROUP BY OrderLine.ProductID) AS Add
ON Inventory.ProductID = Add.ProductID
SET Inventory.OnHand = Inventory.OnHand + Add.NumOrdered
;
this query gives Operation must use an updateable query error.
Try this out.
UPDATE Inventory
SET Inventory.OnHand = Inventory.OnHand + Add.NumOrdered
FROM INVENTORY
INNER JOIN (SELECT OrderLine.ProductID, SUM(OrderLine.NumOrdered) AS NumOrdered
FROM Orders, OrderLine
WHERE Orders.OrderID = OrderLine.OrderID
AND Orders.OrderDate = Date()
GROUP BY OrderLine.ProductID) AS Add
ON Inventory.ProductID = Add.ProductID;

How to create a function that totals an order with several items?

I need to create a function which will return the total of an order. I've been given three tables with the following variables
Table 1 - Order
Order_ID
Date_Placed
Date_Fulfilled
Table 2 - Order Product
Order_ID
Product_ID
Product_Quantity
Table 3 - Product
Product_ID
Price
I'm struggling to put together a coherent function. Any help would be greatly appreciated.
I've already attempted to set up the function with joins between both tables, but am unable to figure out where I should be putting my equation.
BEGIN
SELECT order.order_id, SUM(product.price * order_item.quantity)
FROM `order`
JOIN `order_item` ON order.order_id = order_product.order_id
JOIN `product` ON order_product.product_id = product.product_id;
END $$
You might be surprised, but the orders table is not needed for this query. You can just aggregate off the other two tables:
SELECT oi.order_id, SUM(p.price * oi.quantity)
FROM order_item oi JOIN
product p
ON po.product_id = p.product_id
GROUP BY oi.order_id;
You'll need to take your select statement, and group it by your order.order_id. That way you'll have one row per order, with the sum total of that order.
SELECT order.order_id, SUM(product.price * order_item.quantity) as total_price
FROM `order`
JOIN `order_item` ON order.order_id = order_product.order_id
JOIN `product` ON order_product.product_id = product.product_id
GROUP BY order.order_id
this will work:
SELECT order.order_id, SUM(product.price * order_item.quantity)
FROM order o,
JOIN order_item oi,
JOIN product p where
o.order_id = oi.order_id and
oi.product_id = p.product_id
group by order_product.product_id = product.product_id;

Update based on the results of previous update statement

We had a requirement to update price values in the Orders table based on the productids from the Products table. This is simple:
update o
set o.price = p.price
from Orders o
inner join Products p
on o.productid = p.productid
Then the ask was to update the price from a different table (OrderPricing) based on a different column (orderpricingid) for the orders that were not updated by the previous query.
So I have another update statement:
update o
set o.price = op.price
from Orders o
inner join OrderPricing op
on o.orderpricingid = p.orderpricingid
where o.price is null
So my question is - can the two update statements be combined together? Is there a way to update based on the results of the previous update query?
You can do both in the same query, if you change the joins from inner to left, using coalesce:
update o
set o.price = coalesce(p.price, op.price, o.price)
from Orders o
left join Products p on o.productid = p.productid
left join OrderPricing op on o.orderpricingid = p.orderpricingid
This will update the price from the products table if the product exists there, if not, it will use the price from the OrderPricing table, but if that also don't exists it will just keep the original price.
You can try this way, as if o.price is null then you will update from order price so we can use case when in time of update
update o
set o.price= case when p.price is null then op.price else p.price end
from Orders o
inner join Products p
on o.productid = p.productid
inner join OrderPricing op
on o.orderpricingid = p.orderpricingid
I believe you need a left join:
update o
set o.price = coalesce(p.price, op.price)
from Orders o left join
Products p
on o.productid = p.productid left join
OrderPricing op
on o.orderpricingid = p.orderpricingid;

How to multiply 2 columns and then SUM the results?

I'm learning SQL and I couldn't find the solution to my problem anywhere on he forums. Anyway, I'm using a www.dofactory.com SQL Sandbox and I've made a query:
select customer.LastName, Product.ProductName, OrderItem.Quantity, Product.UnitPrice, OrderItem.Quantity*Product.UnitPrice AS "Quantity x UnitPrice"
FROM Customer
JOIN [Order] ON [Order].CustomerID = Customer.ID
JOIN OrderItem ON OrderItem.OrderId = [Order].ID
JOIN Product ON Product.Id = OrderItem.ProductId
where
[Order].TotalAmount = (select max([Order].TotalAmount) FROM [Order])
And result:
LastNam ProductName Quantity UnitPrice Quantity x UnitPrice
Kloss Côte de Blaye 60 263.50 15810.00
Kloss Chartreuse verte 80 18.00 1440.00
Now I want to SUM the whole Column "Quantity x UnitPrice". What should I do?
You use SUM(). Something like this:
SELECT SUM(oi.Quantity * oi.UnitPrice) AS Summary
FROM Customer c JOIN
[Order] o
ON o.CustomerID = c.ID JOIN
OrderItem oi
ON oi.OrderId = o.ID JOIN
Product p
ON p.Id = oi.ProductId
WHERE o.TotalAmount = (select max(o2.TotalAmount) FROM [Order] o2);
Note the use of table aliases. They make the query easier to write and to read.
This is SQL Server (T-SQL). You can use SUM and GROUP BY like so. I've done it in a subquery, which is how I'd approach this sort of thing.
select LastName, ProductName, Quantity, UnitPrice, SUM(Summary)
from (
select customer.LastName, Product.ProductName, OrderItem.Quantity, Product.UnitPrice, OrderItem.Quantity*OrderItem.UnitPrice AS Summary, [Order].TotalAmount
FROM Customer
JOIN [Order] ON [Order].CustomerID = Customer.ID
JOIN OrderItem ON OrderItem.OrderId = [Order].ID
JOIN Product ON Product.Id = OrderItem.ProductId
where
[Order].TotalAmount = (select max([Order].TotalAmount) FROM [Order])) x
group by LastName, ProductName, Quantity, UnitPrice

Select order products in MySQL from muliple tables

I have 3 tables:
orderProducts (orderId, productId, productValue),
products (productId, productName)
values (productId, productValue, productValuePrice).
I need to select order products (product id, name, value, price) with defined orderId.
How to do that with one MySQL query?
You could use a left join to return rows in the orderProducts table for which no corresponding rows in the other tables exist. Any missing columns will be NULL, allowing you to flag an error condition. The default join is an inner join, and will only return rows with matching entries in both joined tables.
select op.product id, p.name, v.productValue, p.productValuePrice
from orderProducts op
left join products p on p.productId = op.productId
left join values v
on v.productId = op.productId
and v.productValue = op.productValue
where op.orderId = <YourOrderId>
You can do this via a join, something like:
Select o.orderId, p.productId, p.name, o.productValue, v.productValuePrice
From products p
Join values v on p.productId = v.productId
Join orderProducts o on p.productId = o.orderId
Where orderId = 5
.......
select p.productId, p.productName, o.productValue, v.prodctValuePrice from orderProducts o join products p on o.productId = p.productId join values v on v.productId = p.productId where p.productId = 1