can TIME values be aggregated?

Hello,

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
0
new
9/2/2010 8:53:42 PM
sqlserver.programming 1873 articles. 0 followers. Follow

5 Replies
910 Views

Similar Articles

[PageSpeed] 44

new DBA in '09 wrote:
> Hello,
>
> 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.
>
What is the datatype of the column you are storing these in? If these
are durations, then you should be storing them as integers (number of
seconds) so they can easily be aggregated. Storing them in the manner
you have chosen has forced you to take an extra step: if you are storing
them in a datetime (or char) column, then you need to convert them into
the number of seconds since midnight in order to aggregate them. Use
DateDiff to calculate the seconds since midnight before aggregating
them. Putting them into h:m:s format is, well, a formatting issue for
whatever application is displaying the result.

-- 
HTH,
Bob Barrows


0
Bob
9/2/2010 9:02:29 PM
On Sep 2, 4:53=A0pm, "new DBA in '09" <ericbra...@gmail.com> wrote:
> Hello,
>
> Can time values be aggregated? =A0I'm putting together a summary report
> that will need to show the sum time that something happened.
> (Employee breaks, for instance.) =A0If 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"? =A0Simply 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

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.
0
Tom
9/2/2010 9:13:05 PM
new DBA in '09 (ericbragas@gmail.com) writes:
> 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.
 
SELECT datediff(ss, '00:00', length_of_break)

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, esquel@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

0
Erland
9/2/2010 10:03:25 PM
At first read, your answers seemed logical and simple to enact, but
I'm having a hard time with the rendering now.

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.
0
new
9/2/2010 10:29:17 PM
I know I told you to do things like this in the front end.  But I'm not an 
SSRS person, so I can't help you with how to do it there.  If you can't find 
a way, it can be done in SQL, the following SQL will tell you that 
2938442134 seconds is 81,622 hours, 48 minutes and 54 seconds.

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" <ericbragas@gmail.com> wrote in message 
news:5e58f430-55b7-472e-bb56-59bd738f52f3@i31g2000yqm.googlegroups.com...
> At first read, your answers seemed logical and simple to enact, but
> I'm having a hard time with the rendering now.
>
> 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. 

0
Tom
9/3/2010 2:58:10 AM
Reply:

Similar Artilces:

Can't see 401(k) contribiltion total
I've set up a 401(k)accont. I transfer contribiltions to this 401(k)account from each paycheck. When I go to the 401 (k) account view, it shows all the informations such as transections and the account vallue ect., EXCEPT the total contribiltion. I want to know not only what my 401(k) vallue is, but how much money I have contribilt total or over a period of time as well. I find no place to see it. Anybody can help? I'm using Money2002 and 2004 Trial Verstion. Thanks a lot! ...

How can I print a selection in Outlook 2003?
How can I print just a selection of an email in Outlook 2003? The printer driver window is different from the one that comes up in Outlook Express. Thank you! You can do this for HTML formatted messages. Highlight your text, File-> Print... and select to print only the selection -- Robert Sparnaaij [MVP-Outlook] www.howto-outlook.com Tips of the month: -What do the Outlook Icons Mean? -Create an Office 2003 CD slipstreamed with Service Pack 1 ----- "Murphie" <Murphie@discussions.microsoft.com> wrote in message news:92F39B50-B877-408A-AAA7-DF350B1D72ED@microsoft.com....

Newbie: counting repeated values ???????
Hello all, I know little about Excel that's why i am posting this message here. I have a sheet with several columns, one of those columns have always one of this 2 values: YES or NO My question is, is there a way to count how many YES or NO are in that column and display the number in other cell? I already tried =+COUNTIF(D10:D240,"Yes") but i get a msg saying my form have an error. Thanks in advance, Helder Portugal Helder Take the + sign out of your formula =COUNTIF(C3:C8,"Yes") >-----Original Message----- >Hello all, > >I know little about Excel...

error: OBE-15502 Can only have 16000 per document
Hi, I tried to export data from oracle query builder to Excel sheet, got a error message: OBE-15502 Can only have 16000 per document Is there any way i can get more records(over 16000 or unlimited) in one sheet? thanks. -- JJ ...

Can different editions of Publisher be used together?
If my son uses Publisher 2003 on a school computer, downloads his work to a flash drive, brings it home to edit on Publisher 2007, will he then be able to take the edited work back to the school computer to be used, or will it not be compatible? Stateparent wrote: > If my son uses Publisher 2003 on a school computer, downloads his work to a > flash drive, brings it home to edit on Publisher 2007, will he then be able > to take the edited work back to the school computer to be used, or will it > not be compatible? Publisher 2003 and 2007 share a common file format, so the sc...

