2016-04-10 58 views
1

在配置單元的特定字段中刪除具有重複值的連續行的有效方法是什麼?例如:刪除配置單元中的連續行

輸入:

ID field1 field2 date 
1 a  b  2015-01-01 
1 a  b  2015-01-02 
2 e  d  2015-01-03 

輸出:

ID field1 field2 date 
1 a  b  2015-01-01 
2 e  d  2015-01-03 

在此先感謝

回答

2

一種方式來去除連續重複是使用lag檢查以前id只保留以前的id是不同的行:

select * from (
    select * , 
     lag(id) over (order by date) previous_id 
    from mytable 
) t where t.previous_id <> t.id 
or t.previous_id is null -- accounts for the 1st row 

如果你還需要檢查field1field2,那麼你可以添加獨立lag語句爲每個字段:

select * from (
    select * , 
     lag(id) over (order by date) previous_id, 
     lag(field1) over (order by date) previous_field1 
    from mytable 
) t where (t.previous_id <> t.id and t.previous_field1 <> field1) 
or t.previous_id is null 
+0

非常感謝您@Fuzzy Tree..this方法的工作.. :) – user2895589

+0

@ user2895589很高興它有幫助 – FuzzyTree