MySQL Join two tables and sum? - mysql

I have 2 table - orders and orderdetail.
I need to join them together and show the total. I can join them but how do I calculate the total?
SELECT *
FROM Orders as o
INNER JOIN OrderDetails as od on o.order_id = od.order_id
WHERE o.table_id = 1

select a.order_id,
order_date,
order_status,
table_id,
item_id,
item_price,
quantity,
b.item_price*b.quantity as total
from dbo.orders as a inner join dbo.orderdetails as b
on a.order_id=b.order_id
WHERE table_id = 1
Update : as you mentioned in comment if you want a computed column which automatically multiply quantity and item_price, then you need
execute this query :
ALTER TABLE dbo.orderdetails ADD Total AS (quantity* item_price);

Related

Updating table with select sum from another table

I have two tables: orders and order_items.I need to update the column xpto_spent on orders table with the sum of the total spent with items of the brand XPTO (items described in order_items table).
My current query is returning timeout from mysql server. The timetou is set to 28800 seconds.
UPDATE orders
SET orders.xpto_spent = (
select
format( sum(total), 2) as xpto_spent
from order_items
where order_items.brand = "XPTO"
AND orders.order_id = order_items.order_id
group by order_items.order_id
);
Any help will be appreciated! Thank you!
You can join the table orders to the query that returns all the sums from order_items:
UPDATE orders o
INNER JOIN (
SELECT order_id, FORMAT(SUM(total), 2) AS xpto_spent
FROM order_items
WHERE brand = 'XPTO'
GROUP BY order_id
) t ON o.order_id = t.order_id
SET o.xpto_spent = t.xpto_spent
You would generally do this using join, but you can use a correlated subquery:
UPDATE orders o
SET o.xpto_spent = (SELECT SUM(oi.total)
FROM order_items oi
WHERE oi.brand = 'XPTO' AND
oi.order_id = o.order_id
);
For this query, you want an index on order_items(order_id, brand, total). That will probably speed your query.

SELECT a column and SUM() of values from another table in SQL

I'm pretty new with SQL, and this is giving me trouble. The idea is that I have several tables. Here are the relevant tables and columns:
customers:
customer_id, customer_name
orders:
order_id, customer_id
orderline:
order_id, item_id, order_qty
items:
item_id, unit_price
I need to return customer_name as well as total revenue from that customer (calculated as item_price * order_qty * 2).
Here's what I have written:
SELECT customers.customer_name, sum(revenue)
FROM SELECT orderline.order_qty * items.unit_value * 2 AS revenue
FROM orderline
INNER JOIN orders
ON orderline.order_id = orders.order_id
INNER JOIN customers
ON revenue.customer_id = customers.customer_id;
This throws a syntax error and I'm not really sure how to proceed.
This is only one example of this type of problem that I need to work out, so more generalized answers would be helpful.
Thanks in advance!
EDIT:
With help from answers I ended up with this code, which just gets total revenue and puts it next to the first person in the DB's name. What did I get wrong here?
SELECT customers.customer_name, sum(revenue)
FROM(SELECT orderline.order_qty * items.unit_price * 2 AS revenue, orders.customer_id AS CustomerID
FROM( orderline
INNER JOIN orders
ON orderline.order_id = orders.order_id
INNER JOIN items
ON orderline.item_id = items.item_id)) CustomerOrders
INNER JOIN customers
ON CustomerOrders.CustomerID = customers.customer_id;
A couple issues with your query.
First, you need to scope your subquery and alias it:
(SELECT orderline.order_qty * items.unit_value * 2 AS revenue
FROM orderline
INNER JOIN orders
ON orderline.order_id = orders.order_id) CustomerOrders
Secondly, you need to select more than the revenue in the subquery since you are joining it to your customers table
(SELECT
orderline.order_qty * items.unit_value * 2 AS revenue,
orders.customer_id AS CustomerId
FROM
orderline
INNER JOIN orders ON orderline.order_id = orders.order_id) CustomerOrders
Then you need to use the subquery alias in the join to the customers table and wrap it all up in a group by customer_id and CustomerOrders.Revenue
I would tend to do it differently. I'd start with selecting from the customer table, because that is the base of what you are looking for. Then I'd do a cross apply on the orders that would all aggregating the order revenue in the subquery. It would look like this (tsql, you could do the same in mysql with a join with some aggregation):
SELECT
customers.customer_name,
ISNULL(customerOrders.Revenue, 0) AS Revenue
FROM
customers
OUTER APPLY (
SELECT
SUM (orderline.order_qty * items.unit_value * 2) AS Revenue
FROM
orders
INNER JOIN
orderline ON orders.order_id = orderline.order_id
INNER JOIN
items on orderline.item_id = items.item_id
WHERE
orders.customer_id = customers.customer_id
) CustomerOrders
In this case, the subquery aggregates all your orders for you and only returns one row per customer, so no extraneous returned data. Since it's an outer apply, it will also return null for customers with no orders. You could change it to a CROSS APPLY and it will filter out customers with no orders (like an INNER JOIN).
SELECT c.customer_name,
sum(COALESCE(ol.order_qty,0) * COALESCE(i.unit_value,0) * 2)
FROM customers c
INNER JOIN orders o
ON o.customer_id = c.customer_id;
INNER JOIN orderline ol
ON ol.order_id = o.order_id
INNER JOIN items i
ON i.item_id = ol.item_id
GROUP BY c.customer_id
select customer_name, sum(item_price * order_qty * 2) as total_revenue
from (
select * from customers
inner join orders using(customer_id)
inner join orderline using(order_id)
inner join items using(item_id)
)
group by customer_name
select
c.customer_name,
r.revenue
from
customers c
inner join
orders ord on
ord.customer_id = c.customer_id
inner join
(select i.item_id, o.order_id, sum(o.order_qty * items.unit_value * 2) as revenue
from orderline o
inner join items i on
i.item_id = o.item_id
group by o.order_id, i.item_id) as r on r.order_id = o.order_id

