有以下数据 表A
Name Cash
--------------------
马振远 ¥-100.00
马振远 ¥100.00
高静 ¥150.00
高静 ¥-100.00
杨兰 ¥400.00
杨兰 ¥-400.00
于静波 ¥30.00
于静波 ¥60.00
王波 ¥90.00
张小发 ¥-90.00请教关于SQL查询 name列重复 且 Cash列互为相反数 方法希望得到以下查询结果:
Name Cash
--------------------
马振远 ¥-100.00
马振远 ¥100.00
杨兰 ¥400.00
杨兰 ¥-400.00
我已经找出Name列重复的方法.还需要判断Cash列互为相反数的方法.SELECT A.Name,A.Cash
FROM A
WHERE (((A.Name) In (select Name from A group by name having count(*)>1)) AND 需要判断互为相反数的方法);

解决方案 »

  1.   

    我已经在EXCEL中使用辅助列中加入
    =SUMPRODUCT(($A$2:$A$10=A2)*($B$2:$B$10=-B2))就可以筛选出A列重复记录 且 B列互为相反数
    但在SQL中就不知所措了
      

  2.   

    select a1.* from A a1,A a2 where a1.name = a2.name and a1.cash <> 0 and a1.cash+a2.cash = 0
      

  3.   

    --如果不要求是两两相反
    select * from tb where name in
    (select Name from tb group by name having sum(Cash) = 0)
      

  4.   

    create table tb(Name varchar(10) , Cash decimal(18,2))
    insert into tb values('马振远' ,-100.00 )
    insert into tb values('马振远' ,100.00 )
    insert into tb values('高静'   ,150.00 )
    insert into tb values('高静'   ,-100.00 )
    insert into tb values('杨兰'   ,400.00 )
    insert into tb values('杨兰'   ,-400.00 )
    insert into tb values('于静波' ,30.00 )
    insert into tb values('于静波' ,60.00 )
    insert into tb values('王波'   ,90.00 )
    insert into tb values('张小发' ,-90.00 )
    insert into tb values('AAAAAA' ,0 ) --增加的测试数据go--如果不要求是两两相反 
    select * from tb where name in (select Name from tb group by name having sum(Cash) = 0) 
    /*
    Name       Cash                 
    ---------- -------------------- 
    马振远        -100.00
    马振远        100.00
    杨兰         400.00
    杨兰         -400.00
    AAAAAA     .00(所影响的行数为 5 行)
    */--如果要求是两两相反 
    select * from tb where name in (select Name from tb group by name having sum(Cash) = 0 and count(*) = 2) 
    /*
    Name       Cash                 
    ---------- -------------------- 
    马振远        -100.00
    马振远        100.00
    杨兰         400.00
    杨兰         -400.00(所影响的行数为 4 行)
    */drop table tb
      

  5.   

    create table tb([Name] varchar(10) , Cash money )
    insert into tb values('马振远' , -100.00 )
    insert into tb values('马振远' , 100.00 )
    insert into tb values('高静', 150.00 )
    insert into tb values('高静', -100.00 )
    insert into tb values('杨兰' , 400.00 )
    insert into tb values('杨兰' , -400.00 )
    insert into tb values('于静波' , 30.00)
    insert into tb values('于静波' , 60.00)
    insert into tb values('王波' , 90.00)
    insert into tb values('张小发' , -90.00)
    select * from tb where [name] in (
    SELECT   [Name]FROM tb group by [name]
    having sum(Cash)=.0000)drop table tb