在 Oracle 中对 SUM() 聚合函数使用 Max()

发布于 2024-12-19 11:28:18 字数 362 浏览 1 评论 0原文

我正在尝试在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

南烟 2024-12-26 11:28:18

您可以像这样使用排名功能

select * from
(
select a.*,rank() over (order by sum_sales desc) r from
(
select pub_id,sum(sales*price) sum_sales from titles group by pub_id
) a
)
where r = 1;    

You can use the rank function like this

select * from
(
select a.*,rank() over (order by sum_sales desc) r from
(
select pub_id,sum(sales*price) sum_sales from titles group by pub_id
) a
)
where r = 1;    
﹂绝世的画 2024-12-26 11:28:18

借助强大的 ORACLE 分析函数,这一切变得简单。

下面将给出每个 pub_id 的最大收入。

  select pub_id,REV from 
   (
   select pub_id, (sales*price) as REV,
   max(sales*price) over (partition by pub_id order by 1) as MAX 
   from titles
   )
   where REV=MAX

如果您想确定收入最大的 pub_id

   select * from
   (
   select pub_id,REV from 
   (
   select pub_id, (sales*price) as REV,
   max(sales*price) over (partition by pub_id order by 1) as MAX 
   from titles
   )
   where REV=MAX order by MAX desc
   )
   where rownum<2

That's simple with the powerful ORACLE ANALYTICAL FUNCTIONS

Below will give the max revenues for each pub_id.

  select pub_id,REV from 
   (
   select pub_id, (sales*price) as REV,
   max(sales*price) over (partition by pub_id order by 1) as MAX 
   from titles
   )
   where REV=MAX

If you want to determine the pub_id with the maximum revenue:

   select * from
   (
   select pub_id,REV from 
   (
   select pub_id, (sales*price) as REV,
   max(sales*price) over (partition by pub_id order by 1) as MAX 
   from titles
   )
   where REV=MAX order by MAX desc
   )
   where rownum<2
巴黎盛开的樱花 2024-12-26 11:28:18
SELECT PUB_ID, SUM(SALES*PRICE) as TotalRevenue 
FROM TITLES 
GROUP BY PUB_ID
HAVING SUM(SALES*PRICE) = ( SELECT MAX(SUM(SALES*PRICE)) 
                            FROM TITLES 
                            GROUP BY PUB_ID );
SELECT PUB_ID, SUM(SALES*PRICE) as TotalRevenue 
FROM TITLES 
GROUP BY PUB_ID
HAVING SUM(SALES*PRICE) = ( SELECT MAX(SUM(SALES*PRICE)) 
                            FROM TITLES 
                            GROUP BY PUB_ID );
花桑 2024-12-26 11:28:18

在这种情况下,实际上并不需要分析函数。最好的选择是将 sum() 和 max() 的以下时间与 dense_rank 选项。

select max(pub_id) keep (dense_rank last order by TotalRevenue)
from (
        SELECT PUB_ID, SUM(SALES*PRICE) as TotalRevenue 
        FROM TITLES 
        GROUP BY PUB_ID
    )

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.

select max(pub_id) keep (dense_rank last order by TotalRevenue)
from (
        SELECT PUB_ID, SUM(SALES*PRICE) as TotalRevenue 
        FROM TITLES 
        GROUP BY PUB_ID
    )
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文