how to calculate total hours from datetime field using sql

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

moe57Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

PortletPaulfreelancerCommented:
each row represents 30 minutes

(count the rows with flag=1) * 30 minutes = total minutes

select
       cast(FullDateTime as date) as TheDate
       ,  count(*) * 30   as SleepMinutes
from YourTable
where flag = 1
group by
       cast(FullDateTime as date)


you probably have another field for personID or similar, & that can be included into the query too so you get a result per person per day

--alternative using SUM()
select
       cast(FullDateTime as date) as TheDate
       ,  SUM(1) * 30   as SleepMinutes
from YourTable
where flag = 1
group by
       cast(FullDateTime as date)
0
IrogSintaCommented:
@PortletPaul, I originally thought the same but if you take a closer look, the last half hour of each contiguous timeframes should not be counted.  With the solution you provided, the end result is 4.5 hours.  In this case it's really supposed to be 3.5 hours.

Ron
0
PortletPaulfreelancerCommented:
right.

use a "self join" matching each recoird to it's earlier record. Then when both flags = 1 you have a 30 minute period.

select ( count(*) * 30.0 ) / 60.0
from table1 as e
left join table1 as s on time(e.FullDateTime, '-30 MINUTE') = time(s.FullDateTime)
where s.flag = 1 and e.flag = 1
;
on that sample data as table1 this whould equal 3.5

see stdout here: http://ideone.com/bkabOv

 
 
CREATE TABLE Table1
	(`ID` int, `FullDateTime` date, `Flag` int)
;
 
INSERT INTO Table1
	(`ID`, `FullDateTime`, `Flag`)
VALUES
	(88, '2015-02-26 04:30:00', 0),
	(22, '2015-02-26 04:00:00', 0),
	(45, '2015-02-26 03:30:00', 1),
	(25, '2015-02-26 03:00:00', 1),
	(11, '2015-02-26 02:30:00', 1),
	(57, '2015-02-26 02:00:00', 1),
	(15, '2015-02-26 01:30:00', 0),
	(36, '2015-02-26 01:00:00', 0),
	(00, '2015-02-26 00:30:00', 0),
	(47, '2015-02-26 00:00:00', 1),
	(56, '2015-02-25 23:30:00', 1),
	(66, '2015-02-25 23:00:00', 1),
	(27, '2015-02-25 22:30:00', 1),
	(55, '2015-02-25 22:00:00', 1)
;
 
select ( count(*) * 30.0 ) / 60.0
from table1 as e
left join table1 as s on time(e.FullDateTime, '-30 MINUTE') = time(s.FullDateTime)
where s.flag = 1 and e.flag = 1
;
 
 

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

PortletPaulfreelancerCommented:
sorry for mssql use DATEADD()

select ( count(*) * 30.0 ) / 60.0
from table1 as e
left join table1 as s on dateadd(minute,-30,e.FullDateTime) = s.FullDateTime
where s.flag = 1 and e.flag = 1
0
moe57Author Commented:
thanks, which solution do i need to use?  there are money solutions here.  thanks again
0
PortletPaulfreelancerCommented:
the approach is the same, the first is suited to SQLite and the second is suited to MS SQL Server;
there is only one approach the option is just syntax
0
Pratik MakwanaData AnalystCommented:
Try this with above query.....

select ( count(*) * 30.0 ) / 60.0
from Posting as e
left join Posting as s on CAST(e.FullDateTime-'00:30:00'  as Time)= CAST(s.FullDateTime-'00:30:00'  as Time)
where s.flag = 1 and e.flag = 1
0
PortletPaulfreelancerCommented:
there needs to be a 30 minute  difference when performing the join

deducting 30 minutes from both e.FullDateTime & s.FullDateTime does not produce a difference (they will be equal)
0
IrogSintaCommented:
@PortletPaul, that was a pretty good idea for subtracting the 30 minutes in the join.
:-)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.