如何删除一组中的最高值

发布于 2024-10-31 12:09:32 字数 526 浏览 0 评论 0原文

我正在尝试从组中删除最高值。鉴于此数据:

group_table

group_id | st_area
---------------
1        |  20
1        |  30
2        |   1
2        |   1
2        | 200

我想删除第二行和第五行。我知道如何从每个组中选择最高的:

SELECT max(area) FROM group_table GROUP BY group_id

不过,我不知道如何制定一个删除语句来接受它作为子查询。当我尝试 delete from group_table where st_area = max(st_area); 时,

出现错误:WHERE 子句中不允许聚合

我很快就了解到的一件事是,我不是一个非常好的沟通者。如果我问的不清楚&你很有耐心,我一定会尽力澄清。

谢谢!

I'm trying to delete the highest value from a group. Given this data:

group_table

group_id | st_area
---------------
1        |  20
1        |  30
2        |   1
2        |   1
2        | 200

I'd like to remove the second and fifth rows. I know how to select the highest from each group:

SELECT max(area) FROM group_table GROUP BY group_id

I can't figure out how to formulate a delete statement that will accept that as a subquery, though. When I try delete from group_table where st_area = max(st_area);

I get an error: aggregates not allowed in WHERE clause.

One thing I have learned pretty quickly on SO is that I'm not a terribly good communicator. If what I'm asking is unclear & you're feeling patient, I'll certainly try to clarify.

Thanks!

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

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

发布评论

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

评论(1

薔薇婲 2024-11-07 12:09:32

你的表中有主键吗?根据你的规范,你不需要。

在这种情况下:

DELETE 
  FROM group_table 
  WHERE (group_id, st_area) 
    IN (SELECT group_id, MAX(st_area) FROM group_table GROUP BY group_id);

但是如果您有两个最大值行,它将删除这两行。

如果在这种情况下您只想删除一个,则应该为选择添加一个 id 列

DELETE 
  FROM group_table
  WHERE (id) 
    IN (
      SELECT MAX(id) FROM group_table 
        WHERE (group_id, st_area) 
          IN ( SELECT group_id, MAX(st_area) 
                 FROM group_table 
                 GROUP BY group_id) 
        GROUP BY group_id;

:);

因此,您要为每个组 id 的每个最大 st_area 选择最大 id。

如果您有这样的结构:

id | gid | area
 1    1     2
 2    1     1
 3    1     2

第一个查询将删除第 1 行和第 3 行,第二个查询将删除第 3 行。

Do you have some primary key in your table? According to your specification, you don't.

In this case:

DELETE 
  FROM group_table 
  WHERE (group_id, st_area) 
    IN (SELECT group_id, MAX(st_area) FROM group_table GROUP BY group_id);

But if you have TWO maximum-value rows, it will delete both.

If in this case you want to delete just one, you should add an id column for the selection:

DELETE 
  FROM group_table
  WHERE (id) 
    IN (
      SELECT MAX(id) FROM group_table 
        WHERE (group_id, st_area) 
          IN ( SELECT group_id, MAX(st_area) 
                 FROM group_table 
                 GROUP BY group_id) 
        GROUP BY group_id;

);

So you are selecting the maximum id for every maximum st_area for every group id.

In case you have a structure like this:

id | gid | area
 1    1     2
 2    1     1
 3    1     2

the first query will delete rows 1 and 3, and the second query just 3.

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