【问题标题】:Problem with Max() function and other columnsMax() 函数和其他列的问题
【发布时间】:2021-01-11 22:02:41
【问题描述】:

这是我的查询

Select max(sale) as MaximumSales, cID
From(
Select SUM(totalPerSale) as sale,CustomerID as cID
From(
Select Quantity, UnitPrice, 
Quantity*UnitPrice as totalPerSale, CustomerID
From DbAssignment.`e-commerce-2021 (1)` order by totalPerSale DESC
) as Records 
group by CustomerID order by CustomerID DESC)
as total 

我想做的是让花最多钱的客户.... 具有别名 Records 的内部子查询按预期工作。它返回每个客户花费的金额总和。但是最外层的查询别名总计没有给出正确的 CustomerID 当我将它与最外层的选择一起使用时,它会返回数据集中的第一个 CustomerID...虽然 MAX 正在返回正确的数据....

我该如何解决...

还有其他方法吗?

【问题讨论】:

    标签: mysql sql subquery max


    【解决方案1】:

    您似乎在请求从未使用过的字段,一方面,我想知道您为什么不只按客户 ID 分组,按产品排序,然后限制到前 1 个。

    SELECT customerID, sum(Quantity*UnitPrice) as sales
    FROM DbAssignment.`e-commerce-2021 (1)` 
    GROUP BY customerID
    ORDER BY sum(Quantity*UnitPrice)
    LIMIT 1
    

    【讨论】:

    • 谢谢伙计....我不知道为什么我如此专注于我的 MAX() 工作....
    • 没问题,有时我们都会蒙蔽双眼。如果这对您有所帮助,请记住接受它作为答案。
    猜你喜欢
    • 2018-08-29
    • 1970-01-01
    • 1970-01-01
    • 2013-09-10
    • 1970-01-01
    • 2017-04-09
    • 1970-01-01
    • 2021-11-03
    • 2023-03-26
    相关资源
    最近更新 更多