algorithm - Transact-SQL: Detect Date boundaries across multiple rows -


I am creating a stored procedure that prepares reports based on the data in the SQL database and these reports have a different database The data being stored in the data was running a motor range longer than a date range. I want to know that I am reporting on the basis of time, based on the date of time I overlap past reports. For example, we have the following table:

  line --- - value ----- start ------------------ End date 1 ------- 12 -------- 2010-01-21 00: 00: 00,000 ---- 2010-01-21 11: 59: 99.997 2 ------ - 12 -------- 2010-01-22 00: 00: 00,000 ---- 2010-01-22 11: 59: 99.997 3 ------- 12 ------- - 2010-01-22 12: 00,000 ---- 2010-01-22 23:59: 99.997 4 ------- 12 -------- 2010-01-23 00: 00 : 00,000 ---- 2010-01-23 11: 59: 99.997 5 ------- 12 -------- 2010-01-24 00: 00: 00,000 ---- 2010-01 -24 11: 59: 99.997  

If I was included in this table, the following lines are:

INPUT:

  1. Value = 12, Start Date = 2010-01-21 6: 00: 00.000, End VAT = 2010-01- 21 17: 59: 99.97 2. Value = 14, start date = 2010-01-21 11: 00: 00,000, end date = 2010-01-22 0: 59: 99.997 3. Value = 13, Start date = 2010-01-20 12: 00: 00,000, end date = 2010-01-21 0: 59: 99.997  

Output:

  1. Value = 6, start Ate = 2010-01-21 12: 00: 00,000, end date = 2010-01-21 17: 59: 99.97 2. value = 12, start date = 2010-01-21 12: 00: 00,000, End date = 2010-01- 21 23: 59: 99.997 3. Value = 12, start date = 2010-01-20 12: 00: 00,000, end date = 2010-01-20 23: 59: 99.9 97  

As you can see, start / end dates are adjusted and no extra time Cal is. It is important to note that if only 1 row is added, then not all 3 at the same time.

I think I have seen this problem very long and for this the forest does not see the tree, the tree. However, in the process of writing this question I have come up with some more ideas and have tried to amend my tables, but still you would like to see how you will fight this situation.

Question: How many queries will you use to find out that in many rows is there a start and end date ranging between different rows?

* Edit

To illustrate my example data, value = 1 for output 1 comes from 6 fact that this line 1 to 6 Fights with the hour. Below are changes for each input.

  1. Input conflicts with line 1, start time of input for line + 3 ms end date and query (timepan = 6 hours).
  2. Input conflicts with line 1; 2, Move the start date of line 1 + 3 ms and the opening date of line 2 - 3 ms for the end time. (Typpan = 12 hours)
  3. Move the input conflict with line 1, end time to the expiration date of line 1 - 3 ms. (Timepace = 12 hours).

Although this example only shows the duration within 1 day, it can be an example where there is a time period of several days.

itemprop = "text">

To find the overlap, assume that the overlapping rows must be preceded by both the start- should be there.

So you should be able to do something like this:

 Choose from  Include in your Tables on your Tables in your Tables. Start date & lt; T1.EndDate and t1.StartDate & lt; T2.EndDate  

You want to make sure that you do not have a line where StartDate> EndDate, using check barrier. Otherwise, this includes:

  and t1.StartDate & lt; T1.EndDate and t2.StartDate & lt; T2.EndDate  

Comments

Popular posts from this blog

sql - dynamically varied number of conditions in the 'where' statement using LINQ -

asp.net mvc - Dynamically Generated Ajax.BeginForm -

Debug on symbian -