在查询中访问DateDiff

时间:2017-06-15 20:30:15

标签: ms-access

我有一个查询,其中应该从当天的余额中减去前一天的余额。这是我的SQL

   SELECT [206_Src_045].[Eff Date], [103_Wire_Log].[Eff Date], [103_Wire_Log].Portfolio, [206_Src_045].Activity, [206_Src_045].[SumOfUSD Balance] AS Today_Amount, [103_Wire_Log].Balance AS Yesterday_Amount, Format([Today_Amount]-[Yesterday_Amount],"Fixed") AS Difference
FROM 206_Src_045 INNER JOIN (103_Wire_Log INNER JOIN (54_Port_Activity INNER JOIN 205_Wire_Details_Clean ON [54_Port_Activity].MetlifePortCode = [205_Wire_Details_Clean].Portfolio) ON [103_Wire_Log].Portfolio = [205_Wire_Details_Clean].Portfolio) ON [206_Src_045].Activity = [54_Port_Activity].ActivityCode
WHERE ((([206_Src_045].[Eff Date])=DMax("[Eff Date]","206_Src_045")) AND ((DateDiff('d',[206_Src_045].[Eff Date],[103_Wire_Log].[Eff Date]))="1") AND (([205_Wire_Details_Clean].Negative)=IIf([206_Src_045].[SumOfUSD Balance]-[103_Wire_Log].[Balance]<0,"Yes","No")));

((DateDiff(&#39; d&#39;,[206_Src_045]。[Eff Date],[103_Wire_Log]。[Eff Date]))=&#34; 1&#34;

更改

的输出
  ---------------------------------------------------------------------------------------  ---------------------------------------------------------------------------------------------
|  206_Src_045.Eff   |     103_Wire_Log.Eff Date      |     Portfolio      |      Activity      |     Today_Amount      |         Yesterday_Amount         |      Difference       |
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|         06/15/2017 | 06/13/2017                     | 7TZ                | 4200007025         |                -10000 | 21770.83                         | -31770.83             |
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|         06/15/2017 | 06/14/2017                     | 7TZ                | 4200007025         |                -10000 | 1000                             | -11000.00             |
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

什么都没有,当第二行,日期为6/15/2017和6/14/2017,是正确的输出。我怎么用错了?我在另一个查询中以类似的方式正确使用它。

1 个答案:

答案 0 :(得分:0)

仔细检查日期的顺序。如果日期顺序相反,DateDiff将返回一个负数。要解决这些问题,请将调用包裹在Abs()中,如Abs(DateDiff(...)) = 1或使用DateDiff(...) In (-1, 1)

最后的调试提示:值得在select子句中添加一个有问题的表达式,以便您可以实际看到表达式返回的值:SELECT DateDiff('d',[206_Src_045].[Eff Date],[103_Wire_Log].[Eff Date]) As ElapsedDays, ...这将显示值{{1} }。