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.
Author |
Topic |
rookie_sql
Constraint Violating Yak Guru
443 Posts |
Posted - 2006-11-30 : 12:00:05
|
I'd like to figure out how much time was between the time given and the time between every half an hour time slots.For example if i have a time of hh:mm:ss "11:30:04" this would fall into the time slot of 11:30 - 12:00 and the time used within that time slot was 4 seconds.Another example would be a give time of 08:52:04 this would fall into the time slot of 08:30 - 09:00 and the time used within that time slot was 22 mins 4 seconds. Now things get a bit tricky what if my 22 mins 4 seconds span over 2 time slots Example start time was 08:20 End time was 08:42.So the first 10 mins was in the time slot of 08:00 to 08:30 and the next 12 mins was in the time slot of 08:30 to 09:00Or if the given time spaned over more than 2 time slots Example Start time was 08:03:00 End time was 09:35:00Now we have 27 mins from the time slot 08:00 - 08:3030 mins from the time slot 08:30 - 09:0030 mins from the time slot 09:00 - 09:305 mins from the time slot 09:30 - 10:00I have a table that hold time slots they go up in 30 mins starting form 06:00 am to 18:00pmExample06:0006:30...18:00 |
|
mwjdavidson
Aged Yak Warrior
735 Posts |
Posted - 2006-11-30 : 12:54:00
|
Just out of interest, how are you storing these times given that SQL Server has no 'time' data type?Mark |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-11-30 : 16:46:14
|
[code]-- prepare test datadeclare @slots table (slot int, fromtime datetime, totime datetime)insert @slotsselect number, dateadd(second, 21600 + 1800 * number, 0), dateadd(second, 23400 + 1800 * number, 0)from master..spt_valueswhere name is null and number between 0 and 23declare @times table (timeid int, fromtime datetime, totime datetime)insert @timesselect 1, '08:03:04', '09:35:00' union allselect 2, '08:20', '08:42'-- do the workselect t.timeid, s.fromtime, s.totime, sum( case when t.fromtime >= s.fromtime and t.fromtime < s.totime then datediff(second, t.fromtime, s.totime) else 0 end + case when t.fromtime < s.fromtime and t.totime >= s.totime then datediff(second, s.fromtime, s.totime) else 0 end + case when t.totime >= s.fromtime and t.totime < s.totime then datediff(second, s.fromtime, t.totime) else 0 end ) secondsfrom @slots sinner join @times t on t.totime >= s.fromtime and t.totime < s.totime or t.fromtime < s.fromtime and t.totime >= s.totime or t.fromtime >= s.fromtime and t.fromtime < s.totimegroup by t.timeid, s.fromtime, s.totimeorder by t.timeid, s.fromtime[/code]Peter LarssonHelsingborg, Sweden |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
|
rookie_sql
Constraint Violating Yak Guru
443 Posts |
Posted - 2006-12-05 : 10:58:20
|
Thanks for your help on this peter, i plugged in my table and it works fine..To answer "mwjdavidson" question i don't want just the time part, if i did i could copy the date time from the datatime column to a varchar column and use the substring to dirplsy just the time part.. |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-12-05 : 11:01:42
|
It is good to hear from you again and that the query worked out well for you.Good luck!Peter LarssonHelsingborg, Sweden |
 |
|
rookie_sql
Constraint Violating Yak Guru
443 Posts |
Posted - 2006-12-14 : 06:53:45
|
Hi Peter,ok here is my problem if you run this code see below you get the result which is incorrect as the final result should be 60 and not 1860 for some reason i get 30 mins = 1800 seconds added on to the result. Expected result = 60 Actual result = 1860-- the time spent between the time slot 8:30 and 9:00 was 1 min = 60 secondsResultsFromTime ToTime Seconds1900-01-01 08:30:00.000 1900-01-01 09:00:00.000 1860drop table table1create table table1( FromTime datetime, ToTime datetime)insert into table1 (FromTime,Totime)values('01/01/1900 08:51:00','01/01/1900 08:52:00')-- the time spent between the time slot 8:30 and 9:00 was 1 min = 60 seconds-- prepare test datadeclare @slots table (slot int, fromtime datetime, totime datetime)insert @slotsselect number,dateadd(second, 21600 + 1800 * number, 0),dateadd(second, 23400 + 1800 * number, 0)from master..spt_valueswhere name is nulland number between 0 and 23declare @times table (fromtime datetime, totime datetime)insert @times(FromTime,Totime)select FromTime, Totime From table1 -- do the workselect s.fromtime, s.totime, sum( case when t.fromtime >= s.fromtime and t.fromtime < s.totime then datediff(second, t.fromtime, s.totime) else 0 end + case when t.fromtime < s.fromtime and t.totime >= s.totime then datediff(second, s.fromtime, s.totime) else 0 end + case when t.totime >= s.fromtime and t.totime < s.totime then datediff(second, s.fromtime, t.totime) else 0 end ) secondsfrom @slots sinner join @times t on t.totime >= s.fromtime and t.totime < s.totimeor t.fromtime < s.fromtime and t.totime >= s.totimeor t.fromtime >= s.fromtime and t.fromtime < s.totimegroup by s.fromtime,s.totimeorder by s.fromtime |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-12-18 : 07:18:56
|
Try thisSELECT s.fromtime, s.totime, SUM(CASE WHEN t.FromTime < s.FromTime AND t.ToTime >= s.FromTime AND t.ToTime < s.ToTime THEN DATEDIFF(second, s.FromTime, t.ToTime) WHEN t.FromTime < s.FromTime AND t.ToTime > s.ToTime THEN DATEDIFF(second, s.FromTime, s.ToTime) WHEN t.FromTime >= s.FromTime AND t.ToTime <= s.ToTime THEN DATEDIFF(second, t.FromTime, t.ToTime) WHEN t.FromTime >= s.FromTime AND t.FromTime < s.ToTime AND t.ToTime >= s.ToTime THEN DATEDIFF(ss, t.FromTime, s.ToTime) END) SecondsFROM @Times tCROSS JOIN @Slots sWHERE (t.FromTime < s.FromTime AND t.ToTime >= s.FromTime AND t.ToTime < s.ToTime) OR (t.FromTime < s.FromTime AND t.ToTime > s.ToTime) OR (t.FromTime >= s.FromTime AND t.ToTime <= s.ToTime) OR (t.FromTime >= s.FromTime AND t.FromTime < s.ToTime AND t.ToTime >= s.ToTime)GROUP BY s.fromtime, s.totimeORDER BY s.FromTime Peter LarssonHelsingborg, Sweden |
 |
|
|
|
|
|
|