簡體   English   中英

Ms-Access中的IIF聲明

[英]IIF Statement in Ms-Access

我有這3個表: 在此輸入圖像描述

我正在使用此代碼將它們連接在一起(我正在使用Delphi):

ADOQ.SQL.Text := 'select a.IdArt as [Code d''Article], '+
                   'a.Nom,'+
                   'Str(a.Prix)+" TND" as Prix, '+
                   '(select Str(sum(QteEntree))+" "+a.unit from Stock where IdArt = a.IdArt group by IdArt) as [Quantite Entree],' +
                   '(select Str(sum(Qte))+" "+a.unit from Sold where IdArt = a.IdArt group by IdArt) as [Quantite Vendu],'+
                   'Str((select sum(QteEntree) from Stock where IdArt = a.IdArt group by IdArt) -' +
                   '(select sum(Qte) from Sold where IdArt = a.IdArt group by IdArt))+" "+a.unit as [Quantite Existe]'+

                   'from Article a ';

在此輸入圖像描述

如您所見,當一個表中缺少記錄時,它將Null返回到DbGrid,因此我想用“0”替換該缺失記錄。 我試過這段代碼:

ADOQ.SQL.Text := 'select a.IdArt as [Code d''Article], '+
                   'a.Nom,'+
                   'Str(a.Prix)+" TND" as Prix, '+
                   '(select Str(sum(QteEntree))+" "+a.unit from Stock where IdArt = a.IdArt group by IdArt) as [Quantite Entree],' +
                   '(select IIF( IsNull(sum(Qte)), "111" , Format( sum(Qte),"00.00") ) from Sold where IdArt = a.IdArt group by IdArt) as [Quantite Vendu],'+
                   'Str((select sum(QteEntree) from Stock where IdArt = a.IdArt group by IdArt) -' +
                   '(select sum(Qte) from Sold where IdArt = a.IdArt group by IdArt))+" "+a.unit as [Quantite Existe]'+

                   'from Article a ';

但沒有改變,雖然這段代碼完美無缺:

ADOQ.SQL.Text := 'Select a.IdArt,IIF(( IsNull( s.qte) ), "00,00" , Format( (s.qte),"00.00") ) from Article a left join sold s on s.IdArt = a.IdArt';

在此輸入圖像描述

我在這做錯了什么?

嘗試取出“由IdArt組”部分 - 它們似乎沒必要,因為您已經排除了WHERE子句中的所有其他部分。

否則,這樣的事情可能會改善您編寫查詢的方式:

select 
    a.IdArt as [Code d''Article], 
    a.Nom,
    Str(a.Prix)+" TND" as Prix, 
    Str(sum(stock.QteEntree))+" "+a.unit as [Quantite Entree],
    IIF( IsNull(sum(sold.Qte)), "111" , Format( sum(sold.Qte),"00.00") ) as [Quantite Vendu],
    Str(sum(stock.QteEntree) - sum(sold.Qte))+" "+a.unit as [Quantite Existe]

from Article a 
left join stock on a.IdArt = stock.IdArt
left join sold on a.IdArt = sold.IdArt
group by a.IdArt, a.Nom, a.Prix, a.unit

你可能需要調整一下

實際上,我更願意從上面寫下這句話:

IIF( IsNull(sum(sold.Qte)), "111" , Format( sum(sold.Qte),"00.00") ) as [Quantite Vendu],

改為:

Format( sum( IIF( IsNull( sold.Qte ) , 0, sold.Qte ) ), "00.00" ) as [Quantite Vendu],

Againt,我不是100%肯定會工作,我只是看看你的功能的順序和邏輯

確定找到了解決方案:

iif( isnull((select sum(QteEntree) from Stock where IdArt = a.IdArt group by IdArt)) ,0, (select sum(QteEntree) from Stock where IdArt = a.IdArt group by IdArt) )  

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM