Can time values be aggregated? I'm putting together a summary report
that will need to show the sum time that something happened.
(Employee breaks, for instance.) If break times for 3 employees were
"01:00:00", "00:15:00", and "00:12:30" in length, how do I aggregate
them to get "01:27:30"? Simply treating them like char values so they
can be parsed into integers and added and then divided by 60 doesn't
seem like the best way.
Thanks,
Eric
--
HTH,
Bob Barrows
You cannot aggregate on a TIME datatype. TIME defines a time of day
not a length of time. You can aggregate on the DIFF between start and
end times.
gives you the length of the breaks in seconds.
Then, let's see, this might work:
SELECT convert(char(8),
dateadd(ss, SUM(datediff(ss, '00:00', length_of_break)),
convert(time, '00:00'),
108)
But I did not test.
--
Erland Sommarskog, SQL Server MVP, esq...@sommarskog.se
Links for SQL Server Books Online:
SQL 2008: http://msdn.microsoft.com/en-us/sqlserver/cc514207.aspx
SQL 2005: http://msdn.microsoft.com/en-us/sqlserver/bb895970.aspx
SQL 2000: http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx
Since my employees are located world-wide, a starting break time could
conceivably be 11:50:00 p.m. from to 12:50:00 a.m. That's 3600
seconds. So far so good, since I'm using "DATEDIFF(second, StartDate,
EndDate)" to figure out the number of seconds in the timespan. When
they all sum to a value greater than 86,400, however, the value that
gets returned is always less than 24 hours, leading me to believe a
TIME value cannot exceed :23:59:59.999. OK, so I understand why you
suggest keeping integer values in the database and formatting them in
the app.
Any idea how I can format 293842134 seconds as a time value in SSRS
2008? One that has the potential to exceed 24 hours, such as
"26:19:34" (signifying 26 hours, 19 minutes, 34 seconds)?
Thanks, Bob and Tom. Five stars for both posts.
Declare @Test Table(StartTime datetime, EndTime datetime);
Insert @Test(StartTime, EndTime)
Values (Current_Timestamp, DateAdd(second, 293842134, Current_Timestamp));
With cte As
(Select StartTime, EndTime, DATEDIFF(second, StartTime, EndTime) As Seconds
From @Test)
Select StartTime, EndTime, Seconds As DiffInSeconds,
CAST(Seconds / 3600 As varchar(11)) + ':'
+ CAST((Seconds / 60) % 60 As varchar(2)) + ':'
+ CAST((Seconds % 60) % 60 As varchar(2)) As 'DiffInH:M:S'
From cte;
Tom
"new DBA in '09" <ericb...@gmail.com> wrote in message
news:5e58f430-55b7-472e...@i31g2000yqm.googlegroups.com...