1

我有两个几乎相同的数据库(第二个是第一个的副本,然后用不同的公司/产品信息进行了修改)。有一个老化报告从来没有正常工作,我必须修复。我让它在一个数据库(A)上工作,但第二个(B)引发了“无效”错误。

报表查询是从另一个查询生成的,它基本上是抓取订单信息和客户信息。从那里,有问题的查询根据订单的发货时间将订单分成几块,并返回余额。错误消息弹出以下代码段:

Round(Sum(IIf((Date()-[ShipDate] Is Null),[LineTotal]+(CLng([LineTotal]*[SalesTaxRate]*100)/100)+[FreightCharge]-nz([Total Payments]),0)),2) AS Current_Unshipped

如果 Is Null 更改为其他任何值,则查询将正常执行。如果我尝试引入任何“如果变量为空”代码,则会引发错误。我尝试了以下方法,但无济于事。

Round(Sum(IIf((nz[ShipDate]),[LineTotal]+(CLng([LineTotal]*[SalesTaxRate]*100)/100)+[FreightCharge]-nz([Total Payments]),0)),2) AS Current_Unshipped

如果发货日期为空,我还有另一列会返回“NULL”,这出乎意料地有效。第一个代码段在另一个数据库中完美运行,所以我很困惑。我需要能够为未发货的订单生成一列,因此缺少 [ShipDate]。我不知道为什么代码不起作用。

整体

(CLng([LineTotal]*[SalesTaxRate]*100)/100)+[FreightCharge]-nz([Total Payments]),0))

代码的一部分是余额是如何生成的,它是如何工作的。它还在数据库中大约一百万个位置实现——设计者认为每次运行查询时都会自动生成总余额,而不是存储在某个地方。

整个查询代码也发布在下面。如果有人能指出我正确的方向,那将有很大帮助。

SELECT DISTINCTROW 
    Round(Sum(IIf((Date()-[ShipDate]<31),[LineTotal]+(CLng([LineTotal]*[SalesTaxRate]*100)/100)+[FreightCharge]-nz([Total Payments]),0)),2) AS [Current],
    Round(Sum(IIf((Date()-[ShipDate] Is Null),[LineTotal]+(CLng([LineTotal]*[SalesTaxRate]*100)/100)+[FreightCharge]-nz([Total Payments]),0)),2) AS Current_Unshipped,
    Round(Sum(IIf((Date()-[ShipDate])<61 And (Date()-[ShipDate]>30),[LineTotal]+(CLng([LineTotal]*[SalesTaxRate]*100)/100)+[FreightCharge]-nz([Total Payments]),0)),2) AS [31-60 Days], 
    Round(Sum(IIf((Date()-[ShipDate])<91 And (Date()-[ShipDate]>60),[LineTotal]+(CLng([LineTotal]*[SalesTaxRate]*100)/100)+[FreightCharge]-nz([Total Payments]),0)),2) AS [61-90 Days],
    Round(Sum(IIf((Date()-[ShipDate])>90,[LineTotal]+(CLng([LineTotal]*[SalesTaxRate]*100)/100)+[FreightCharge]-nz([Total Payments]),0)),2) AS [91+ Days],
    Round(Sum(nz([LineTotal])+(CLng(nz([LineTotal])*nz([SalesTaxRate])*100)/100)+nz([FreightCharge])-nz([Total Payments])),2) AS Balance,
    [Receivables Aging Report Query].CompanyName, 
    [Receivables Aging Report Query].OrderDate, 
    [Receivables Aging Report Query].ShipDate, 
    [Receivables Aging Report Query].OrderID, 
    nz((Round(Date()-[ShipDate])),"NULL") AS Span, 
FROM [Receivables Aging Report Query]
GROUP BY 
    [Receivables Aging Report Query].CompanyName, 
    [Receivables Aging Report Query].OrderDate, 
    [Receivables Aging Report Query].ShipDate, 
    [Receivables Aging Report Query].OrderID
HAVING(((Round(Sum(nz([LineTotal])+(CLng(nz([LineTotal])*nz([SalesTaxRate])*100)/100)+nz([FreightCharge])-nz([Total Payments])),2))>0 Or (Round(Sum(nz([LineTotal])+(CLng(nz([LineTotal])*nz([SalesTaxRate])*100)/100)+nz([FreightCharge])-nz([Total Payments])),2))<0) AND (([Receivables Aging Report Query].ShipDate) Is Not Null)) OR (((Round(Sum(nz([LineTotal])+(CLng(nz([LineTotal])*nz([SalesTaxRate])*100)/100)+nz([FreightCharge])-nz([Total Payments])),2))>0 Or (Round(Sum(nz([LineTotal])+(CLng(nz([LineTotal])*nz([SalesTaxRate])*100)/100)+nz([FreightCharge])-nz([Total Payments])),2))<0) AND (([Receivables Aging Report Query].ShipDate) Is Null))
ORDER BY 
    [Receivables Aging Report Query].OrderID;
4

1 回答 1

0

用这个:

Round(Sum(IIf(DateDiff("d",Date(),[ShipDate])=0,[LineTotal]+CLng([LineTotal]*[SalesTaxRate]*100)/100+[FreightCharge]-Nz([Total Payments],0),0)),2) AS Current_Unshipped
于 2016-03-19T09:18:43.497 回答