MySQL中所有公司的交易总和[删除]

时间:2017-10-18 07:28:06

标签: mysql sql

我想找出最大资本[价格*数量]的公司(包括整个表格中该公司的所有条目)。有什么建议吗?

我做到了这一点:

SELECT symbol, SUM(amount*price) AS total
FROM orders
GROUP BY symbol

2 个答案:

答案 0 :(得分:2)

如果支持FETCH FIRST WITH TIES

SELECT symbol, SUM(amount*price) AS total
FROM orders
GROUP BY symbol
ORDER BY total DESC
FETCH FIRST 1 ROW WITH TIES

如果不是:

SELECT symbol, SUM(amount*price) AS total
FROM orders
GROUP BY symbol
HAVING SUM(amount*price) = (SELECT SUM(amount*price) AS total
                            FROM orders
                            GROUP BY symbol
                            ORDER BY total DESC
                            LIMIT 1)

答案 1 :(得分:1)

您的查询应如下所示。

SELECT symbol,SUM(amount*price) AS total FROM orders GROUP BY symbol order by total desc limit 1;

我正在添加限制1,因为您想找出资金最多的公司。