问题
I found many questions similar to mine but not quite the same, and nothing I found in them helped me so here is new one.
Expected output: Difference between one time record and previous one if the User is the same.
Sample table:
+-------+--------+-------------------+------+
| 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 | |
+-------+---------+------------------+------+
So query should for row 1 give 0, for for 2 give =14:23:57-14:23:03 and so on.
So far I have something like this (based on @Tom Collins answer in other question) but for some reason it gives [Error]
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;
What am I doing wrong in this one? Also, on another step I will have to take account of passing midnight in between.
Conclusion:
If anyone will look for similar problem here is code that worked for me. Thanks Gustav, you are a God!
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;
回答1:
That is because you times stored as text. You must convert to true time to compare directly:
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;
To use the 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;
回答2:
You can use a combination of DMax
and DLookUp
, although on a large dataset you'll probably get performance issues:
SELECT RowID, User, Godzina_transakcji,
Nz(DMax("RowId","tblTemp2","User='" & [User] & "' and RowId<" & [RowId]),0) AS Prev_RowID,
DLookUp("Godzina_transakcji","tblTemp2","User='" & [User] & "' AND RowId=" & [Prev_RowId]) AS Prev_Godzina_transakcji,
DateDiff("s",[Prev_Godzina_transakcji],[Godzina_transakcji]) AS Diff_in_sec
FROM tblTemp2;
I've used the RowId
instead of the time in the lookup with the assumption that your data is stored sorted by the time.
The DMax
gets the previous RowId
based on the User
:
Nz(DMax("RowId","tblTemp2","User='" & [User] & "' and RowId<" & [RowId]),0) AS Prev_RowID
The DLookUp
get the previous time based on the user and previous RowId
:
DLookUp("Godzina_transakcji","tblTemp2","User='" & [User] & "' AND RowId=" & [Prev_RowId]) AS Prev_Godzina_transakcji
You then calculate the difference using DateDiff
:
DateDiff("s",[Prev_Godzina_transakcji],[Godzina_transakcji]) AS Diff_in_sec
This will give you the time difference in seconds.
来源:https://stackoverflow.com/questions/65184049/calculate-difference-between-two-time-records