Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Summation of datetime fields

Posted on 2011-02-15
9
Medium Priority
?
411 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Fill in the form and get your FREE NFR key NOW!

Veeam® is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

 
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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
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 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.

661 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