I'm using MySQL, and I have an orders table and an order_items table. An order can have many order_items. The foreign key in order_items is orderId. In my database there are 26 orders and 69 order_items. I want to get all orders, while joining to the order_items table - but only joining to one order_item for each order. So the result should be 26 rows.
select o.*, oi.amount from orders o
JOIN order_items oi on o.id = (select orderId from order_items where orderId =
o.id group by orderId);
orders Table:
id
email
total
createdTimestamp
order_items table:
id
orderId
amount
status
Somehow, this is giving me 1794 rows back when I expect 26, and I am drawing a complete blank at to what is going on.
It does not mater which order_item row is joined to for each order.
EDIT
This will work:
select o.*, oi.amount from order_items oi
join orders o on o.id = oi.orderId
group by oi.orderId;
But how can it be done by using the orders table in the from and joining to order_items?
If you want one item per order, then use window functions:
select o.*, ol.*
from orders o join
(select ol.*,
row_number() over (partition by orderId order by rand()) as seqnum
from order_items oi
) oi
on o.id = oi.orderId and seqnum = 1;
Related
I have two different queries that each do half the job I need. how to combine them.
orders table has orderNumber and customerNumber, customers table has customerNumber and salesRepEmployeeNumber, orderdetails has multiple lines of the same orderNumber each showing price&quantity of different items).
(counting the number of orders from different customers each sales rep has)
select c.salesRepEmployeeNumber, count(*)
from customers c
inner join orders o1
on c.customerNumber = o1.customerNumber
group by c.salesRepEmployeeNumber;
and
(counting the revenue made by each sales rep)
select c.salesRepEmployeeNumber, sum(o2.priceEach*o2.quantityOrdered) as "Revenue"
from customers c
inner join orders o1
on c.customerNumber = o1.customerNumber
inner join orderdetails o2
on o1.orderNumber = o2.orderNumber
group by c.salesRepEmployeeNumber;
I need a query to know the employee number, # of orders, and revenue. I tried
select sum(o2.priceEach*o2.quantityOrdered) as "Revenue", c.salesRepEmployeeNumber, count(*)
from customers c
inner join orders o1
on c.customerNumber = o1.customerNumber
inner join orderdetails o2
on o1.orderNumber = o2.orderNumber
group by c.salesRepEmployeeNumber;
but it returns the count of items/products from the orders (e.g. 1 order has three products)
SELECT salesRepEmployeeNumber,
t1.`count(*)` AS `count`,
t2.Revenue
FROM (complete text of 1st query) AS t1
LEFT JOIN (complete text of 2nd query) AS t2 USING (salesRepEmployeeNumber)
complete text means "without final semicolon".
If your data guarantees that the amount of rows produced by 2nd query is equal to one produced by 1st query then you may use not LEFT but INNER joining.
Also test
select c.salesRepEmployeeNumber
, COUNT(DISTINCT o1.id) AS orders_count
, sum(o2.priceEach*o2.quantityOrdered) as "Revenue"
from customers c
join orders o1
on c.customerNumber = o1.customerNumber
join orderdetails o2
on o1.orderNumber = o2.orderNumber
group
by c.salesRepEmployeeNumber;
where o1.id is primary key expression (or any unique non-NULL column/expression/index) of orders 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.
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);
I am trying to show the average price of products bought by customers from ‘Tucson’, however this query returns null even though there are two customers that have placed orders from Tuscon.
select AVG(product_price) from product where product_id in
(select product_id from orderline where order_id in
(select order_id from ordertable where cust_id in
(Select cust_id from customer where city = 'Tuscon')))
You're using fom instead of from in your query: select order_id fom ordertable where cust_id in
This should be select order_id from ordertable where cust_id in
fom is not a recognized keyword. The MySQL parser doesn't know what to do with that, so it throws an error about "invalid syntax".
Consider using join operations in place of nested IN subqueries. If we are guaranteed:
product_id is unique in product table
order_id is unique in ordertable table
cust_id is unique in customer table
then we can get an equivalent result set, the average of the price of distinct products that were ordered...
SELECT AVG(p.product_price)
FROM ( SELECT l.product_id
FROM orderline l
JOIN ordertable o
ON o.order_id = l.order_id
JOIN customer c
ON c.cust_id = o.cust_id
WHERE c.city = 'Tuscon'
GROUP BY l.product_id
) q
JOIN product p
ON p.product_id = l.product_id
If we want that "average price" of all products ordered (a different result, with the average taking into account the number of times a product was ordered...then we could use a query like this:
SELECT AVG(p.product_price)
FROM product p
JOIN orderline l
ON l.product_id = p.product_id
JOIN ordertable o
ON o.order_id = l.order_id
JOIN customer c
ON c.cust_id = o.cust_id
WHERE c.city = 'Tuscon'
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