简体   繁体   English

从列A中具有相同值的每组行中删除除B列中具有最高值的行之外的所有行

[英]From every set of rows with the same value in column A delete all rows but the row with the highest value in column B

I'm searching for a SQL command that does the following: 我正在搜索执行以下操作的SQL命令:

From every set of rows with the same value in column A delete all rows but the row with the highest value in column B. If there are multiple rows with the same B value in a set keep at least one of them. 从列A中具有相同值的每组行中删除除了列B中具有最高值的行之外的所有行。如果在集合中存在具有相同B值的多个行,则保留其中至少一个。

Additional notes 补充笔记

  • The column format should not be modified nor additional tables should be required to achieve the desired result. 不应修改列格式,也不需要其他表来实现所需结果。
  • The table has only two columns from which none is primary, unique or multiple occurrences key. 该表只有两列,其中没有列是主键,唯一键或多次键。
  • The query should work well with bigger datasets ie the running time should be proportional to the number of rows in the table (not quadratic/exponential). 查询应该适用于更大的数据集,即运行时间应该与表中的行数成比例(不是二次/指数)。

Example: 例:

Initial state: 初始状态:

+---+---+
| A | B |
+---+---+
| x | 1 |
| x | 2 |
| y | 3 |
+---+---+

Desired result: 期望的结果:

+---+---+
| A | B |
+---+---+
| x | 2 |
| y | 3 |
+---+---+

Oy, this is a pain. 哦,这是一种痛苦。 I think the best way is truncate / insert : 我认为最好的方法是truncate / insert

create table temp_t as
    select *
    from t;

truncate table t;

insert into t(a, b)
    select distinct a, b
    from temp_t tt
    where tt.b = (select max(tt2.b) from temp_t tt2 where tt2.a = tt.a);

Another alternative would be to add a third column and assign that a unique number, which can then be used for the delete. 另一种方法是添加第三列并分配一个唯一的编号,然后可以将其用于删除。

This query should work!! 这个查询应该工作!!

I am retaining all the MAX B's for each A and deleting all the remaining rows from the table which are not required. 我保留每个A的所有MAX B并删除表中不需要的所有剩余行。

DELETE s1
FROM table s1,
    ( SELECT A,MAX(B) AS B FROM table GROUP BY A ) s2
WHERE s1.A = s2.A 
    AND s1.B <> s2.B;

Use MySQL's multi-table delete: 使用MySQL的多表删除:

delete t2
from mytable t1, mytable t2
where t2.A = t1.A
and t2.B < t1.B

An index on column A will make this perform well. A列上的索引将使其表现良好。

Use a JOIN with a subquery that gets the maximum B for each A, and make the JOIN condition match the rows where B doesn't match this. 使用带有子查询的JOIN ,该子查询为每个A获取最大B,并使JOIN条件与B不匹配的行匹配。

DELETE t1
FROM Table AS t1
JOIN (
    SELECT A, MAX(B) AS maxB
    FROM Table
    GROUP BY A) AS t2
ON t1.A = t2.A AND t1.B != maxB

To get rid of the duplicates that remain, use one of the solutions in How to delete duplicates on a MySQL table? 要删除剩余的重复项,请使用如何删除MySQL表上的重复项中的一个解决方案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM