Plsql PL/SQl,oracle 9i,使用SQl删除重复行
这里有一个场景,我们需要根据时间戳从表中删除所有重复的行。表结构如下所示: Item Ref1 Ref2 Timestamp 1 A test1 2/3/2012 10:00:00 1 A test2 2/3/2012 11:00:00 1 A test1 2/3/2012 12:00:00 2 A prod1 2/3/2012 10:00:00 2 B prod2 2/3/2012 11:00:00 2 A prod2 2/3/2012 12:00:00 项目Ref1 Ref2时间戳 1 A测试1 2012年2月3日10:00:00 A测试2012年2月3日11:00:00 1 A测试1 2012年2月3日12:00:00 2 A prod1 2012年3月2日10:00:00 2 B prod2 2/3/2012 11:00:00 2 A prod2 2/3/2012 12:00:00 因此,我们需要根据item和ref1从这个表中删除重复的行。像这里一样,对于项目1和带有最新时间戳的参考1 A,我们应该只有一行。对于第2项,我们应该只有一行ref1 A和最新时间戳Plsql PL/SQl,oracle 9i,使用SQl删除重复行,plsql,oracle9i,Plsql,Oracle9i,这里有一个场景,我们需要根据时间戳从表中删除所有重复的行。表结构如下所示: Item Ref1 Ref2 Timestamp 1 A test1 2/3/2012 10:00:00 1 A test2 2/3/2012 11:00:00 1 A test1 2/3/2012 12:00:00 2 A prod1 2/3/2012 10:00:00
任何指针都很好您可以按项和Ref1查询记录分组,然后删除其中项和Ref等于且时间戳
select Item
, Ref1
, max(Timestamp) tm
from table
group by Item, Ref1
结果
delete from table where Item = ? and Ref1 = ? and Timestamp < ?
delete from table where Item=?参考文献1=?时间戳<?
您可以查询按项和Ref1分组的记录,然后删除其中项和Ref等于且时间戳select Item
, Ref1
, max(Timestamp) tm
from table
group by Item, Ref1
结果
delete from table where Item = ? and Ref1 = ? and Timestamp < ?
delete from table where Item=?参考文献1=?时间戳<?
我手头没有安装Oracle 9,因此无法测试,但我相信这可能会起作用:
从表中选择(按项目划分,参考1按时间戳描述的顺序)ix*上的行号(
ix
大于1的视图中删除记录我手头没有Oracle 9安装,因此无法测试,但我相信这可能会起作用:
从表中选择(按项目划分,参考1按时间戳描述的顺序)ix*上的行号(
ix
大于1的视图中删除记录假设您想要的最终结果是一个包含这3行的表
Item Ref1 Ref2 Timestamp
1 A test1 2/3/2012 12:00:00
2 B prod2 2/3/2012 11:00:00
2 A prod2 2/3/2012 12:00:00
差不多
DELETE FROM table_name a
WHERE EXISTS( SELECT 1
FROM table_name b
WHERE a.item = b.item
AND a.ref1 = b.ref1
AND a.timestamp < b.timestamp );
假设您想要的最终结果是一个包含这3行的表
Item Ref1 Ref2 Timestamp
1 A test1 2/3/2012 12:00:00
2 B prod2 2/3/2012 11:00:00
2 A prod2 2/3/2012 12:00:00
差不多
DELETE FROM table_name a
WHERE EXISTS( SELECT 1
FROM table_name b
WHERE a.item = b.item
AND a.ref1 = b.ref1
AND a.timestamp < b.timestamp );