有一销售单表(sale_t)中有以下字段:
saleno(单号)           saleprice(售价)realprice(实际售价) 
200710010011275247 10 10
200710010011275034 10 7
200710010011274266 10 9
200710010011274185 9 10
200710010011274383 10 9
200710010011275463 10 10

我想查找两张销售单售价是相同,实际售价也相同,然后就是售价和实际售价都一样的;
比如销售单A:200710010011274266  10 9,售价是10元,实际售价是9元,我想查的是其它销售单中售价是10元,实际售价是9元,但是其它销售单中售价和实际售价要是10元和9元,不能是10元和8元或者8元和9元。sale_t最终我想要的结果是:200710010011274383 10 9
                           200710010011274266 10 9
                           200710010011275247 10 10
                        200710010011275463 10 10麻烦各位高手帮忙,我找了很多关于查找重复记录的语句都没有结果,多谢!
                        

解决方案 »

  1.   


    create table T (saleno varchar(100),saleprice int, realprice int )insert into T select '200710010011275247', 10, 10 
    insert into T select '200710010011275034', 10, 7 
    insert into T select '200710010011274266', 10, 9 
    insert into T select '200710010011274185', 9 ,10 
    insert into T select '200710010011274383', 10, 9 
    insert into T select '200710010011275463', 10, 10 
    select a.*
    from T as a
        inner join 
        (
            select saleprice,realprice 
            from T 
            group by saleprice,realprice 
            having Count(*)>1
        ) as b on a.saleprice=b.saleprice and a.realprice=b.realpricedrop table T
      

  2.   

    select * from salt_t where cast(saleprice as varchar) + ',' + cast(realprice as varchar) in
    (
      select cast(saleprice as varchar) + ',' + cast(realprice as varchar) from
        (
          select saleprice,realprice from sale_t group by saleprice,realprice having count(*) > 1
        ) t
    )
      

  3.   

    create table tb(saleno varchar(20),saleprice int,realprice int)
    insert into tb values('200710010011275247', 10, 10) 
    insert into tb values('200710010011275034', 10, 7 )
    insert into tb values('200710010011274266', 10, 9 )
    insert into tb values('200710010011274185', 9 , 10 )
    insert into tb values('200710010011274383', 10, 9 )
    insert into tb values('200710010011275463', 10, 10 )
    go
    select * from tb where cast(saleprice as varchar) +  ', ' + cast(realprice as varchar) in 

      select cast(saleprice as varchar) +  ', ' + cast(realprice as varchar) from 
        ( 
          select saleprice,realprice from tb group by saleprice,realprice having count(*)  > 1 
        ) t 
    )
    order by saleprice , realprice
    drop table tb/*
    saleno               saleprice   realprice   
    -------------------- ----------- ----------- 
    200710010011274266   10          9
    200710010011274383   10          9
    200710010011275463   10          10
    200710010011275247   10          10(所影响的行数为 4 行)
    */
      

  4.   

    select * into #a
    from sale_t
    select * from sale_t
    where saleprice=#a.saleprice
    and realprice =#a.realprice
    and saleno<>#a.saleno 
      

  5.   

    多谢各位的帮忙,我也试用INNER JION的方法,可能我没有注意细节。多谢啦