根据下一条记录删除SQL中的记录

Deleting record in SQL depending on next record(根据下一条记录删除SQL中的记录)
本文介绍了根据下一条记录删除SQL中的记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我有以下列的记录:IDTime_EndAttribute.

I have records with columns: ID, Time_End and Attribute.

我需要删除所有记录,

WHERE Time_End = '1990-01-01 00:00:00.000' AND Attribute <> '9'

但仅限:

  • 如果下一行没有相同的属性编号

  • 下一行具有相同的属性编号和 Time_End1990-01-01 00:00:00.000

例如:

ID     Time_End                     Attribute
---------------------------------------------
235    1990-01-01 00:00:00.000      5                  /delete
236    1990-01-01 00:00:00.000      5                  /delete
237    1990-01-01 00:00:00.000      5
238    2016-10-10 23:45:40.000      5


ID     Time_End                     Attribute
---------------------------------------------
312    1990-01-01 00:00:00.000      8                  /delete
313    2016-01-09 18:00:00.000      6                  
314    1990-01-01 00:00:00.000      4                  /delete
315    1990-01-01 00:00:00.000      7
316    2016-10-10 23:45:40.000      7

我们的客户有 50 个数据库表,每个表中有数千条记录(当然还有更多的列,我只提到了那些对解决方案有影响的列).记录从PLC发送到数据库,但有时(我们不知道为什么)PLC也会发送错误的记录.

Our customer have 50 database tables with thousands of records in every table (and of course more columns, I mentioned only those, which have impact on solution). Records are send in to the database from PLC, but sometimes (we don't know why) PLC send also wrong records.

所以我需要一个查询来查找那些错误的记录并删除它们.:)

So what I need is a query which finds those wrong records and deletes them. :)

有人知道 SQL 代码应该是什么样的吗?

Anybody who knows how the SQL code should look like?

推荐答案

请看下面我的 SQL.首先,我们使用两个窗口函数 (LEAD) 收集要删除的 id,以获取下一行所需的数据.然后,计算所有需要的数据后,应用 OP 提出的评估规则.最后,使用获取到的 id 通过带有 in 子句的 id 删除 tablet 受影响的记录.

Please see my SQL below. First, we collect ids to delete using two window functions (LEAD) to get the next row needed data. Then, with all needed data computed, apply the evaluation rules proposed by the OP. Last, use the obtained ids to delete the affected records of the tablet by id with an in clause.

 DELETE toDeleteTable 
 WHERE toDeleteTable.id IN (WITH dataSet
                              AS (SELECT toDeleteTable.id,
                                         toDeleteTable.time_end, 
                                         toDeleteTable.attribute, 
                                         LEAD(toDeleteTable.time_end,1,0)  OVER (ORDER BY toDeleteTable.id) AS next_time_end,
                                         LEAD(toDeleteTable.attribute,1,0) OVER (ORDER BY toDeleteTable.id) AS next_attribute
                                    FROM toDeleteTable)
                            SELECT dataSet.id
                              FROM dataSet
                             WHERE dataSet.time_end = '1990-01-01 00:00:00.000' 
                               AND dataSet.attribute <> '9' 
                               AND (  (dataSet.next_attribute = dataSet.attribute AND dataSet.next_time_end  = '1990-01-01 00:00:00.000')   
                                    OR dataSet.next_attribute <> dataSet.attribute)
                           )

这篇关于根据下一条记录删除SQL中的记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

ibtmp1是非压缩的innodb临时表的独立表空间,通过innodb_temp_data_file_path参数指定文件的路径,文件名和大小,默认配置为ibtmp1:12M:autoextend,也就是说在文件系统磁盘足够的情况下,这个文件大小是可以无限增长的。 为了避免ibtmp1文件无止境的暴涨导致
SQL query to group by day(按天分组的 SQL 查询)
What does SQL clause quot;GROUP BY 1quot; mean?(SQL 子句“GROUP BY 1是什么意思?意思是?)
MySQL groupwise MAX() returns unexpected results(MySQL groupwise MAX() 返回意外结果)
MySQL SELECT most frequent by group(MySQL SELECT 按组最频繁)
Include missing months in Group By query(在 Group By 查询中包含缺失的月份)