select * from a where (select count(distinct price) from b where code=a.code)>1

解决方案 »

  1.   


    select
    a.code,quantity=(a.quantity-isnull(b.quantity,0))a.price
    from 
    a
    left join
    b on a.code=b.code
      

  2.   

    select
    a.code,quantity=(a.quantity-isnull(b.quantity,0)),a.price
    from
    (select code,quantity=sum(quantity),price from a group by code,price  )a
    left join
    (select code,quantity=sum(quantity),price from b group by code,price  )b on a.code=b.code
      

  3.   

    select '102' as code,1 as quantity,2 as price into #t1 select '102' as code,3 as quantity,2  as price into #t2select b.code,b.quantity-a.quantity,b.price from #T1 a, #t2 b where a.code=b.code and a.price=b.price
    drop table #t1
    drop table #t2
      

  4.   

    roy_88 能否有更好的方法,只输商品代码就行!
      

  5.   


    create table #a(code int,quantity int,price float)
    insert into #a select 101,20,4
    insert into #a select 101,40,2.5
    insert into #a select 102,10,12
    insert into #a select 102,5,8
    insert into #a select 102,30,15create table #b(code int,quantity int,price float)
    insert into #b select 101,20,4
    insert into #b select 102,10,12
    insert into #b select 102,10,15
    select a.code,quantity=isnull(a.quantity-b.quantity,a.quantity), a.price from #a a left join #b b
    on a.code=b.code and a.price=b.price 
    where a.code=102
    code        quantity    price
    ----------- ----------- ----------------------
    102         0           12
    102         5           8
    102         20          15(3 行受影响)
      

  6.   


    create table #a(code int,quantity int,price float)
    insert into #a select 101,20,4
    insert into #a select 101,40,2.5
    insert into #a select 102,10,12
    insert into #a select 102,5,8
    insert into #a select 102,30,15create table #b(code int,quantity int,price float)
    insert into #b select 101,20,4
    insert into #b select 102,10,12
    insert into #b select 102,10,15
    select a.code,quantity=isnull(a.quantity-b.quantity,a.quantity), a.price from #a a left join #b b
    on a.code=b.code and a.price=b.price 
    where a.code=102
    code        quantity    price
    ----------- ----------- ----------------------
    102         0           12
    102         5           8
    102         20          15(3 行受影响)