Can't send/receive
anyone have a situation where all of sudden they can't send new messages or receive anything? I get a message when I click on "accounts" that says 'fatal error in outlook unable to complete operation" ...

Should be an easy but I can not figure it out!!
I have a text box where the user enters a date and it is assigned to variable txtDate. How to I convert that to the serial number of the date? Thanks for the help IIRC, you can use DATEVAL(String) to get the serial date. -- HTH, Barb Reinhardt "VinceW" wrote: > I have a text box where the user enters a date and it is assigned to variable > txtDate. How to I convert that to the serial number of the date? > > Thanks for the help Barb, Thanks for the quick reply, however I get the error "Sub or Function not defined" when trying...

Hyperlink value crashes Access
If I assign a hyperlink value to a cmd button or in code using the follow hyperlink method, using a link value like http://www.cookbookplus.com/Activate.aspx, the page is accessed [Internet Explorer] OK, but the Access app crashes every time. However, if I instead just target the main web site http://www.cookbookplus.com that page is opened AND Access does not crash. Crashes also occur if a hyperlink field is populated with http://www.cookbookplus.com/Activate.aspx Again, that page is opened in IE, but Access crashes. The targeted page is a simple, safe, publicly available...

Can not Customize Leads View
Hi I have tried to customize the Leads view, I tried to add a view and add some columns to it. The view is shown however I see only the name column. When I go back to the View customization, I see the columns I added. Published your customizations??? -- ---------------------- regards, Ilya Milshtein MBS Master, MBSCP, MCP Softline International www.softline.ru "alsap" wrote: > Hi I have tried to customize the Leads view, I tried to add a view and add > some columns to it. > The view is shown however I see only the name column. > When I go back to the View customi...

Can I change the "Applied Task Update Requests and Errors" view?
I really like this view in PWA 2007, but I would like to replace one column with another. Is this view customizeable? Randy -- Bad news, my friend. This view is not customizable, nor is the view shown in the Task Updates customizable (which many users wish it was). Hope this help. -- Dale A. Howard [MVP] VP of Educational Services msProjectExperts http://www.msprojectexperts.com http://www.projectserverexperts.com "We write the books on Project Server" "Randy Schmid" <RandySchmid@discussions.microsoft.com> wrote in message news:72FA34B5-...

Bulk attachments
I have a very long list of Images in a single folder that I want to attach to seperate fields in a table/form. Each record contains two images, which are named like this: W:\Foldername\Micromap Run 001 A.bmp =(1st record) W:\Foldername\Micromap Run 001 B.bmp =(1st record) W:\Foldername\Micromap Run 002 A.bmp =(2nd record) W:\Foldername\Micromap Run 002 B.bmp =(2nd record) W:\Foldername\Micromap Run 003 A.bmp =(3rd record) W:\Foldername\Micromap Run 003 B.bmp =(3rd record) and so on... Image A goes to field A Image B goes to field B Is there a way of doing this au...

