这样排板好难看 UNION ALL 换成  UNION  先看看

解决方案 »

  1.   

    不好意思,讓大家一看就覺得太復雜了,其實很容易看懂的.如第一個查詢就是Waqty<=Stqty,則設A=0,第二個查詢就是Waqty>=Stqty,而在表IOSTORE中沒有任何該物料出入倉記錄,則設A=1,第三個查詢就是Waqty>=Stqty而在表IOSTORE中有該物料出入倉記錄,但在20060525這個日期之前沒有出入倉記錄,則設A=1,第四個查詢就是Waqty>=Stqty而在表IOSTORE中有該物料出入倉記錄,在20060525這個日期之前也有出入倉記錄,則設A=2,只不過我用UNION All把以上四個查詢連接起來了,請大家幫幫忙,好嗎,第一個查詢  select distinct Isnull(Ma_NO,'') as Materiel,isnull(Chname,'') as Name,isnull(SMname,'') as SmName,
    (Case When Waqty>=Stqty then 1 else 0 end) as IsWarn,A=0 FROM MASTORE WHERE Isnull(StQty,0)>=isnull(Waqty,0)  UNION ALL
    第二個查詢
    select distinct Isnull(Ma_NO,'') as Materiel,isnull(Chname,'') as Name,isnull(SMname,'') as SmName,
    (Case When Waqty>=Stqty then 1 else 0 end) as IsWarn,A=1 FROM MASTORE WHERE MA_NO NOT IN 
    (SELECT DISTINCT MA_NO FROM IOSTORE) AND Isnull(StQty,0)<=isnull(Waqty,0) UNION ALL
    第三個查詢
    select distinct Isnull(Ma_NO,'') as Materiel,isnull(Chname,'') as Name,isnull(SMname,'') as SmName,
    (Case When Waqty>=Stqty then 1 else 0 end) as IsWarn,A=1 FROM MASTORE WHERE 
    Isnull(StQty,0)<=isnull(Waqty,0) and MA_NO  IN 
    (SELECT DISTINCT MA_NO FROM IOSTORE WHERE mA_No in 
    (select a.ma_No from iostore a where ((exists(select max(b.indate) from iostore b where a.ma_no=b.ma_no 
    group by b.ma_no having max(b.indate)<=(select filedate from setdate))) or 
    (exists(select max(c.outdate) from iostore c where a.ma_no=c.ma_no group by c.ma_no 
    having max(c.outdate)<=(select filedate from setdate))))
     AND Isnull(StQty,0)<=isnull(Waqty,0)) AND Isnull(StQty,0)<=isnull(Waqty,0))  UNION All
    第四個查詢
    select distinct Isnull(Ma_NO,'') as Materiel,isnull(Chname,'') as Name,isnull(SMname,'') as SmName,
    (Case When Waqty>=Stqty then 1 else 0 end) as IsWarn,A=2 FROM MASTORE WHERE 
     Isnull(StQty,0)<=isnull(Waqty,0) and MA_NO  IN 
    (SELECT DISTINCT MA_NO FROM IOSTORE WHERE mA_No in 
    (select m.ma_No from iostore m where ((exists(select max(n.indate) from iostore n where m.ma_no=n.ma_no
    group by n.ma_no having max(n.indate)>(select filedate from setdate))) or 
    (exists(select max(p.outdate) from iostore p where m.ma_no=p.ma_no group by p.ma_no 
    having max(p.outdate)>(select filedate from setdate))))
     AND Isnull(StQty,0)<=isnull(Waqty,0)))
      

  2.   

    第三個sql語句
    select distinct Isnull(Ma_NO,'') as Materiel,isnull(Chname,'') as Name,isnull(SMname,'') as SmName,
    (Case When Waqty>=Stqty then 1 else 0 end) as IsWarn,A=1 FROM MASTORE WHERE 
    Isnull(StQty,0)<=isnull(Waqty,0) and MA_NO Not IN 
    (select a.ma_No from iostore a,setdate b where a.indate <b.filedate and a.outdate <b.filedate) AND Isnull(StQty,0)<=isnull(Waqty,0))
    第四個sql語句
    select distinct Isnull(Ma_NO,'') as Materiel,isnull(Chname,'') as Name,isnull(SMname,'') as SmName,
    (Case When Waqty>=Stqty then 1 else 0 end) as IsWarn,A=2 FROM MASTORE WHERE 
    Isnull(StQty,0)<=isnull(Waqty,0) and MA_NO  IN 
    (select a.ma_No from iostore a,setdate b where a.indate <b.filedate and a.outdate <b.filedate) AND Isnull(StQty,0)<=isnull(Waqty,0))而且 之間用 union 不要用union all
      

  3.   

    問題解決了,多謝gxqianqin(會飛)的幫肋!!!!!!!!!!!!!!!1