We need a Crystal Report formula to display the number of seconds the oldest arriving call has been waiting. Across multiple resources that can each have an "oldest call". The database stores a datetime value for the time the oldest call arrived. If there are no waiting calls, then this field is NULL. (MSSQL database). It seemed reasonable to implement this in a formula that 1) discovers the minimum of "oldest call" timestamps in the selected records, and 2) to use "datediff" to produce the difference (in seconds) between the "oldest call" timestamp and current time.
The first attempt at this relied on "implied" iteration that could be done within a formula. Something like:
data: OLDESTARRIVALTIME
null
'2014-06-14 08:08:08.000'
null
'2014-06-14 08:07:55.000'
null
whilereadingrecords;
datetimevar minArrival;
if isNull({SVCCLASSMEASURES_VW.OLDESTARRIVALTIME}) = False
and minArrival < {SVCCLASSMEASURES_VW.OLDESTARRIVALTIME} then
minArrival := {SVCCLASSMEASURES_VW.OLDESTARRIVALTIME};
DateDiff("s", minArrival, {SVCCLASSMEASURES_VW.UTCDATE})
We tried storing the values of OLDESTARRIVALTIME in an array. We could see it iterating, but the values in the array only contained
the column value from the first record.
This was to solve the problem of screening null values and producing the minimum of the set.
Since that didn't work (and the web articles seemed to imply this would iterate over all the records, we tried another approach. This
time we set NULL timestamps in the table to a timestamp far in the future, so that we could directly apply "minimum" to produce
the correct "begin" for datediff.
data: OLDESTARRIVALTIME
'2030-01-01 00:00:00.000'
'2014-06-14 08:08:08.000'
'2030-01-01 00:00:00.000'
'2014-06-14 08:07:55.000'
'2030-01-01 00:00:00.000'
datetimevar minArrival = minimum({SVCCLASSMEASURES_VW.OLDESTARRIVALTIME});
datetimevar minUTC = minimum({SVCCLASSMEASURES_VW.UTCDATE});
if minArrival < minUTC then
DateDiff("s", minArrival, minUTC)
else
0
(minUTC would be current time in UTC)
So, to start things off, the last formula produces negative numbers! They hover in negative seconds within a negative minute (-33, -45, etc.).
That's inconceivable, considering the test for minArrival < minUTC. Both of the fields are "datetime".
As it turns out, in the near term, it's most important to get the second formula working. And, of course, insights into getting the first
formula to work are welcome as well!
Have we run into some weird behavior of the "DateDiff" function?
Thanks!