报告中行之间毫秒访问的日期差异

Date difference in ms access between row in report

我有一个名为 CustomerLog 的 table 字段跟随

ID    CustomerID      BuyingDate
1     1001            12/02/2020(dd/mm/yyyy)
2     2023            10/02/2020
3     2024            14/02/2020
4     1001            11/03/2020
5     2023            12/03/2020
6     2024            20/03/2020
7     1001            23/04/2020
8     2023            23/04/2020
9     2024            25/04/2020

现在我需要如下查询

ID    CustomerID      BuyingDate      Difference
1     1001            12/02/2020      0
4     1001            11/03/2020      28
7     1001            23/04/2020      43

如果有人能提供示例文件,那将非常有帮助,因为我已经阅读了一些有关此类型的内容,但无法理解。post。

您可以做的是使用子查询来获取客户的上一个日期。您还需要使用 Nz (Null to Zero) 函数将每个客户的第一个先前日期设置为与购买日期相同,然后使用 DateDiff 获得天数差异.您的 SQL 应该类似于:

SELECT 
    C.ID, 
    C.CustomerID, 
    C.BuyingDate, 
    Nz((SELECT TOP 1 C1.BuyingDate FROM CustomerLog AS C1 WHERE C1.CustomerID=C.CustomerID AND C1.BuyingDate<C.BuyingDate ORDER BY C1.BuyingDate DESC),C.[BuyingDate]) AS PreviousDate, 
    DateDiff("d",PreviousDate,C.BuyingDate) AS Difference
FROM CustomerLog AS C;

此致,