同一个表里
ID Name price
1 12   1.1
   
2 12   5
3 13   2 
4 13   3
5 14   4
6 14   1.2如何得出 price之差 <0.2 都认为是符合的记录
比如上面的结果应该是
1 12 1.1
6 14 1.2

解决方案 »

  1.   

    select a.id,a.name,a.price from table1 a,table1 b where a.id >< b.id and abs(a.price - b.price) < 0.2
      

  2.   

    select * 
    from tab a 
    where exists(select * 
               from tab b 
               where abs(a.price-b.price)<0.2 and a.id <> b.id)
      

  3.   

    谢谢上面的两位解答!为什么处理几万条记录以上的时候,
    oracle处理速度非常慢,有没有效率高点的语句?
      

  4.   

    select tab1.id, tab1.name, tab1.price
      from (select id,
                   name,
                   price,
                   LAG(price, 1, price + 1) OVER(ORDER BY price) as gg,
                   lead(price, 1, price + 1) OVER(ORDER BY price) as ff
              from bbb) tab1
     where abs(tab1.price - tab1.gg) < 0.2
        or abs(tab1.price - tab1.ff) < 0.2