选择具有最大值的行,与其他两个列相关

发布于 2025-01-23 21:45:00 字数 373 浏览 3 评论 0原文

我有一个带有三列的桌子:区域,国家,计数。 我想将我的桌子减少到地区,国家和计数的行 - 该地区最大的数量。

例如,如果我有下表:

region | country | count
asia   | jo      | 12
asia   | ir      | 12
asia   | il      | 10  
europe | fr      |  8
europe | it      |  2

我希望得到回报:

region | country | count
asia   | jo      | 12
asia   | ir      | 12
europe | fr      |  8

I have a table with three columns: region, country, count.
I want to reduce my table to rows of region, country and count - where count is maximal among the region.

For example, if I have the following table:

region | country | count
asia   | jo      | 12
asia   | ir      | 12
asia   | il      | 10  
europe | fr      |  8
europe | it      |  2

I'd expect to get in return:

region | country | count
asia   | jo      | 12
asia   | ir      | 12
europe | fr      |  8

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

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

发布评论

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

评论(1

柠栀 2025-01-30 21:45:00

您可以以这种方式实现这一目标:首先,您需要按region字段对数据进行分组并获取最大值。然后在条件中执行一个简单的count

select * from my_table WHERE (region, count) in (select region, MAX(count) from my_table  GROUP BY region)

demo in sqldaddy.io

You can achieve this in this way: First you need to group the data by the region field and get the maximum value. Then execute a simple IN condition with fields region and count

select * from my_table WHERE (region, count) in (select region, MAX(count) from my_table  GROUP BY region)

Demo in sqldaddy.io

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