473,765 Members | 1,952 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Overlaping time intervals calculations

2 New Member
Hi I have a MSSQL table with many time intervals stored as datetime. Each time interval is also assigned a numeric type that specifies what type of job was done during the time interval.

I need to add-up the time intervals by type, make sure overlaping portions of intervals are not accounted for twice and also respect interval type priorities. Here's a theoric example:

Priority is given to types 30, 50 and 70. Meaning that when there's an overlap between one of these types and another one (40,60) the overlaping portion is given to the type with priority and removed from the type without priority. If there is an overlap between to types with priority, the time is kept for both.

Example 1:

Type, Start, End

30, 2006-11-01 09:00:00, 2006-11-01 11:00:00
30, 2006-11-01 13:00:00, 2006-11-01 17:00:00
40, 2006-11-01 16:30:00, 2006-11-01 17:30:00
50, 2006-11-01 17:30:00, 2006-11-01 18:30:00

Result:
Type, interval (h)

30,6.0
40,0.5
50,1.0

Example 2:

Type, Start, End

30, 2006-11-01 09:00:00, 2006-11-01 11:00:00
30, 2006-11-01 13:00:00, 2006-11-01 17:00:00
40, 2006-11-01 16:30:00, 2006-11-01 17:30:00
50, 2006-11-01 16:30:00, 2006-11-01 18:30:00

Result:
Type, interval (h)

30,6.0
40,0.0
50,2.0


My plan is to go with a stored procedure an cursors but if I could avoid it I would appreciate it. This query will be attaches to a Crystal Report report to produce a graph. of the hours spent by work type. Start and End datetime parameters would be keyed uppon printing the report and the data would be dynamically calculated.

Thanks a lot

