We help IT Professionals succeed at work.

# how to calculate total hours from datetime field using sql

on
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
``````
Comment
Watch Question

## View Solutions Only

Most Valuable Expert 2014
Awarded 2013

Commented:
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)

Commented:
@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
Most Valuable Expert 2014
Awarded 2013
Commented:
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
;

``````
Most Valuable Expert 2014
Awarded 2013
Commented:
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

Commented:
thanks, which solution do i need to use?  there are money solutions here.  thanks again
Most Valuable Expert 2014
Awarded 2013

Commented:
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
Data Analyst

Commented:
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