有这么一批数据,相同的物料凭证、物料号、数量的绝对值相同可以对应出一批一批每两条对应的数据,我想从数据库中像这样的数据相同的只查出其中一条,也就是查出的数据不再是两条对应而是成为一条,有没有高手能写出这样的sql来,先谢谢了。也不知道说清楚了没

解决方案 »

  1.   

    查询重复记录?
    用GROUP BY 可以解决。
      

  2.   

    select 物料凭证,物料号,sum(数量) from talbe 
           group by 物料凭证,物料号
      

  3.   

    用GROUP BY 可以解决 或根据情况用distinct
      

  4.   

     (不要高估你的汉语表达能力或者我的汉语理解能力)
       建议你列出你的表结构,并提供测试数据以及基于这些测试数据的所对应正确结果。
       参考一下这个贴子的提问方式http://topic.csdn.net/u/20091130/20/8343ee6a-417c-4c2d-9415-fa46604a00cf.html
       
       1. 你的 create table xxx .. 语句
       2. 你的 insert into xxx ... 语句
       3. 结果是什么样,(并给以简单的算法描述)
       4. 你用的数据库名称和版本(经常有人在MS SQL server版问 MySQL)
       
       这样想帮你的人可以直接搭建和你相同的环境,并在给出方案前进行测试,避免文字描述理解上的误差。   
      

  5.   

    select * from tbl where not exists(select 1 from tbl a where a.物料凭证=tbl.物料凭证 and a.物料号=tbl.物料号 and abs(a.数量)=abs(tbl.数量) and a.rowid>tbl.rowid);
      

  6.   

    select * from tbl where not exists(select 1 from tbl a where a.物料凭证=tbl.物料凭证 and a.物料号=tbl.物料号 and abs(a.数量)=abs(tbl.数量) and a.rowid>tbl.rowid);
      

  7.   

    查询重复记录? 
    用GROUP BY 可以解决。
      

  8.   

    你的意思是你要再查询第一条相同的记录后再查询另一条相同的记录也可以?rowid应该可以吧!
      

  9.   

    select * from
    (
      select A.*,row_number()over(partition by 物料凭证,物料号,数量 order by 物料凭证) rn from tbl A
    )
    where rn<2