繁体   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