We help IT Professionals succeed at work.
Get Started

how to calculate total hours from datetime field using sql

191 Views
Last Modified: 2015-03-02
I am trying to add the total hours where flag =1.  I really don't how to solve this problem.  Users indicate when they were sleeping during the night so they provide feedback by going to a website and check some check-boxes and these check-boxes indicate the hours they were sleeping during the night. So in the example below,  we see the following
10:00 to 12:00 = sleep
12:30 to 1:30 = no sleep
2:00 to 3:30 = sleep
4:00 to 4:30 = no sleep
so the total hours of sleep for this user is 3.5 hours.  This is challenge for me please help.  

ID                 FullDateTime                       Flag
88          2015-02-26 04:30:00.000               0   
22          2015-02-26 04:00:00.000               0      
45           2015-02-26 03:30:00.000              1
25           2015-02-26 03:00:00.000              1
11           2015-02-26 02:30:00.000              1
57           2015-02-26 02:00:00.000              1
15           2015-02-26 01:30:00.000              0
36           2015-02-26 01:00:00.000              0
00          2015-02-26 00:30:00.000               0
47          2015-02-26 00:00:00.000               1
56          2015-02-25 23:30:00.000               1
66          2015-02-25 23:00:00.000               1
27           2015-02-25 22:30:00.000              1
55            2015-02-25 22:00:00.000             1

Open in new window

Comment
Watch Question
EE Topic Advisor
CERTIFIED EXPERT
Most Valuable Expert 2014
Awarded 2013
Commented:
This problem has been solved!
Unlock 2 Answers and 9 Comments.
See Answers
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE