首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >IIF语句( Ms-Access )

IIF语句( Ms-Access )
EN

Stack Overflow用户
提问于 2012-07-05 14:59:14
回答 2查看 3.4K关注 0票数 3

我有三个表:

我使用下面的代码将它们连接在一起(我使用Delphi):

代码语言:javascript
复制
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 ';

正如您所看到的,当一个表中缺少记录时,它会向DbGrid返回Null,所以我想用"0“替换丢失的记录。我试过这段代码:

代码语言:javascript
复制
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 ';

但是没有什么变化,尽管这段代码运行得很好:

代码语言:javascript
复制
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';

我在这里做错了什么?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-07-06 04:15:24

ok找到了解决方案:

代码语言:javascript
复制
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) )  
票数 0
EN

Stack Overflow用户

发布于 2012-07-05 15:56:11

尝试去掉“group by IdArt”部分-它们似乎没有必要,因为您已经在WHERE子句中排除了所有其他部分。

否则,像这样的东西可能会改善您编写查询的方式:

代码语言:javascript
复制
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

不过,您可能需要对此进行调整

实际上,我更喜欢从上面写下这句话:

代码语言:javascript
复制
IIF( IsNull(sum(sold.Qte)), "111" , Format( sum(sold.Qte),"00.00") ) as [Quantite Vendu],

就像这样:

代码语言:javascript
复制
Format( sum( IIF( IsNull( sold.Qte ) , 0, sold.Qte ) ), "00.00" ) as [Quantite Vendu],

同样,我不是100%确定这是否有效,我只是在看你的函数的顺序和逻辑

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11339253

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档