Mysql Max()函数和其他列出现问题
这是我的问题Mysql Max()函数和其他列出现问题,mysql,sql,subquery,max,Mysql,Sql,Subquery,Max,这是我的问题 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
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
我想做的是找到花费最多的客户。。。。
具有别名记录的内部子查询正在按预期工作。它返回每个客户花费的金额总和。但是最外层的查询别名as total没有给出正确的CustomerID
当我将它与最外层的select一起使用时,它返回数据集中的第一个CustomerID…尽管MAX返回了正确的数据
我该如何解决这个问题
还有其他方法吗?首先,您似乎在请求从未使用过的字段,我想知道您为什么不按客户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()工作。。。。我的错…没问题,我们都有失明的时候。如果这对你有帮助,记得接受它作为答案。