admin

计算两个时间记录之间的差异

sql

我发现了许多与我的问题相似但又不完全相同的问题,但是我发现这些问题都没有帮助我,因此这里是新问题。

预期输出:如果用户相同,则一个时间记录与上一个时间记录之间的差异。

样表:

+-------+--------+-------------------+------+   
| RowID | User   | Godzina_transakcji| Diff |   
+-------+--------+-------------------+------+   
|    1  | AAA    | 14:23:03          |      |   
|    2  | AAA    | 14:23:57          |      |   
|    3  | AAA    | 14:25:03          |      |   
|    4  | BBB    | 03:37:23          |      |   
|    5  | BBB    | 03:39:21          |      |   
|    6  | BBB    | 05:23:11          |      |   
+-------+---------+------------------+------+

因此,对于第1行,查询应该给0,对于2行,查询应该给= 14:23:57-14:23:03,依此类推。

到目前为止,我有这样的事情(基于@Tom Collins在其他问题中的回答),但由于某种原因,它会显示[错误]
1个

 SELECT tblTemp2.RowID,
       tblTemp2.User,
       tblTemp2.Godzina_transakcji,
       Nz(Dmax("Godzina_transakcji", "User", "(Godzina_transakcji <" & [Godzina_transakcji] &
                                             ") and (User = '" & [User]
                                                        &"')"), 0) - [Godzina_transakcji]  AS Diff
FROM   tblTemp2;

我在这做错了什么?另外,在另一步骤中,我将不得不考虑在两者之间经过午夜。

结论:
如果有人会寻找类似的问题,那么这里的代码对我有用。谢谢古斯塔夫,你是神!

SELECT 
    tblTemp2.RowID,
    tblTemp2.User,
    tblTemp2.Godzina_transakcji,
    Format((    Select TimeValue(T.Godzina_transakcji) 
                From tblTemp2 As T
    Where tblTemp2.RowID = T.RowID + 1  And tblTemp2.User = T.User ) - TimeValue([Godzina_transakcji]), "hh:nn:ss") As Diff
FROM
   tblTemp2;

阅读 257

收藏
2021-07-01

共1个答案

admin

那是因为您将时间存储为文本。您必须转换为真实时间才能直接进行比较:

SELECT tblTemp2.RowID,
       tblTemp2.User,
       tblTemp2.Godzina_transakcji,
       Nz(Dmax("Godzina_transakcji", "tblTemp2", "(TimeValue([Godzina_transakcji]) < #" & [Godzina_transakcji] & "#) and (User = '" & [User] & "')"), #00:00#) - TimeValue([Godzina_transakcji])  AS Diff
FROM   tblTemp2;

要使用ID:

SELECT 
    tblTemp2.RowID,
    tblTemp2.User,
    tblTemp2.Godzina_transakcji,
    (Select TimeValue(T.Godzina_transakcji) 
    From tblTemp2 As T
    Where T.RowID = tblTemp2.RowID + 1 And T.User = tblTemp2.User) - TimeValue([Godzina_transakcji]) As Diff
FROM
   tblTemp2;
2021-07-01