Oracle查找重复行,这是给定字段的连续的
我们在Oracle 19C数据库中有一些桌子,这些桌子是为了审计目的而建立的。此“审核”表是由更新/删除到另一个表“小部件”的触发器生成的。它没有检查新数据是否与旧数据匹配,因此除时间戳以外的所有内容中都有许多相同的行。我不是在寻找解决审计的评论,因为那里有很多解决方案。目前,我们只是停止出血,直到可以实现更好的解决方案为止。
我的问题是如何将现有的审核数据删除到实际更改的记录。这些表已经越来越多地影响了我们的某些过程。
auditwidget表数据:
datarowid | widgetId | widgetDesc | timestampoftableupdate |
---|---|---|---|
1 | 1 | A | 1/29/2016 12:12:55 PM |
2 2 | 2 | 测试 | 1/29/2016 12:13: |
13:13:13:13:13:13:13 pm 3 | 1 | A | 1/29/2016 12:14:55 PM PM PM PM PM PM |
4 | 1 | A | 1/29/2016 12:15:55 PM |
5 | 2 | 测试 | 1/29/2016 12:16:55 PM |
6 | 1 | C | 1/29/2016 12:17:55 PM |
7 | 1 1 | B | 1/29/2016 12:18:55 PM |
8 | 2 | D | 1/29/2016 12:19:55 PM |
9 | 1 | A | 1/29/2016 12:20:55 pm |
如您所见,第1,3和4行之间什么都没有改变或在2到5之间。这些行不应创建。我正在尝试找到一种方法来识别/删除小部件表中未显示任何更改的条目。我的最终数据看起来就是这样:
datarowid | widgetid | widgetDesc | timestampoftableupdate |
---|---|---|---|
1 | 1 | a 1 a | 1/29/2016 12:12:55 PM |
2 2 | 2 2 | B | 1/29/2016 12:13:13:13:13:13:13:13:13:13 pm |
6 | 1 | C | 1/29/2016 12: 17:55 PM |
7 | 1 | B | 1/29/2016 12:18:55 PM |
8 | 2 | D | 1/29/2016 12:19:55 PM |
9 | 1 | A | 1/29/2016 12:20:55 PM PM |
lag lag loct上一行可能包含不同的witgetID。有成千上万的小山针。每个小扇区也可能会连续许多相同的行。是否有任何解决方案缺少数据循环并通过行进行有条件地做某事?任何方向都将不胜感激。
SQL创建用于测试的数据:
创建表AuditWidget
(( “ datarowid”号码(,0), “ widgetID”数字(,0), “ widgetdesc” varchar2(30), “ timestampoftableupdate”日期);
插入auditwidget(datarowid,widgetID,widgetDesc,timestampoftableupdate)值(1,1,'a',sysdate -sysdate -Intersdal'100'分钟); 插入auditwidget(datarowid,widgetID,widgetDesc,timestampoftableupdate)值(2,2,'test',sysdate -sysdate -Interval'99'分钟); 插入auditwidget(datarowid,widgetID,widgetDesc,timestampoftableupdate)值(3,1,'a',sysdate -sysdate -Intersdal'98'分钟); 插入auditwidget(datarowid,widgetID,widgetDesc,timestampoftableupdate)值(4,1,'a',sysdate -sysdate -Intersdal'97'分钟); 插入auditwidget(dataRowid,widgetID,widgetDesc,timestampoftableupdate)值(5,2,'test',sysdate -sysdate -Interval'96'分钟); 插入auditwidget(datarowid,widgetID,widgetDesc,timestampoftableupdate)值(6,1,'c',sysdate -sysdate -Interval'95'分钟); 插入AUDITWIDGET(DATAROWID,WIDGETID,WIDGETDESC,TIMESTAMPOFTableUpdate)值(7,1,'B',sysdate -sysdate -Interval'94'分钟); 插入auditwidget(dataRowid,widgetID,widgetDesc,timestampoftableupdate)值(8,1,'d',sysdate -sysdate -Interval'93'分钟); 插入auditwidget(dataRowid,widgetID,widgetDesc,timestampoftableupdate)值(9,1,'a',sysdate -sysdate -Intersdal'92'分钟);
We have some tables in an Oracle 19c database that were put in place many years ago for audit purposes. This 'audit' table is being generated by a trigger on update/delete to another table "widget". It is not checking to see if the new data matches the old data, so there are a lot of rows that are identical in everything but timestamp. I am not looking for comments on fixing the auditing as there many solutions to that mess. We are currently just stopping the bleeding until a better solution can be implemented.
My question is how to cut my existing audit data down to only the records that actually changed. These tables have grown to the point that it is impacting some of our processes.
auditWidget table data:
dataRowID | widgetID | widgetDesc | timestampOfTableUpdate |
---|---|---|---|
1 | 1 | A | 1/29/2016 12:12:55 PM |
2 | 2 | test | 1/29/2016 12:13:55 PM |
3 | 1 | A | 1/29/2016 12:14:55 PM |
4 | 1 | A | 1/29/2016 12:15:55 PM |
5 | 2 | test | 1/29/2016 12:16:55 PM |
6 | 1 | c | 1/29/2016 12:17:55 PM |
7 | 1 | b | 1/29/2016 12:18:55 PM |
8 | 2 | d | 1/29/2016 12:19:55 PM |
9 | 1 | A | 1/29/2016 12:20:55 PM |
As you can see, nothing has changed between row 1,3 and 4, or between 2 and 5. These rows should never have been created. I am trying to find a way to identify/delete the entries that don't show any change in the widget table. My final data would look like this:
dataRowID | widgetID | widgetDesc | timestampOfTableUpdate |
---|---|---|---|
1 | 1 | A | 1/29/2016 12:12:55 PM |
2 | 2 | b | 1/29/2016 12:13:55 PM |
6 | 1 | c | 1/29/2016 12:17:55 PM |
7 | 1 | b | 1/29/2016 12:18:55 PM |
8 | 2 | d | 1/29/2016 12:19:55 PM |
9 | 1 | A | 1/29/2016 12:20:55 PM |
LAG only looks at the previous row which might contain a different widgetID. There are thousands of widgetIDs. There is also potentially many identical rows per widgetID in a row. Are there any solutions out there short of looping through the data and conditionally doing something with it row by row? Any direction would be appreciated.
SQL to create data for testing:
CREATE TABLE AUDITWIDGET
(
"DATAROWID" NUMBER(,0),
"WIDGETID" NUMBER(,0),
"WIDGETDESC" VARCHAR2(30),
"TIMESTAMPOFTABLEUPDATE" DATE);
Insert into AUDITWIDGET (datarowID,widgetID,widgetDesc,timeStampOfTableUpdate) values (1,1,'A',sysdate - interval '100' minute);
Insert into AUDITWIDGET (datarowID,widgetID,widgetDesc,timeStampOfTableUpdate) values (2,2,'test',sysdate - interval '99' minute);
Insert into AUDITWIDGET (datarowID,widgetID,widgetDesc,timeStampOfTableUpdate) values (3,1,'A',sysdate - interval '98' minute);
Insert into AUDITWIDGET (datarowID,widgetID,widgetDesc,timeStampOfTableUpdate) values (4,1,'A',sysdate - interval '97' minute);
Insert into AUDITWIDGET (datarowID,widgetID,widgetDesc,timeStampOfTableUpdate) values (5,2,'test',sysdate - interval '96' minute);
Insert into AUDITWIDGET (datarowID,widgetID,widgetDesc,timeStampOfTableUpdate) values (6,1,'c',sysdate - interval '95' minute);
Insert into AUDITWIDGET (datarowID,widgetID,widgetDesc,timeStampOfTableUpdate) values (7,1,'b',sysdate - interval '94' minute);
Insert into AUDITWIDGET (datarowID,widgetID,widgetDesc,timeStampOfTableUpdate) values (8,1,'d',sysdate - interval '93' minute);
Insert into AUDITWIDGET (datarowID,widgetID,widgetDesc,timeStampOfTableUpdate) values (9,1,'A',sysdate - interval '92' minute);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
仅针对
选择
的简单解决方案相关数据是使用lag()
。例如:The simple solution to only
SELECT
the relevant data is to useLAG()
. For example:然后删除重复项,然后:
Delete duplicates, then: