Sql query to calculate total cost using 3 tables - mysql

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;

Related

How to count grouped records that match multiple criteria

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

MySql - How to get a sum of a particular column by joining three tables along with filters

There are three tables
Inventory - Which contains the stock information
Order - Which contains the store information of an order
OrderParts - Which contains the Parts associated with the order
Inventory Table
Order Table
Order Parts Table
Below Expected Report needs to be generated
Query Tried to achieve the above result
SELECT
inventory.partNumber, inventory.storeId, SUM(parts.quantity) as orderedQuantity,
inventory.availableQuantity
FROM
(SELECT *, SUM(availableQuantity) AS availQty
FROM
inventory AS inventory
GROUP BY storeId , partNumber) AS inventory
LEFT JOIN
(SELECT
*
FROM
order
GROUP BY storeId) AS order ON order.storeId = inventory.storeId
LEFT JOIN
(SELECT
*
FROM
orderParts) AS parts ON inventory.partNumber = parts.partNumbe
where inventory.partNumber in ("A1234", "B1234");
GROUP BY order.storeId , parts.partNumber
Getting null for Ordered Quantity
Kindly let me know the way to get the cumulative Ordered quantity as expected. Thanks in Advance.
A relatively simple method is union all and aggregation:
select storeid, partnumber,
sum(availableqty) as availableqty,
sum(orderedqty) as orderedqty
from (select storeid, partnumber, availableqty, 0 as orderedqty
from inventory
union all
select o.storeid, op.partnumber, 0, op.quantity
from orderparts op join
orders o
on op.ordernumber = o.ordernumber
) sp
group by storeid, partnumber;
Your question doesn't explicitly describe the filters, but you can add filters in either the subqueries or the outer query.

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

Issue with sql query to get the results

I am having a problem with a sql query. This is how the table structure looks like.
The scenario is there are order details in order table(Initially shipping details id is null) and ordered items are also saved accordingly. When dispatching the orders the driver can select one or more orders. Assume if he selects two orders('CC0-C1B-50B-63B' and 'FB2-FC6-57B-DD8') a new record is added to the shipping_details table(so the no of places is 2 in our case) and two records are saved accordingly in Delivery_Place table.
What I want : If I select order No 'CC0-C1B-50B-63B', I want to get all the delivery orders(In our case there is another order which is 'FB2-FC6-57B-DD8'and the result should be those 2 orders) and the departmentID in orderd_items table for those 2 orders. (If there are multiple items in a single order, all items are in a same department)
So I tried this query,
select oi.FKOrderID,dp.deliveryPlaceID,dp.FKShippingDetailsID,dp.city,dp.position,oi.FKDepartmentID
from
`order` o join `shipping_details` sd
on o.FKShippingDetailsID = sd.shippingDetailsID
join `ordered_items` oi
on o.orderID = oi.FKOrderID
join `delivery_places` dp
on dp.FKShippingDetailsID=sd.shippingDetailsID
where o.orderID = 'CC0-C1B-50B-63B'
group by dp.deliveryPlaceID
order by dp.position asc ;
And the result is this,
But I for second row I should get 'FB2-FC6-57B-DD8' as FKOrderID and '11' for the FKDepartmentID.
This is the data table
UPDATED WITH INPUT DATA
Order Table
Shipping Details Table
Ordered_items table (columns: orderedItemsID , FKItemID , quantity , size, FKDepartmentID , FKOrderID)
Department Table
So how to modify this query to get that result?
Thanks.
This is how I achieved the result ,
select
distinct(oi.FKOrderID),dp.deliveryPlaceID,dp.FKShippingDetailsID,dp.city,dp.longitude,dp.latitude,dp.timestamp,dp.position,dp.status,o.orderStatus,oi.FKDepartmentID
from
`ordered_items` oi,
`order` o join `shipping_details` sd
on o.FKShippingDetailsID = sd.shippingDetailsID
join `delivery_places` dp
on dp.FKShippingDetailsID=sd.shippingDetailsID
where o.orderID = 'CC0-C1B-50B-63B'
and
oi.FKOrderID in (select orderID from `order` where
FKShippingDetailsID=o.FKShippingDetailsID)
group by oi.FKOrderID
order by dp.position asc
And here is the result,

How to get all order ID which not paid in SQL Server 2008?

I want to get all order id numbers for selected customer which not paid till now, my data show as following:
What I want is Write a SELECT statement that answers this question:
select orderID
from order
where customer id = #custID
and Total cashmovementValue
for current order id
is less than total (sold quantity * salePrice )
for current order id
How to do it?
Thanks.
You need to compare the sum of each order line with the sum of each payment per order. GROUP BY and a few sub-queries is what you need to get the job done.
Something like this should work:
SELECT
O.OrderID
FROM [Order] O
INNER JOIN (
-- Add up cost per order
SELECT
OrderID,
SUM(SoldQuantity * P.SalePrice) AS Total
FROM OrderLine
INNER JOIN Product P ON P.ProductID = OrderLine.ProductID
GROUP BY OrderID
) OL ON OL.OrderID = O.OrderID
LEFT JOIN (
-- Add up total amount paid per order
SELECT
OrderID,
SUM(CashMovementValue) AS Total
FROM CashMovement
GROUP BY OrderID
) C ON C.OrderID = O.OrderID
WHERE
O.CustomerID = #custID
AND ( C.OrderID IS NULL OR C.Total < OL.Total )
EDIT
I've just noticed you're not storing the sale price on each order line. I've updated my answer accordingly, but this is a very bad idea. What will happen to your old orders if the price of an item changes? It is okay (and actually best practice) to denormalise the data by storing the price at the time of sale on each order line.