计算同一列中访问日期和保存最新代码之间的天数

时间:2016-06-08 08:05:30

标签: ms-access-2007 access

我需要一些帮助。我正在寻找网络,并尝试过仍然得到一个" 0"回答。

orderid   accno   invdate       next date      days
1          a001   31/05/2016    03/06/2016     3
2          a001   03/06/2016    04/06/2016     2
3          a001   04/06/2016    02/06/2016        this is where it is wrong
4          a002   02/06/2016

我试过以下

DateDiff("d",[InvDate],[NextDate]) AS Days 

这是我在Access查询中的SQL

SELECT Rental.OrderID, Rental.AccNo, Rental.Customer, Rental.InvDate, Rental.Invoice, NZ(Sum([ArgD]+[ActD]+[OxgD])) AS QtyDel, NZ(Sum([ArgR]+[ActR]+[OxyR])) AS QtyRet, NZ(Sum(DSum("(ArgD+ActD+OxgD)-(ArgR+ActR+OxyR)+NZ([BF])","[Rental]","[Rental].[OrderID]=" & Rental.OrderID & " AND [Rental].[OrderID]<=" & Rental.OrderID))) AS RT, AllDays.InvDate, Sum(DSum("(ArgD+ActD+OxgD)-(ArgR+ActR+OxyR)+NZ([BF])","[Rental]","[Rental].[OrderID]=" & Rental.OrderID & " AND [Rental].[OrderID]<=" & Rental.OrderID)) AS DT, AllDays.NextDate, DateDiff("d",AllDays.InvDate,[NextDate]) AS DAYS, Rental.ArgD, Rental.ActD, Rental.OxgD, Rental.ArgR, Rental.ActR, Rental.OxyR, Rental.Period, Rental.Rate, Rental.InvRental, Rental.BF, Rental.BF, "cyl" AS str, "Days" AS cyldays, Rental.NitD, Rental.NitR, Rental.MagD, Rental.MagR, Rental.OtherD, Rental.OtherR
FROM (SELECT OrderID, AccNo, InvDate, Nz((SELECT     MIN(InvDate)   FROM Rental T2   WHERE T1.accno = T2.accno   AND T2.InvDate > T1.InvDate), DateSerial(YEAR(T1.InvDate), MONTH(T1.InvDate) + 1, 1)) AS NextDate FROM Rental AS T1)  AS AllDays INNER JOIN Rental ON AllDays.OrderID = Rental.OrderID
GROUP BY Rental.OrderID, Rental.AccNo, Rental.Customer, Rental.InvDate, Rental.Invoice, AllDays.InvDate, AllDays.NextDate, Rental.ArgD, Rental.ActD, Rental.OxgD, Rental.ArgR, Rental.ActR, Rental.OxyR, Rental.Period, Rental.Rate, Rental.InvRental, Rental.BF, Rental.NitD, Rental.NitR, Rental.MagD, Rental.MagR, Rental.OtherD, Rental.OtherR
ORDER BY Rental.OrderID;

1 个答案:

答案 0 :(得分:0)

您只需要过滤一个accno

WHERE T1.accno = T2.accno And T2.InvDate > T1.InvDate) AS NextDate