Solved

Summation of datetime fields

Posted on 2011-02-15
9
408 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 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
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 
LVL 74

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
 

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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
In this article I will describe the Detach & Attach 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.
Via a live example show how to connect to RMAN, make basic configuration settings changes and then take a backup of a demo database
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

830 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