Mysql inner query to get sum of amount

I have three tables which are interlinked :
1) First is order table that contains 2 columns, id and vendor_id
2) Second table is order_products that contains product detail of order table and has columns order_id (foreign key of id from order table) and product_id.
3) Third table is vendors_product that contains prices of products for different vendor like vendor_1 has $10 for product_a and vendor_2 has $20 for product_a so each vendor has different prices of same products. This table has columns, vendor_id (foreign key of vendor_id from order table), product_id (foreign key of product_id from order_products table) and product_amount columns
Now I want to get the sum of product_amount for all order and should be based on vendor of each order.
I tried this by using below query but I couldn't get the result
SELECT
a.id, a.vendor_id, (
SELECT
SUM(product_amount)
FROM
vendors_product
WHERE
vendor_id = a.vendor_id
AND product_id IN (
SELECT
product_id
FROM
order_products
WHERE
order_id = a.id
)
) as total_price
FROM
`order` a
Can somebody help me out ???
Try this:
SELECT o.id, o.vendor_id, SUM(product_amount) product_amount
FROM `order` o
INNER JOIN order_products op ON o.id = op.order_id
INNER JOIN vendors_product vp ON a.vendor_id = vp.vendor_id AND op.product_id = vp.product_id
GROUP BY o.id, o.vendor_id;
Try this:
SELECT a.id, a.vendor_id, SUM(product_amount) AS 'product amount'
FROM `order` a
INNER JOIN order_products vp1 ON a.id = vp1.order_id
INNER JOIN vendors_product vp2 ON a.vendor_id = vp2.vendor_id AND vp1.product_id = vp2.product_id
GROUP BY a.id, a.vendor_id

sum sql group by values from another table

I've got 2 tables: Online Orders, Online orders details
I have an issue on how to calculate the total amount of the order in Online Order table based on the detail order table.
The detail table looks like:
id_order Id_product quantity price value
1 2 1 3 3
1 3 2 2 4
2 1 1 5 5
I would like to sum all the values from an id_order and insert them into the total amount of the order in the Online orders table.
Can you help me with the SQL command?
It's not clear what database system you use.
If you want to UPDATE orders.total_amount
This update statement will work under any DB:
update orders
set total_amount = (
select SUM(value)
from orders_details
where id_order = orders.id
)
where EXISTS(select *
from orders_details
where id_order = orders.id)
This update statement works under MySQL:
update orders u
inner join (select id_order, SUM(value) as total
from orders_details
GROUP BY id_order) s on
u.id = s.id_order
set u.total_amoun = s.total
Is this what you want?
SELECT o.id_order, SUM(quantity * price) AS total_price
FROM online o
INNER JOIN detail d
ON d.id_order = o.id_order
GROUP BY o.id_order

SQL query for 3 tables

I have 3 tables as follows
item{id, name, price}
customer{id, name, tel_no}
order{id, time, customer_id}
order_item{id, item_id, price, order_id}
process{id, order_item_id, status}
I need to get order_items which are not processed for a particular customer. I tried with the following query. But it doesn't help. Please correct me some one.
SELECT *
FROM order_item`
INNER JOIN `order` ON `order`.id = order_item.order_id
WHERE `order`.customer_id=1 AND NOT EXISTS (
SELECT *
FROM process
WHERE process.order_item_id=order_item.id
)
I'm using mysql as my server
Select *
from order_item OI
INNER JOIN ORDER O on O.ID=OI.Order_Id
LEFT JOIN Process P ON P.Order_item_ID = OI.Item_ID
where O.Customer_ID = 1 and P.ID is null
LEFT JOin gives you all the ORDER_Items and only those records with a matching process record so P.ID will be null thus the item has not been processed