sql statement where there is top5 and inner join - mysql

How do i write a sql statement where there is top5 and inner join?
Currently, this is what i have:
$query = "SELECT OrderItem.ProductCode , Product.ProductName,
count(OrderItem.OrderID) as total_orders
FROM `OrderItem`
GROUP BY ProductCode
ORDER BY total_orders DESC LIMIT 5
INNER JOIN Product ON Product.ProductCode = OrderItem.ProductCode";
but it's not working.
tables:
Product table has columns:
ProductCode, ProductName, Cat, Qty, CostPr, RetailPr, VendorID
OrderItem table has columns:
OrderID, ProductCode, UnitPr, Qty,TotalPr
My objective is to display the productcode and productname of the top 5 products in orders submitted. Please help. thanks.

A JOIN is part of the FROM clause, and should come before your GROUP BY:
SELECT
OrderItem.ProductCode,
Product.ProductName,
count(OrderItem.OrderID) as total_orders
FROM
OrderItem
INNER JOIN Product
ON Product.ProductCode = OrderItem.ProductCode
GROUP BY OrderItem.ProductCode, Product.ProductName
ORDER BY total_orders DESC LIMIT 5

select ol.ProductCode, ol.total_orders, p.ProductName
from (
select ProductCode, count(OrderID) as total_orders
from OrderItem
group by ProductCode
order by total_orders desc LIMIT 5
) ol
inner join Product p on p.ProductCode = ol.ProductCode

Related

Select 3 products (product name and order number) sold in the most quantity in one order (in MySQL)

Whole db with all tables is in the LINK
I tried using this code:
SELECT Products.ProductName, OrderDetails.OrderID, SUM(Quantity) FROM Products
INNER JOIN OrderDetails ON Products.ProductID = OrderDetails.ProductID
GROUP BY ProductName
ORDER BY SUM(Quantity) DESC
But the result was wrong for sure...
You were almost there, and you need to group by two columns orderdetails.orderid and products.productid
SELECT products.productname,
products.productid,
orderdetails.orderid,
Sum(quantity) AS MaxQuantity
FROM products
INNER JOIN orderdetails
ON products.productid = orderdetails.productid
GROUP BY orderdetails.orderid,
products.productid
ORDER BY maxquantity DESC LIMIT 0, 3

MySQL - the cheapest order

I have 2 MySQL tables:
Persons:
person_id
person_name
Orders:
person_id
cost
order_name
I need to make a ONE sql-query to get person name and it's cheapest order.
Want to write some like this: SELECT person_name, order=(SELECT order_name FROM orders WHERE order.person_id = person.person_id ORDER BY order.cost ASC LIMIT 1) FROM person
But it don't work.
I would use a correlated query. There are many ways to do it: as a subquery in the SELECT clause, or as a subquery in the WHERE clause.
In this case, I implemented the second option:
SELECT
person_name,
order_name
FROM Persons
INNER JOIN Orders ON Persons.person_id = Orders.person_id
WHERE
(
cost = (
SELECT MIN(cost)
FROM Orders
WHERE
(Orders.person_id = Persons.person_id)
)
)
remove order= and add AS order_name
Something like this:
SELECT p.person_name
, ( SELECT o.order_name
FROM orders o
WHERE o.person_id = p.person_id
ORDER BY o.cost ASC
LIMIT 1
) AS order_name
FROM person p
ORDER BY p.person_name
SELECT person_name, order_id, order_name, MIN(cost) as cost
FROM persons
INNER JOIN orders
ON persons.person_id = orders.person_id
GROUP BY person.person_name, order_id, order_name;
You can do this just by joining your 2 tables and use MIN.

select sum qty from 2 tables