Excel 2007 Chart with Zero Values
I have chart data for sales that I add to each day. I hide the lines for the days to come(ie if it's the 5th, I hide lines for the 6th - 31st) so that the chart doesn't go all the way down to zero for the 6th thru the 31st. I'd like to be able to leave all lines unhid, but not have the chart line go down to zero. I've tried putting N/As in those days that I don't have data, but the chart is still going to down to zero - I don't know if I have a setting wrong or what, but any help would be much appreciated. Thanks. Hi, Try either #N/A or th...

can't uninstall exchange from cluster nodes
hi our client has several exchange clusters, and one of them is being retired. we have removed the exchange resources and the EVS and the server no longer appears in the organization and everything else there seems to be ok. we can't seem to uninstall exchange from either of the nodes. setup will start saying it's loading components then terminates with no error; happens on both nodes. both nodes are exchange 2003 sp1 on windows 2003. i did find the setup log; here is what it says, hope someone can shed some light on this (company info replaced with < >): [19:47:23] *...

Can't delete email #2
I'm am trying to help a friend who is using Outlook Express on W98, I cannot delete any messages from her in box. She has over 500 in there, which may be part of the problem. Any suggestions? Desperate! Mark Mark Ryan <m.ryan@earthlink.com> wrote: > I'm am trying to help a friend who is using Outlook Express > on W98, I cannot delete any messages from her in box. She > has over 500 in there, which may be part of the problem. > Any suggestions? The first suggestion is to ask in an Outlok Express newsgroup. The second suggestion is to delete Deleted Items.dbx and ...

Can Not Reply To Emails
Running Windows 7 Professional with latest version of WLM. I get email messages I can not reply to. The reply button is grayed out. For now I am forwarding them to my WinXP computer and then replying from that computer with OE6. Anybody have any ideas? -- <Bill> Brought to you from Anchorage, Alaska. The most obvious reason for not having an active Reply button is not having a mail account set up. --=20 Gary VanderMolen, Microsoft MVP (Mail) http://mvp.support.microsoft.com/default.aspx/profile/vandermolen "Bill Bradshaw" <bradshaw@gci.net>...

How can I rename a database table?
Hi I am working on a project that uses the MFC CDatabase and CRecordset classes to handle its interfacing with an Access database. It is necessary for the software to rename a table within existing user databases to ensure that they are valid for use with the new version of the software. As a result of my limited database and MFC knowledge I am having difficulty finding a way of renaming the table. I have tried using the following: try { theDataBase.ExecuteSQL( "RENAME oldTableName TO newTableName"); } catch( CDBException* e) { e->m_strError; } However, it results in th...

Fixed task time with flexible schedule
I am trying to create a yearly task schedule for building maintenance. My task durations are fixed at 4 to 8 hours but each task needs to be scheduled for sometime during the week of... "8 hours window cleaning anytime during the third week of July" for example. How do I represent this in MS Project 2007? Here is a reasonable work around. Project cannot schedule work "Any random 8 hour period during the week of January 25, 2010 to Januar 29, 2010." Here is what it can do: First key the task name and duration. On the main Gantt, select Window/Split... T...

spin button value
Hi Does the value in spinbutton properties have to be numeric or can it be a day eg mon tues wed etc if so how do I dio this Thanks tina A Forms toolbar spinner control returns an integer. You can have another cell with a formula that references the cell linked to the spinner like =CHOOSE(A1,"Sun","Mon","Tues","Weds","Thurs","Fri","Sat") Here A1 is linked to the spinner. You'd want to limit the spinner's minimum value to 1 and maximum value to 7 in this case. -- Jim Rech Excel MVP "tina" <tina@...

how can I restore unsaved changes in excel #2
...

Can (or how can) I do this
Hello, My Access application tracks an employee's progress through a process similar to hiring. There are 5 steps in the process. I track 4 of the steps by counting the number of times a value appears in a date field for that step. The count is then displayed and used in a calculation. The 5th step is more complicated. The 5th step needs to determine if the employee has verified that he can access all the applications that he should have access to. My process tracking table has the first 4 dates. The application access info comes from a different table. This process trackin...

Can Not Connect to Exchange Server after Mailbox Move
I have 2 exchange servers. While logged into the domain, and at the location, I can log into the email system just fine. While remote VPN in, I can not access the server. I can ping the server, I can see all shares, and I can even access the printer and print from the exchange server. But I can not get to it with email. Any Ideas? Oh.. both exchange 5.5, both in the same site. Exchange Admin shows both sites just fine. is there a firewall with rules configured, in between the users vpn connection and the internal network? kmelillo@gmail.com wrote: > Oh.. both exchange 5.5, bot...

How do I calculate duty times
How do I calculate duty times and need to separate if any one is late and their overtime Hi, Take a look at C. Pearson web it has the answers you are looking for http://www.cpearson.com/excel/overtime.htm If this was helpful please say yes, thanks "Sala" wrote: > How do I calculate duty times and need to separate if any one is late and > their overtime ...

Can not archive
I get the message "Error while archiving folder "Deleted Items" in store "mailbox-user name." Some items could not be copied. They were either moved or deleted, or access was denied. This only happend recently and only occurs with the deleted items. I can archive send and the inbox without a problem. Any suggestions? Thanks ...

can'not set or connect to an email account
This is a multi-part message in MIME format. ------=_NextPart_000_0010_01CAAC2C.BBDF6A00 Content-Type: multipart/alternative; boundary="----=_NextPart_001_0011_01CAAC2C.BBDF9110" ------=_NextPart_001_0011_01CAAC2C.BBDF9110 Content-Type: text/plain; charset="Windows-1252" Content-Transfer-Encoding: quoted-printable I am triying to set an email account.I am vodafone mobile customer.Some = settings bloking my access to email service.I am not a professinal = computer user,following the the instractions but no luck.Can you help = me? Thank you. ------=_Nex...

Where can I download this type of db example?
Hi, I'm working in Access 2003. Where can I download a (free) Access database example that shows how to implement forms and queries for tables that break down many-to-many relationships with a link table? I understand normalization and I believe my structure is 3rd order normalized. I need to figure out how to input data into the tables and have them linked together automatically by a query (or, if necessary, VB code). In other words, a database that has tblParents (1->oo) tblParent_Child_LINK (1->oo) tblChildren where (1->oo) means One-to-many, if it wasn't c...