_______________ _______________ ______
Rombolt
Dec 11 '06 #1
2 4344
almaz
168 Recognized Expert New Member
OK, here you go. For most of the simple cases when overlapping is a pretty rare case it will work, but it produces incorrect results for certain cases described in the code, and it cannot be fixed with simple queries (actually I'm not sure if it can be fixed in T-SQL without thousands-of-lines SP). I tried to describe the bug in comments, hope you'll understand.
Expand|Select|Wrap|Line Numbers
  1. declare @tasks table (id int, ispriority bit)
  2. insert @tasks values (30, 1)
  3. insert @tasks values (40, 0)
  4. insert @tasks values (50, 1)
  5. insert @tasks values (60, 0)
  6. insert @tasks values (70, 1)
  7.  
  8. declare @schedule table(taskid int, startdate datetime, enddate datetime)
  9. --First example:
  10. --insert @schedule values (30, '2006-11-01 09:00:00', '2006-11-01 11:00:00')
  11. --insert @schedule values (30, '2006-11-01 13:00:00', '2006-11-01 17:00:00')
  12. --insert @schedule values (40, '2006-11-01 16:30:00', '2006-11-01 17:30:00')
  13. --insert @schedule values (50, '2006-11-01 17:30:00', '2006-11-01 18:30:00')
  14.  
  15. --Second example:
  16. --insert @schedule values (30, '2006-11-01 09:00:00', '2006-11-01 11:00:00')
  17. --insert @schedule values (30, '2006-11-01 13:00:00', '2006-11-01 17:00:00')
  18. --insert @schedule values (40, '2006-11-01 16:30:00', '2006-11-01 17:30:00')
  19. --insert @schedule values (50, '2006-11-01 16:30:00', '2006-11-01 18:30:00')
  20.  
  21.  
  22. -- contains schedule records with additional overlapping higher-priority records
  23. -- note: it will incorrectly parse situations when several non-overlapping between each other higher priority
  24. -- schedule records will overlap a single schedule row. Example:
  25.  
  26. -- high-priority rows will be treated as:
  27. --                         [-----------------------------]
  28. -- actual higher priority: [---]    [---]  [-------------]
  29. -- standard priority:   [------------------------------]
  30. declare @parsedschedule table (
  31.     taskid int, startdate datetime, enddate datetime, overlapped_startdate datetime, overlapped_enddate datetime)
  32.  
  33. insert @parsedschedule
  34. select main.taskid, main.startdate, main.enddate, min(overlapping_schedule.startdate), max(overlapping_schedule.enddate)
  35. from @schedule main 
  36.     inner join @tasks tasks on main.taskid = tasks.id
  37.     left join 
  38.     (
  39.         select startdate, enddate 
  40.         from @schedule 
  41.         where taskid in (select id from @tasks where ispriority = 1)
  42.     ) overlapping_schedule on
  43.         -- this check ensures that current schedule row doesn't belong to high-priority tasks
  44.         tasks.ispriority = 0 and
  45.         -- this check filters out only those schedules overlapping current main schedule row
  46.         main.startdate < overlapping_schedule.enddate and main.enddate > overlapping_schedule.startdate
  47. group by main.taskid, main.startdate, main.enddate
  48.  
  49. -- select * from @parsedschedule
  50.  
  51. select taskid, 24 * sum(
  52.     convert(float, 
  53.         enddate - startdate - isnull(
  54.             case when enddate<overlapped_enddate then enddate else overlapped_enddate end -
  55.             case when startdate>overlapped_startdate then startdate else overlapped_startdate end, 0)))
  56. from @parsedschedule
  57. group by taskid
Dec 11 '06 #2
Rombolt
2 New Member
Thank you Almaz, I will give it a try and let you know.

I'm not certain I understand which case can create a bug but I guess I will see it when trying with more examples.

Thanks again.

Rombolt
Dec 12 '06 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
6093
by: Marcus | last post by:
I am having some problems with trying to perform calculations on time fields. Say I have a start time and an end time, 1:00:00 and 2:30:00 (on a 24 hour scale, not 12). I want to find the difference in minutes, divide this result by a predefined size of interval, and make a loop that runs this many times. For example, with an interval size of 15 minutes, it will return 6 blocks... I have this all working fine, but am getting stuck on...
2
5219
by: androtech | last post by:
Hello, I'm looking for a function that returns a date range for a specified week number of the year. I'm not able to find functions like this anywhere. Any pointers/help would be much appreciated. TIA
4
1363
by: Hans | last post by:
Hi there, I need to create a list (diary style) of times in a day. I can do this with normal "metricated" break-up of the hours, but need to display the actual time. I have a "Starting time" and an ending time as variables. I also have a "Timespan" for the increment. For example Start time=8:00 AM End Time 6:00 PM and increment in 30 minute intervals I need to convert anything after the decimal point into Minutes. If I have
2
47856
by: Shaun | last post by:
Hi, I have a table called Bookings which has two important columns; Booking_Start_Time and Booking_End_Time. These columns are both of type DATETIME. Given any day how can I calculate how many hours are available between the hours of 09.00 and 17.30 so a user can see at a glance how many hours they have unbooked on a particular day (i.e. 8.5 hours less the time of any bookings on that day), can this be done with a query or do I have to...
11
4675
by: lduperval | last post by:
Hi, I`m trying to do date calculations in three types of time zones: local, GMT and specified. The issue I am facing is that I need to be able to specify a date in the proper time zone, and I`m having a heck of a time doing so. I have created a form where I use drop downs do specify year, month, date, hour, minute and seconds. When the form is loaded, the dropdowns have to display the proper values for the current time zone type. This
6
18670
by: Penguin | last post by:
At some long ago time Steve Jorgensen answered thus: Subject: Re: How can I round a time? Newsgroups: comp.databases.ms-access Date: 1998/12/11 Access represents a date internally as a double and will convert between date/time and double automatically. The double value Access (or VB) creates is based on 1 day = 1.0 and the fractional part represents a
0
1141
by: Hiten | last post by:
I want our application must run on many browser mainly Internet Explorer . Problem comes when we are using TreeViews & menubars on our webpage in IE it works fine but in other browsers it overlaps other controls , which dosent give good interactive user interface even some time user cant access treeview/menu control duto overlaping.. Any one can suggest solution for this i will be thankfull to him/her....
2
4771
by: phanicrn | last post by:
Hi Everyone I Have column in sql server databas as "HHMMSS" and data as and i am doing a substring to get values for hours and minutes. since my calculations based on hour interval and 30 minutes interval for ex: Now i want to show all the transaction done b/w 6 to 7 am or pn. and for 30 minutes interval i have get the calculation as transactions done b/w 6:00 to 6:30 and 6:30 to 7:00 either it's am or pm. now how i can write my...
4
22116
by: brazil.mg.marcus.vinicius.lima | last post by:
Hello, I need to create a column that will store hours bigger than 24. For example '25:00:00', '129:23:12', etc). That column will be used too, for perform calculations between datetime intervals: 'time'. In MySQL there is a datatype that perfect fits that necessity. Does anyone know what is the corresponding datatype in SQL Server?
0
9568
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
9951
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9832
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8831
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7378
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5421
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3924
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3531
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2805
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.