How to replace a complex SQL MINUS query with LEFT OUTER JOIN equivalent

后端 未结 3 2032
轮回少年
轮回少年 2021-01-18 11:53

Trying to figure how how to replace the following, with equivalent left outer join:

select distinct(a.some_value)
from table_a a, table_b b
where a.id = b.a_         


        
3条回答
  •  不要未来只要你来
    2021-01-18 12:18

    I'm not convinced a left join is the way to go but I believe it would look like this:

    select *
    from
         (
          select *
          from a
          where a.CreateDate >= '12/1/2013'
            and a.CreateDate < '1/1/2014')December
         left join(
                   select *
                   from b
                   where b.CreateDate < '12/1/2013')PriorMonths on December.Value = PriorMonths.Value
    where PriorMonths.Value is null
    

    How about a "not exists" clause?

    TechnetArticle

    S/O question about In, Left Join, and Not Exists

    select *
    from a
    where a.CreateDate >= '12/1/2013'
      and a.CreateDate < '1/1/2014'
      and not exists(
                     select *
                     from b
                     where b.CreateDate < '12/1/2013'
                       and b.[Value] = a.Value)
    

提交回复
热议问题