在 Oracle 中对 SUM() 聚合函数使用 Max()
我正在尝试在 Oracle 中编写一个查询,该查询将从列出 pub_id、销售额、价格的标题表中返回 pub_id 和最大总收入。 我可以得到带有 pub_id 的列表和每个 pub_id 的总收入
SELECT PUB_ID, SUM(SALES*PRICE) as TotalRevenue FROM TITLES GROUP BY PUB_ID;
或者我可以得到 MAX(销售额*价格) 有
SELECT MAX(SUM(sales*price)) FROM titles GROUP BY pub_id;
任何想法如何才能得到 pub_id 与总收入的最大值?
I am trying to write a query in Oracle which will return both the pub_id and the maximum total revenue from a titles table which lists pub_id, sales, price.
I can get either a listing with pub_id and total revenues for each pub_id with
SELECT PUB_ID, SUM(SALES*PRICE) as TotalRevenue FROM TITLES GROUP BY PUB_ID;
Or I can get just the MAX(Sales*Price) with
SELECT MAX(SUM(sales*price)) FROM titles GROUP BY pub_id;
Any ideas how can I get the pub_id out with the maximum of the total revenue?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以像这样使用排名功能
You can use the rank function like this
借助强大的
ORACLE 分析函数
,这一切变得简单。下面将给出每个
pub_id
的最大收入。如果您想确定收入最大的
pub_id
:That's simple with the powerful
ORACLE ANALYTICAL FUNCTIONS
Below will give the max revenues for each
pub_id
.If you want to determine the
pub_id
with the maximum revenue:在这种情况下,实际上并不需要分析函数。最好的选择是将 sum() 和 max() 的以下时间与 dense_rank 选项。
There's no really need for analytic functions in this case. The best option would be to group two times one for the sum() and the following time for the max() with the dense_rank option.