Oracle查找重复行,这是给定字段的连续的

发布于 2025-02-10 18:47:47 字数 3234 浏览 3 评论 0原文

我们在Oracle 19C数据库中有一些桌子,这些桌子是为了审计目的而建立的。此“审核”表是由更新/删除到另一个表“小部件”的触发器生成的。它没有检查新数据是否与旧数据匹配,因此除时间戳以外的所有内容中都有许多相同的行。我不是在寻找解决审计的评论,因为那里有很多解决方案。目前,我们只是停止出血,直到可以实现更好的解决方案为止。

我的问题是如何将现有的审核数据删除到实际更改的记录。这些表已经越来越多地影响了我们的某些过程。

auditwidget表数据:

datarowidwidgetIdwidgetDesctimestampoftableupdate
11A1/29/2016 12:12:55 PM
2 22测试1/29/2016 12:13:
13:13:13:13:13:13:13 pm 31A1/29/2016 12:14:55 PM PM PM PM PM PM
41A1/29/2016 12:15:55 PM
52测试1/29/2016 12:16:55 PM
61C1/29/2016 12:17:55 PM
71 1B1/29/2016 12:18:55 PM
82D1/29/2016 12:19:55 PM
91A1/29/2016 12:20:55 pm

如您所见,第1,3和4行之间什么都没有改变或在2到5之间。这些行不应创建。我正在尝试找到一种方法来识别/删除小部件表中未显示任何更改的条目。我的最终数据看起来就是这样:

datarowidwidgetidwidgetDesctimestampoftableupdate
11a 1 a1/29/2016 12:12:55 PM
2 22 2B1/29/2016 12:13:13:13:13:13:13:13:13:13 pm
61C1/29/2016 12: 17:55 PM
71B1/29/2016 12:18:55 PM
82D1/29/2016 12:19:55 PM
91A1/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:

dataRowIDwidgetIDwidgetDesctimestampOfTableUpdate
11A1/29/2016 12:12:55 PM
22test1/29/2016 12:13:55 PM
31A1/29/2016 12:14:55 PM
41A1/29/2016 12:15:55 PM
52test1/29/2016 12:16:55 PM
61c1/29/2016 12:17:55 PM
71b1/29/2016 12:18:55 PM
82d1/29/2016 12:19:55 PM
91A1/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:

dataRowIDwidgetIDwidgetDesctimestampOfTableUpdate
11A1/29/2016 12:12:55 PM
22b1/29/2016 12:13:55 PM
61c1/29/2016 12:17:55 PM
71b1/29/2016 12:18:55 PM
82d1/29/2016 12:19:55 PM
91A1/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 技术交流群。

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

发布评论

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

评论(2

旧梦荧光笔 2025-02-17 18:47:47

仅针对选择的简单解决方案相关数据是使用lag()。例如:

select *,
from (
  select t.*,
    lag(widgetdesc) over(
      partition by widgetid 
      order by timestampOfTableUpdate
    ) as prev_desc
  from my_table t
) x
where widgetdesc <> prev_desc

The simple solution to only SELECT the relevant data is to use LAG(). For example:

select *,
from (
  select t.*,
    lag(widgetdesc) over(
      partition by widgetid 
      order by timestampOfTableUpdate
    ) as prev_desc
  from my_table t
) x
where widgetdesc <> prev_desc
清秋悲枫 2025-02-17 18:47:47

然后删除重复项,然后:

delete from auditwidget a
  where a.rowid > (select min(b.rowid)
                   from auditwidget b
                   where b.widgetid = a.widgetid
                     and b.widgetdesc = a.widgetdesc
                  );

Delete duplicates, then:

delete from auditwidget a
  where a.rowid > (select min(b.rowid)
                   from auditwidget b
                   where b.widgetid = a.widgetid
                     and b.widgetdesc = a.widgetdesc
                  );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文