查找 SQL 中某列出现的最高次数

发布于 2024-10-11 04:52:10 字数 884 浏览 9 评论 0 原文

给定此表:

订单
custName 描述 to_char(价格)
一个德萨$14
B 德斯布 $14
C 描述 $21
D desd 65 美元
E 德斯 $21
F 定义 $78
G 设计 $14
H desh $21

我试图显示价格出现次数最多的整行,在本例中为 $14 和 $21

我相信需要有一个子查询。所以我从这个开始:

select max(count(price))  
from orders  
group by price

这给了我 3。

一段时间后我认为这没有帮助。我相信我需要值 14 和 21 而不是计数,这样我就可以将其放在 where 子句中。但我不知道如何显示它。有什么帮助吗?

更新:所以我可以从中查询 14 和 21,

    select price
    from orders
    group by price
    having (count(price)) in
    (select max(count(price))
    from orders
    group by price)

但我需要它来显示客户名称和描述列,我收到错误:

select custname, description, price
from orders
group by price
having (count(price)) in
(select max(count(price))
from orders
group by price)

SQL Error: ORA-00979: not a GROUP BY expression

对此有任何帮助吗?

Given this table:

Order
custName description to_char(price)
A desa $14
B desb $14
C desc $21
D desd $65
E dese $21
F desf $78
G desg $14
H desh $21

I am trying to display the whole row where prices have the highest occurances, in this case $14 and $21

I believe there needs to be a subquery. So i started out with this:

select max(count(price))  
from orders  
group by price

which gives me 3.

after some time i didn't think that was helpful. i believe i needed the value 14 and 21 rather the the count so i can put that in the where clause. but I'm stuck how to display that. any help?

UPDATE: So I got it to query the 14 and 21 from this

    select price
    from orders
    group by price
    having (count(price)) in
    (select max(count(price))
    from orders
    group by price)

but i need it to display the custname and description column which i get an error:

select custname, description, price
from orders
group by price
having (count(price)) in
(select max(count(price))
from orders
group by price)

SQL Error: ORA-00979: not a GROUP BY expression

any help on this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

埖埖迣鎅 2024-10-18 04:52:10

我猜你已经很接近了。由于 HAVING 对 GROUPed 结果集进行操作,因此请尝试

HAVING COUNT(price) IN

HAVING COUNT(price) =

替换当前行。

I guess you are pretty close. Since HAVING operates on the GROUPed result set, try

HAVING COUNT(price) IN

or

HAVING COUNT(price) =

replacing your current line.

明明#如月 2024-10-18 04:52:10

由于您将问题标记为 oracle,因此您可以使用 窗口函数在同一查询中获取聚合和详细数据。

SELECT   COUNT (price) OVER (PARTITION BY price) count_at_this_price, 
         o.* 
from orders o 
order by 1 desc

Since you tagged the question as oracle, you can use windowing functions to get aggregate and detail data within the same query.

SELECT   COUNT (price) OVER (PARTITION BY price) count_at_this_price, 
         o.* 
from orders o 
order by 1 desc
傻比既视感 2024-10-18 04:52:10
select employee, count(employee)
from work
group by employee
having count(employee) =
( select max(cnt) from
( select employee, count(employee cnt
from work
group by employee
)
);

参考

select employee, count(employee)
from work
group by employee
having count(employee) =
( select max(cnt) from
( select employee, count(employee cnt
from work
group by employee
)
);

Reference

痴者 2024-10-18 04:52:10

你可以尝试像

select * from orders where price in (select top 2 price from orders group by price order by price desc)

我不确定Oracle中的限制结果,在SQL Server中是最高的,也许你应该使用限制。

You could try something like

select * from orders where price in (select top 2 price from orders group by price order by price desc)

I'm not sure of limiting results in Oracle, in SQL Server is top, maybe you should use limit.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文