NauticalGent
Ignore List Poster Boy
- Local time
- Today, 15:42
- Joined
- Apr 27, 2015
- Messages
- 6,891
I am having a problem with this query, it is giving me the wrong results.
Here it the result:
Here is the desired result:
What I am trying to do is use the StartDate of the next record as the EndDate for the current record until we get to the last record, in that case the EndDate will be the current date/time. I could use a sub to do this, but I am sure it can be done with a query, but obviously I am not doing it right.
Any ideas?
Code:
INSERT INTO tmp_TimeInPhase
(PRON, [TimeStamp], [Current Proc Point], ProcPointStartDate, EndDate, TimeInProc, [Updated By], PALT)
SELECT
p.PRON,
a.TimeStamp,
c.[Current Proc Point],
a.ProcPointStartDate,
IIf(
(SELECT Min(b.ProcPointStartDate)
FROM tbl_Actual_PROC_Point AS b
WHERE b.PRON_ID = a.PRON_ID
AND b.ProcPointStartDate > a.ProcPointStartDate) Is Not Null,
DateAdd("d", -1, (SELECT Min(b.ProcPointStartDate)
FROM tbl_Actual_PROC_Point AS b
WHERE b.PRON_ID = a.PRON_ID
AND b.ProcPointStartDate > a.ProcPointStartDate)),
Date()
) AS EndDate,
Int(
IIf(
(SELECT Min(b.ProcPointStartDate)
FROM tbl_Actual_PROC_Point AS b
WHERE b.PRON_ID = a.PRON_ID
AND b.ProcPointStartDate > a.ProcPointStartDate) Is Not Null,
DateAdd("d", -1, (SELECT Min(b.ProcPointStartDate)
FROM tbl_Actual_PROC_Point AS b
WHERE b.PRON_ID = a.PRON_ID
AND b.ProcPointStartDate > a.ProcPointStartDate)),
Date()
) - a.ProcPointStartDate
) AS TimeInProc,
a.[Updated By],
c.PALT
FROM
(tbl_PRON AS p
INNER JOIN tbl_Actual_PROC_Point AS a ON p.PRON_ID = a.PRON_ID)
INNER JOIN tbl_Current_PROC_Point AS c ON a.CurrentProcPointID = c.CurrentProcPointID
WHERE
(((p.PRON_ID) = [TempVars]![PRON_ID]))
ORDER BY
a.ProcPointStartDate;
Here it the result:
| PRON | TimeStamp | Current Proc Point | ProcPointStartDate | EndDate | TimeInProc | Updated By | PALT |
| AX6F2449AX | PRON Received Buyer | Christopher Butler | |||||
| AX6F2449AX | Planning | P9CTT - Matt Tatum |
[td width="103pt"]
12/11/2025 13:47
[/td][td width="89pt"]
12/11/2025
[/td][td width="68pt"]
1/9/2026
[/td][td width="44pt"]
29
[/td][td width="48pt"]
-1
[/td][td width="103pt"]
12/11/2025 14:20
[/td][td width="89pt"]
12/11/2025
[/td][td width="68pt"]
1/9/2026
[/td][td width="44pt"]
29
[/td][td width="48pt"]
-1
[/td]Here is the desired result:
| PRON | TimeStamp | Current Proc Point | ProcPointStartDate | EndDate | TimeInProc | Updated By | PALT |
| AX6F2449AX | PRON Received Buyer | Christopher Butler | |||||
| AX6F2449AX | Planning | P9CTT - Matt Tatum |
[td width="103pt"]
12/11/2025 13:47
[/td][td width="89pt"]
12/11/2025
[/td][td width="68pt"]
12/11/2025
[/td][td width="44pt"]
0
[/td][td width="48pt"]
-1
[/td][td width="103pt"]
12/11/2025 14:20
[/td][td width="89pt"]
12/11/2025
[/td][td width="68pt"]
1/9/2026
[/td][td width="44pt"]
29
[/td][td width="48pt"]
-1
[/td]What I am trying to do is use the StartDate of the next record as the EndDate for the current record until we get to the last record, in that case the EndDate will be the current date/time. I could use a sub to do this, but I am sure it can be done with a query, but obviously I am not doing it right.
Any ideas?