Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Summation of datetime fields

Posted on 2011-02-15
9
Medium Priority
?
412 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 1000 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 74

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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 74

Assisted Solution

by:sdstuber
sdstuber earned 1000 total points
ID: 34903654
if your values are dates, you can skip the "to_date" portion and simply sum the difference * 24
0
 

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 74

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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

How to Unravel a Tricky Query Introduction If you browse through the Oracle zones or any of the other database-related zones you'll come across some complicated solutions and sometimes you'll just have to wonder how anyone came up with them.  …
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This video shows how to Export data from an Oracle database using the Original Export Utility.  The corresponding Import utility, which works the same way is referenced, but not demonstrated.
This video shows how to recover a database from a user managed backup

783 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