Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Datetime format breakdown by hours

Author  Topic 

slimt_slimt
Aged Yak Warrior

746 Posts

Posted - 2010-01-31 : 15:01:39
Hi,

this is my sample of simple sql code.


create table work_time
(
date datetime
,working_time_second int
,person varchar(10)
)

insert into work_time
select '2004-06-24 06:19:40.000',3525,'John1' union all
select '2004-06-24 08:03:30.000',13525,'John1' union all
select '2004-06-24 08:03:30.000',1234,'Jim1' union all
select '2004-06-24 10:03:30.000',19284,'Jim1' union all
select '2004-06-24 08:10:30.000',17425,'Lester'



What i want to achieve is, to get the results as following:

hour | work_time_minutes_within_hour | person
------------------------------------------------
6 | 40 | John1
7 | 18 | John1
8 | 57 | John1
9 | 60 | John1
10 | 60 | John1
11 | 48 | John1
8 | 21 | Jim1
10 | 57 | Jim1
11 | 60 | Jim1
12 | 60 | Jim1
13 | 60 | Jim1
.
.
.

where "work_time_minutes_within_hour" is a working time that a person worked from the date until date + working_time_second. If time overlaps the full hour, the calculation goes into next hour as shown in example.

thank you for your ideas.

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-02-01 : 05:18:07
[code]SELECT v.number AS hour,
CASE WHEN v.number = DATEPART(hh,t.StartDate)
THEN 60-DATEPART(mi,t.StartDate)
WHEN v.number = DATEPART(hh,t.EndDate)
THEN DATEPART(mi,t.EndDate)
ELSE 60
END AS work_time_minutes_within_hour,
t.person
FROM (SELECT date AS StartDate,DATEADD(ss,working_time_second,date) AS EndDate,Person FROM Table )t
CROSS JOIN master..spt_values v
WHERE v.type='p'
AND v.number BETWEEN DATEPART(hh,t.StartDate) AND DATEPART(hh,t.EndDate)
[/code]
Go to Top of Page
   

- Advertisement -