表结构如下
ID EVT_OBJECT glh_ID xc begin_dt end_dt JG
1 1000.001 AAAA Y 2005-10-11 2005-11-11 1
2 1000.001 AAAA M 2005-10-10 2005-11-10 1
3 1000.001 AAAA W 2005-10-11 2005-11-11 7
4 1000.001 BBBB Y 2005-10-10 2005-11-10 1去掉重复记录,条件如下:
1,如果glh_id相同,取xc=Y(Y>M>W>D)记录,如果xc相同,取begin_dt大的记录,
2,如果glh_id不相同,比较xc的大小(Y>M>W>D),如果xc相同,取begin_dt大的记录。
3,按条件上面记录最后留下ID=1的记录,其它记录都得删除了。
ID EVT_OBJECT glh_ID xc begin_dt end_dt JG
1 1000.001 AAAA Y 2005-10-11 2005-11-11 1
2 1000.001 AAAA M 2005-10-10 2005-11-10 1
3 1000.001 AAAA W 2005-10-11 2005-11-11 7
4 1000.001 BBBB Y 2005-10-10 2005-11-10 1去掉重复记录,条件如下:
1,如果glh_id相同,取xc=Y(Y>M>W>D)记录,如果xc相同,取begin_dt大的记录,
2,如果glh_id不相同,比较xc的大小(Y>M>W>D),如果xc相同,取begin_dt大的记录。
3,按条件上面记录最后留下ID=1的记录,其它记录都得删除了。
a.*
from
表 a
where
not exists(select 1
from 表
where
glh_ID<>a.glh_ID
and
decode(xc,'Y',4,'M',3,'W',2,'D',1)>decode(a.xc,'Y',4,'M',3,'W',2,'D',1)
or
(decode(xc,'Y',4,'M',3,'W',2,'D',1)=decode(a.xc,'Y',4,'M',3,'W',2,'D',1) and begin_dt>a.begin_dt))
and
not exists(select 1
from 表
where
glh_ID= a.glh_ID
and
decode(xc,'Y',4,'M',3,'W',2,'D',1)>decode(a.xc,'Y',4,'M',3,'W',2,'D',1)
or
(decode(xc,'Y',4,'M',3,'W',2,'D',1)=decode(a.xc,'Y',4,'M',3,'W',2,'D',1) and begin_dt>a.begin_dt))
-----------------------------------------------------------------------------------------------------------------
delete from 表 a
where
exists(select 1
from 表
where
glh_ID<>a.glh_ID
and
decode(xc,'Y',4,'M',3,'W',2,'D',1)>decode(a.xc,'Y',4,'M',3,'W',2,'D',1)
or
(decode(xc,'Y',4,'M',3,'W',2,'D',1)=decode(a.xc,'Y',4,'M',3,'W',2,'D',1) and begin_dt>a.begin_dt))
or
exists(select 1
from 表
where
glh_ID= a.glh_ID
and
decode(xc,'Y',4,'M',3,'W',2,'D',1)>decode(a.xc,'Y',4,'M',3,'W',2,'D',1)
or
(decode(xc,'Y',4,'M',3,'W',2,'D',1)=decode(a.xc,'Y',4,'M',3,'W',2,'D',1) and begin_dt>a.begin_dt))
delete from 表 where id not in(select distinct first_value(id) over(order by begin_dt desc) from 表 where xc='Y')
1。 只保留xc=Y 的记录
2。 保留xc=Y 的记录后取begin_dt最大的delete from 表 where xc<>'Y';
commit;
delete from 表 a where exists (select 1 from 表 a where a.begin_dt<b.begin_dt);
commit;
from 表 b
where b.xc =
(select max(decode(a.xc, 'Y', 4, 'M', 3, 'W', 2, 'D', 1)) from 表 a
and rownum = 1
order by begin_dt descdecode(a.xc, 'Y', 4, 'M', 3, 'W', 2, 'D', 1)) 是从 libin_ftsafe(子陌红尘|潇湘剑公子@dev-club) 处偷来的. ^_^
from 表 b
where b.xc =(select max(decode(a.xc, 'Y', 4, 'M', 3, 'W', 2, 'D', 1)) from 表 a)
and rownum = 1
order by begin_dt descdecode(a.xc, 'Y', 4, 'M', 3, 'W', 2, 'D', 1)) 是从 libin_ftsafe(子陌红尘|潇湘剑公子@dev-club) 处偷来的. ^_^
from 表 b
where decode(b.xc, 'Y', 4, 'M', 3, 'W', 2, 'D', 1) =
(select max(decode(a.xc, 'Y', 4, 'M', 3, 'W', 2, 'D', 1)) from 表 a) and
rownum = 1
order by begin_dt descdecode(a.xc, 'Y', 4, 'M', 3, 'W', 2, 'D', 1)) 是从 libin_ftsafe(子陌红尘|潇湘剑公子@dev-club) 处偷来的. ^_^发出才发现写错了.每一次加贴,见谅. 这里能删或改自己的贴子吗
1,如果glh_id相同,取xc=Y(Y>M>W>D)记录,如果xc相同,取begin_dt大的记录,
2,如果glh_id不相同,比较xc的大小(Y>M>W>D),如果xc相同,取begin_dt大的记录。
3,按条件上面记录最后留下ID=1的记录,其它记录都得删除了。
不是很明白楼主的意思,楼主条件中的glh_id好像没什么用(相同跟不同)
我把楼主的意思理解成,如果glh_id不同,应该取glh_id大的,然后,在跟后面的条件
select * from
( select a.*, rownumber() over(partition by EVT_OBJECT order by glh_id desc, decode(a.xc, 'Y', 1, 'M', 2, 'W', 3, 'D', 4), begin_dt desc) as row_num
from a
) b
where row_num=1
搞定邦腾科技 http://www.partner-soft.com