1

当其中一个重复项满足最低要求(x<15)时,我试图从数据集中删除整个块(请参阅下面的示例数据)。

+------+-------+------------+----+
| id   | block | repitition | x  |
+------+-------+------------+----+
| 5223 | 1     | 1          | 15 |
| 5223 | 1     | 2          | 17 |
| 5223 | 1     | 3          | 16 |
| 5223 | 2     | 1          | 14 |
| 5223 | 2     | 2          | 15 |
| 6238 | 2     | 1          | 18 |
| 6238 | 2     | 2          | 20 |
| 6238 | 2     | 3          | 12 |  
| 6238 | 2     | 4          | 21 |
| 7575 | 1     | 3          | 13 |
| 7575 | 1     | 4          | 21 |
| 7575 | 1     | 5          | 21 |
+------+-------+------------+----+

我已经在这方面得到了以下帮助:

delete t1
from your_table t1
inner join
(
    select id, block
    from your_table
    group by id, block
    having sum(x < 15) > 0
) t2 on t1.id = t2.id and t1.block = t2.block

这非常有效。我想知道的是我是否可以做完全相同的事情,但前提是 x < 15 是块中的第一次重复 - 无论重复次数如何。

因此,我想查询该表并让它只删除第 4、5、10、11、12 行。当前公式还将删除第 6、7、8 和 9 行。

4

1 回答 1

0
delete t4
from your_table t4
inner join
  (select id,block From YourTable t1
    inner join 
    (  
       select id,block,min(repitition) as FirstRepitition 
       From YourTable Group By id,Block 
    ) t2
    On t1.id = t2.id and t1.block = t2.block 
    and t1.Repitition = t2.FirstRepition and t1.x < 15
  ) t3 
  On t4.id = t3.id and t4.block = t3.block

将是一种方式。找到最早的重复,得到 x < 15 的那些,然后删除该批次。虽然很乱...

于 2013-09-05T15:03:52.027 回答