select * from ta,tb where ta.codea=tb.codea and ta.codeb=tb.codeb and ta.price<tb.price
union all 
select * from ta,tb where ta.codea=tb.codea and ta.codeb<>tb.codeb and tb.codeb='99' and ta.price<tb.price

解决方案 »

  1.   

    select codea, codeb, pricea 
    from(
     select a.codea, a.codeb, a.price as pricea,
     case when b.price is null then (select top 1 price 
     from tb where tb.codeb = '99' and tb.codea =a.codea)
     else b.price end as priceb
     from ta as a
     left join tb as b
     on a.codea = b.codea and a.codeb=b.codeb
    ) c
    where pricea < priceb
      

  2.   

    select A.codea, A.codeb, A.price, B.price from ta A 
    inner join tb B on B.codea = A.codea and B.codeb = A.codeb
    where A.price < B.price
    union all
    select A.codea, A.codeb, A.price, B.price from ta A
    left join tb B on B.codea = A.codea and B.codeb = 99 
    where not exists (select 1 from tb where codea = A.codea and codeb = A.codeb)
    and A.price < B.price