【问题标题】:IIF Statement in Ms-AccessMs-Access 中的 IIF 语句
【发布时间】:2012-07-05 06:59:13
【问题描述】:

我有这 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';

我在这里做错了什么?

【问题讨论】:

  • 请不要发布数据图片,与最无格式的数据样本相比,它的用处很小。可以使用样本来重现问题,但没有人会从图片中输入数据来测试它。
  • 您是否在 Ms-Access 中测试过上述查询?

标签: sql delphi ms-access


【解决方案1】:

尝试去掉“group by 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],

再说一次,我不能 100% 确定这会起作用,我只是在看你的函数的顺序和逻辑

【讨论】:

【解决方案2】:

好的,找到了解决方案:

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) )  

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-10-28
    • 2023-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-23
    • 1970-01-01
    相关资源
    最近更新 更多