Solved

Summation of datetime fields

Posted on 2011-02-15
9
405 Views
Last Modified: 2012-05-11
In SQL, how would I sum these values together to get total hours? The total should be about 18.09 hours.

Sample data:
SEG_START                            SEG_END
2011.01.05 00:40:00      2011.01.05 04:50:00
2011.01.05 04:50:00      2011.01.05 07:00:00
2011.01.05 07:00:00      2011.01.05 08:00:00
2011.01.05 08:00:00      2011.01.05 09:00:00
2011.01.05 09:00:00      2011.01.05 10:00:00
2011.01.05 10:00:00      2011.01.05 11:00:00
2011.01.05 11:00:00      2011.01.05 12:00:00
2011.01.05 12:00:00      2011.01.05 13:00:00
2011.01.05 13:00:00      2011.01.05 14:00:00
2011.01.05 14:00:00      2011.01.06 18:45:00
0
Comment
Question by:wdelaney05
  • 3
  • 3
  • 3
9 Comments
 
LVL 28

Accepted Solution

by:
Naveen Kumar earned 250 total points
ID: 34903601
if there are no gaps in the times for all the records, then try this :

select (max(seg_end) - min(seg_start) ) * 24 from your_table
0
 
LVL 28

Expert Comment

by:Naveen Kumar
ID: 34903610
the output given by the query will be in hours.
0
 
LVL 73

Expert Comment

by:sdstuber
ID: 34903651
assuming your values are strings then try this...

select sum(24 * (to_date(seg_end,'yyyy.mm.dd hh24:mi:ss')  - to_date(seg_start,'yyyy.mm.dd hh24:mi:ss')))  from yourtable


note, your total is 42.0833333333 hours because your last row is 28.75 hours

if you change the date to 2011.01.05 then it will come out to 18.0833333333
0
 
LVL 73

Assisted Solution

by:sdstuber
sdstuber earned 250 total points
ID: 34903654
if your values are dates, you can skip the "to_date" portion and simply sum the difference * 24
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

Author Comment

by:wdelaney05
ID: 34903671
both of you are correct, thanks! I just took the time to test out all comments.
I tried select (max(seg_end) - min(seg_start) ) * 24
and I also tried SUM(((seg_end - seg_start) * 24))

0
 

Author Comment

by:wdelaney05
ID: 34903673
and yup, sdstuber, my values are dates.
0
 
LVL 28

Expert Comment

by:Naveen Kumar
ID: 34903695
Good and thanks.
0
 
LVL 73

Expert Comment

by:sdstuber
ID: 34903730
note the max-min  method is only valid if the start/end are contiguous

if there are any holes then you'll need to sum the individual pieces
0
 

Author Comment

by:wdelaney05
ID: 34903890
sdtuber, re: note the max-min  method is only valid if the start/end are contiguous
if there are any holes then you'll need to sum the individual pieces

Noted, thanks. My data will be contiguous, thank goodness. I have other tables with data that won't be contigous, so I'll have to keep that in mind.

Thanks again to both of you!
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Join & Write a Comment

Suggested Solutions

Cursors in Oracle: A cursor is used to process individual rows returned by database system for a query. In oracle every SQL statement executed by the oracle server has a private area. This area contains information about the SQL statement and the…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Via a live example, show how to restore a database from backup after a simulated disk failure using RMAN.
This video shows how to copy an entire tablespace from one database to another database using Transportable Tablespace functionality.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now