在经典模型中不适合找到订购最多的客户。我没有找到订购相同数量的顾客

问题描述 投票:0回答:1
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;

在实践中,查询运行良好,但我必须使用子查询,它必须给出有序结果,并且在我看来,如果有两个客户订购了相同类型的相同数量的件,则它不起作用。 谁能告诉我如何解决这个问题。我尝试使用 where Total = (select 但它给了我一个错误

mysql
1个回答
0
投票

选择产品线、客户名称、总和(总计)

来自( 从客户、订单、产品线、产品、订单详细信息中选择productLines.productline、customers.customerName、sum(quantityOrdered) 作为总计,其中customers.customerNumber =orders.customerNumber 和orderdetails.orderNumber=orders.orderNumber 和orderdetails.productCode = products.productCode 和Productlines.productLine= products.productLine GROUP(按产品线、客户名称)作为列表

其中总计 = ( 从中选择最大值(totale1) ( 从客户、订单、产品线、产品、订单详细信息中选择productLines.productline、customers.customerName、sum(quantityOrdered) as totale1,其中customers.customerNumber =orders.customerNumber和orderdetails.orderNumber=orders.orderNumber和orderdetails.productCode = products.productCode和产品线.productLine= 产品.productLine 按产品线、客户名称分组 ) 作为 x

按产品线分组;

向前迈出一步... 想法???

© www.soinside.com 2019 - 2024. All rights reserved.