日期:2014-05-17  浏览次数:20914 次

去重
A表:A    B    C    D 
      1   ab    a    q
      2   ab    q    a
      3   ab    e    d
      4   ab    d    e
      5   pd    n    m
      6   pd    m    n
      7   pd    v    b
      8   pd    b    v
      9   cd    f    h
     10   cd    h    f
目的是相当得到结果:
     A    B    C    D 
    1   ab    a    q
    3   ab    e    d
    5   pd    n    m
    7   pd    v    b
    9   cd    f    h
数据就是这样
还有就是表中只有BCD三个字段  后来将rowid作为主键(也就是这里的A列)

------解决方案--------------------
使用分析函数LAG

   select m.a, m.b, m.c, m.d
     from (select a,
                  b,
                  c,
                  d,
                  concat(c, d) r,
                  lag(concat(d, c), 1, null) over(order by a) re
             from t) m
    where m.r <> m.re
       or m.re is null

         A B  C D
---------- -- - -
         1 ab a q
         3 ab e d
         5 pd n m
         7 pd v b
         9 cd f h

------解决方案--------------------
   with t as(
     select 1 A,'ab' B,'a' C,'q' D from dual