In MySQL I have 4 tables:
- product(id)
- order(id)
- order_detail_1(id, product_id, order_id, qty)
- order_detail_2(id, product_id, order_id, qty)
I want to get the sum of the quantity of products sold from the 2 tables (order_detail_1, order_detail_2) grouping them by product
produt can existe in order_detail_1 and not in order_detail_2 and vice versa
i tested this query and it worked but I want a simpler query without the union and the subquery.
select tmp.product_id ,sum(tmp.qty) from
(
(
select order_detail_1.product_id ,sum(order_detail_1.qty)
from order_detail_1
inner join order on order_detail_1.id_order = order.id
where order_detail_1.product_id is not null
group by order_detail_1.product_id
)
union all
(
select order_detail_2.product_id ,sum(order_detail_2.qty)
from order_detail_2
inner join order on order_detail_2.id_order = order.id
where order_detail_2.product_id is not null
group by order_detail_2.product_id
)
) tmp
group by tmp.product_id
It looks like you're not using order table other then checking if it exists, so you can use EXISTS()
SELECT p.product_id,sum(p.qty) as qty
FROM (SELECT product_id,qty,id_order FROM order_detail_1
WHERE product_id IS NOT NULL
UNION ALL
SELECT product_id,qty,id_order FROM order_detail_2
WHERE product_id IS NOT NULL) p
WHERE EXISTS(SELECT 1 FROM order o
WHERE o.id = p.id_order)
GROUP BY p.product_id
If a product is in only one table, you can use left join:
select p.id, (coalesce(sum(od1.qty), 0) + coalesce(sum(od2.qty, 0))) as qty
from product p left join
order_detail_1 od1
on od1.product_id = p.id left join
order_detail_2 od2
on od2.product_id = p.id
group by p.id;
This formulation depends on the fact that the two tables are exclusion -- a product is in only one table.
EDIT:
If products can exist in both tables, then you need to aggregate them first:
select p.id, (coalesce(od1.qty, 0) + coalesce(od2.qty, 0)) as qty
from product p left join
(select product_id, sum(qty) as qty
from order_detail_1 od1
group by product_id
) od1
on od1.product_id = p.id left join
(select product_id, sum(qty) as qty
from order_detail_2 od2
group by product_id
) od2
on od2.product_id = p.id;

Select from 3 tables with two order by before two group by

I try to get a list of products with each newest and lowest offer price
Table product:
id | name
Table offer:
id | product_id | price | created | dealer_id
Table invalids:
id | offer_id | status
I have tried:
SELECT * FROM product INNER JOIN
(
SELECT offer.product_id , offer.price
FROM offer
LEFT JOIN invalids
ON offer.id = invalids.offer_id
WHERE invalids.id IS NULL
GROUP BY offer.dealer_id
ORDER BY offer.created DESC
) o
ON o.product_id = product.id
ORDER BY product.name
I have tried an sqlfiddle http://sqlfiddle.com/#!9/32658/3 with this offer values:
(`id`, `price`, `dealer_id`, `product_id`, `created`)
(1,12.60,1,1,'2015-05-17 08:44:45'),
(2,13.00,1,1,'2015-08-17 08:44:45'),
(3,20.00,1,1,'2015-08-17 08:45:30'),
(4,10.00,1,1,'2015-08-17 08:45:46'),
(5,4.00,2,1,'2015-05-17 08:44:11'),
(6,11.00,2,1,'2015-08-17 08:44:46'),
(7,5.00,2,1,'2015-08-17 08:45:31'),
(9,110.00,2,2,'2015-08-17 08:46:58'),
(10,11.00,2,2,'2015-08-17 08:47:12');
Expected value for product ID 1 is offer ID 7 with price 5.
These steps I think I must realize:
Order offers by created and group by dealer_id to get newest entries
Take result from step 1 and order it by price to get smallest price.
Make this for all products
Maybe I must use a second SELECT FROM offer with GROUP BY and ORDER BY but how do I get I the product_id from the first (outer) select?
Well I would start by getting the latest date for each product offer like this:
SELECT product_id, MAX(created) AS latestOffer
FROM offer
GROUP BY product_id;
Once you have that, you can join it to the original table to get that offer:
SELECT o.*
FROM offer o
JOIN(
SELECT product_id, MAX(created) AS latestOffer
FROM offer
GROUP BY product_id) tmp ON tmp.product_id = o.product_id AND tmp.latestOffer = o.created;
Here is an SQL Fiddle example.
This query should help you:
SELECT *
FROM product
JOIN (
SELECT product_id, min(price) as minPrice, max(created) as newestOffer
FROM offer
WHERE id NOT IN (SELECT offer_id FROM invalids)
GROUP BY 1
) as b
ON product.id = b.product_id
A shot in the dark based on what I understand you to be after...
lots of nested subqueries.. keep thinking there's got to be a better way...
SELECT OO.ID, OO.Price, OO.Dealer_Id, OO.Product_ID, OO.created, P.name
FROM Offer OO
INNER JOIN (
SELECT Min(Price) as MinP
FROM offer O
INNER JOIN (
SELECT max(OI.created) as LatestOffer, OI.Dealer_ID, OI.Product_ID
FROM Offer OI
LEFT JOIN invalids I
on OI.Id = I.offer_Id
WHERE I.ID is null
GROUP BY OI.Dealer_Id, OI.Product_Id
) B
on O.Dealer_Id = B.Dealer_Id
and O.Product_Id = B.Product_Id
and O.Created = B.LatestOffer
) Z
on OO.Price = Z.MinP
INNER JOIN product P
on P.ID = OO.Product_ID
SQL FIDDLE

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