如何解决Oracle:是否可以仅在重复出现时过滤掉重复项?
预先感谢您的帮助。
我有一个表,该表包含驾驶员的行程信息。有时,行程似乎有相同的停留时间(但相隔几天)。我希望能够查询表并过滤出地址相同且日期连续的任何记录。
这可能吗?
再次感谢, 乔什
解决方法
with tst as(
select timestamp '2020-08-01 00:00:00' dt,'123 street' loc from dual
union all
select timestamp '2020-08-01 00:00:00','89 street' from dual
union all
select timestamp '2020-08-02 00:00:00','456 airport' from dual
union all
select timestamp '2020-08-04 00:00:00','456 airport' from dual
union all
select timestamp '2020-08-05 00:00:00','67 street' from dual
union all
select timestamp '2020-08-06 00:00:00','89 street' from dual
union all
select timestamp '2020-08-07 00:00:00','123 street' from dual
)
select dt,loc
from (
select dt,loc,nvl(lag(loc) over(order by dt),'FIRST_ROW') prev_loc
from tst
) where loc <> prev_loc;
,
另一种方法是使用RPAD(CAST(lng AS VARCHAR),3,'.')
,为连续的行分配一个组号,然后计算每个组的行数。(在asktom网站上找到)。
Tabibitosan method
从12c开始可用的另一种使用with test_data as(
select date'2020-08-01' dt,'123 street' loc from dual
union all
select date '2020-08-01','89 street' from dual
union all
select date '2020-08-02','456 airport' from dual
union all
select date '2020-08-04','456 airport' from dual
union all
select date '2020-08-05','67 street' from dual
union all
select date '2020-08-06','89 street' from dual
union all
select date '2020-08-07','123 street' from dual
)
select max(dt),loc
from
(
select t.*,row_number() over (order by dt) -
row_number() over (partition by loc order by dt) grp
from test_data t
)
group by grp,loc
having count(*) > 1;
的方法。使用过的match_recognize
表示重复一次或多次
了解更多{1,}
here
match_recognize
游乐场:Dbfiddle
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。