select distinct(K0212) as 库存,isnull(Max(K0228),'1900-01-01') as 结存日期 from K02  where K0212 in(select distinct(B1519) from B07 
left join B15 with(readpast) on B0701=B1501 and B0702=B1517 where B0711='34') group by K0212
K0212和B1519相等

解决方案 »

  1.   

    --这样还好些。select K0212 as 库存, Max(K0228) as 结存日期 from K02 m ,
    (
      select distinct(B1519) B1519 from B07 
      left join B15 with(readpast) on B0701=B1501 and B0702=B1517 where B0711='34'
    ) n
    where m.K0212 = n.B1519
    group by m.K0212
      

  2.   

    ;WITH CTE AS
    (
      select distinct(B1519) from B07 
      left join B15 with(readpast) on B0701=B1501 and B0702=B1517 where B0711='34'
    )
    select distinct(K0212) as 库存
    ,isnull(Max(K0228)
    ,'1900-01-01') as 结存日期 
    from K02 join CTE C on  C.B1519=K0212
    group by K0212
      

  3.   

    你这个是多表查询,用in,exists估计效率都不高。实际上,你可以直接三表关联select m.K0212 as 库存, Max(m.K0228) as 结存日期
    from K02 m , B07 n , B15 t
    where m.K0212 = n.B1519 and n.B0701=t.B1501 and n.B0702=t.B1517 where n.B0711='34'
    group by group by m.K0212后面那些条件,具体在哪个表,你看看。
      

  4.   

    现在如果就像一楼那样的sql语句中in,应该怎么改成exists呢?
      

  5.   

    select distinct(K0212) as 库存,
    isnull(Max(K0228),'1900-01-01') as 结存日期 
    from K02  
    where  exists
    (
    select 1 
    from B07 
    left join B15 with(readpast) on B0701=B1501 and B0702=B1517 
    where B0711='34' and K0212=B1519
    ) group by K0212