有一个表n06,
里面有数据
n0601 n0602
331   23
331   -23
331   45
331   -26
331   -45现在如何查询可以把n0602数据相加为0的去掉,只得到-26这个数据 select * from n06该怎么写语句

解决方案 »

  1.   

    select sum(n0602) from n06
      

  2.   

    select * from n06 where abs(n0602) not in
    (select abs(n0602) from n06 group by abs(n0602) having count(1) > 1);
      

  3.   

    12:47:34 scott@TUNGKONG> select * from n06;N0601           N0602
    ---------- ----------
    331                23
    331               -23
    331                45
    331               -26
    331               -45已用时间:  00: 00: 00.01
    12:47:41 scott@TUNGKONG> select * from n06 where abs(n0602) not in
    12:47:44   2  (select abs(n0602) from n06 group by abs(n0602) having count(1) > 1);N0601           N0602
    ---------- ----------
    331               -26已用时间:  00: 00: 00.00
      

  4.   

    with t as 
    (
    select 331  n0601,23 n0602  from dual
    union all
    select 331 ,-23   from dual
    union all
    select 331 ,45  from dual 
    union all
    select 331 ,-29   from dual
    union all
    select 331 ,-28   from dual
    union all
    select 331 ,-45   from dual)
    select* from (
    select n0601,sum(n0602) as n0602 
    from t
    group by n0601,abs(n0602)
    )
    where n0602<>0
    这样就可以了
      

  5.   

    select * 
    from n06 t1
    where exists (select 1 from n06 t2 where t2.n0602 = - t1.n0602);
      

  6.   

    发错了,应该是not exists:
    select * 
    from n06 t1 
    where not exists (select 1 from n06 t2 where t2.n0602 = - t1.n0602);
      

  7.   

    楼上的,你这个是照作楼主的示例弄的吧!如果楼把其示例变一下,那你的sql不就又要改变了吗。
      

  8.   

    你是说那个WITH语句吗
    那位朋友只是做个实例而已
    所以就只列了6条数据下面跟着的那个SQL语句在楼主那里应该也是可以的
      

  9.   

    select* from (
    select n0601,sum(n0602) as n0602 
    from t
    group by n0601,abs(n0602)
    )
    where n0602<>0这个试试
      

  10.   

    with temp as(
    select '331' no601,23 no602 from dual
    union all
    select '331' no601,-23 no602 from dual
    union all
    select '331' no601,45 no602 from dual
    union all
    select '331' no601,-26 no602 from dual
    union all
    select '331' no601,-45 no602 from dual

    select no601,sum(no602) from temp group by no601 having sum(no602) <> 0结果:
    1 331 -26
      

  11.   

    不行的~with temp as(
    select '331' no601,26 no602 from dual
    union all
    select '331' no601,-26 no602 from dual
    union all
    select '331' no601,26 no602 from dual
    union all
    select '331' no601,-25 no602 from dual
    union all
    select '331' no601,-24 no602 from dual
    )
    select no601,sum(no602) from temp group by no601 having sum(no602) <> 0这样的结果是:-23
      

  12.   

    刚才COPY错了with temp as(
    select '331' no601,26 no602 from dual
    union all
    select '331' no601,26 no602 from dual
    union all
    select '331' no601,-24 no602 from dual
    union all
    select '331' no601,-26 no602 from dual
    union all
    select '331' no601,-25 no602 from dual
    )
    select no601,sum(no602) from temp group by no601 having sum(no602) <> 0
      

  13.   

    select * from sum_zero b where not exists(select * from sum_zero a where b.b=-a.b);select* from (
    select a,sum(b) as n0602 
    from sum_zero
    group by a,abs(b)
    )
    where n0602<>0