select productline, customerName, totale
from(
SELECT productLines.productline,
customers.customerName,
sum(quantityOrdered) as totale
from customers, orders, productlines, products, orderdetails
WHERE customers.customerNumber = orders.customerNumber
and orderdetails.orderNumber= orders.orderNumber
and orderdetails.productCode = products.productCode
and productlines.productLine= products.productLine
GROUP by productlines.productline, customers.customerName
order by sum(quantityOrdered) desc
) as lista
group by productline;
in practice the query works well but I had to use a subquery which must give ordered results and in my opinion it doesn’t work if there are two customers who have ordered the same number of pieces for the same type.
Can anyone tell me how to solve this problem. I tried with where total = (select but it gives me an error
3
Answers
select productline, customerName, sum(totale)
from ( SELECT productLines.productline, customers.customerName, sum(quantityOrdered) as totale from customers, orders, productlines, products, orderdetails WHERE customers.customerNumber = orders.customerNumber and orderdetails.orderNumber= orders.orderNumber and orderdetails.productCode = products.productCode and productlines.productLine= products.productLine GROUP by productline , customerName ) as lista
where totale = ( SELECT max(totale1) FROM ( SELECT productLines.productline, customers.customerName, sum(quantityOrdered) as totale1 from customers, orders, productlines, products, orderdetails WHERE customers.customerNumber = orders.customerNumber and orderdetails.orderNumber= orders.orderNumber and orderdetails.productCode = products.productCode and productlines.productLine= products.productLine GROUP by productline , customerName ) as x
)
GROUP by productline;
one step forward... but return
productline customerName sum(totale) Classic Cars Euro+ Shopping Channel 3816
it s right but only one :(
idea???
select * from
(
SELECT productLines.productline, customers.customerName, sum(quantityOrdered) as totale1 from customers, orders, productlines, products, orderdetails WHERE customers.customerNumber = orders.customerNumber and orderdetails.orderNumber= orders.orderNumber and orderdetails.productCode = products.productCode and productlines.productLine= products.productLine
GROUP by productline , customerName
) as a
left join
SELECT productLines.productline, customers.customerName, sum(quantityOrdered) as totale1 from customers, orders, productlines, products, orderdetails WHERE customers.customerNumber = orders.customerNumber and orderdetails.orderNumber= orders.orderNumber and orderdetails.productCode = products.productCode and productlines.productLine= products.productLine
GROUP by productline , customerName
) as b
on a.productline = b.productline and a.totale1 < b.totale1
where isnull(b.totale1);
ok resolved
As I stated in my comment above, your query is non-deterministic and only working because you have
ONLY_FULL_GROUP_BY
disabled. Bill gives a nice simple explanation of the problem with MySQL’s behaviour with ONLY_FULL_GROUP_BY disabled here.Assuming you just want the top customer(s) for each productline you can use GROUP BY and
RANK()
:You can read more in the docs – MySQL Handling of GROUP BY