sql - How to consolidate blocks of time? -
I have a derived table with a list of seconds relative to the foreign key (id):
Create table time (id INT, time INT, time INT); The table contains mostly non-overlapping data, but there are occasions where I have a timetable & lt; Time from any other record:
+ ---- + ---------- + -------- + | ID | From time to time Time to + ---- + ---------- + -------- + | 10 | 10 | 30 | | 10 | 50 | 70 | | 10 | 60 | 150 | | 10 | 75. 150 | | .. | ... | ... | + ---- + ---------- + -------- + The result set means a flat linear inactive report, but in these With so many overlaps from, I end up with negative time in use. To wit. If the window above was 150 seconds long for the id = 10 , and I had expressed the difference of relative seconds to decrease the size of the window, give me the code <150> (20 + 20 + 90 + 75) = - 55 . I have tried this approach, and I came to know that there were overlaps that were flattened. So, what I'm looking for is the solution to level one overlap at one time: + ---- + ------- --- + -------- + | ID | From time to time Time to + ---- + ---------- + -------- + | 10 | 10 | 30 | | 10 | 50 | 150 | | .. | ... | ... | + ---- + ---------- + -------- + Thoughts: Performance is very important here because it is part of A big query that performs well on its own, and I do not affect its functionality very much, if I can help it.
In relation to "Which is a lag in seconds", it is something that is tried for the end result, and looking for something better with performance optimized for my example:
SELECT SUM (CN) FROM (SELECT AN, ROW_NUMBER), (AN) ROND FROM (maximum SELECT TOP 60 1 Nm. SP_AVALUES from Master) A, (maximum number 1 master 9. Choose .. SP_Avalues) B) C where is present (from SELECT 1 to Times SE.IDID = 10 and SET Imphum & lt; = C.RowID and SE.TimeTo> = C.RowID and current (times 1 times 2D WHERE ID = SEED and D timefrom & lt; = C.RowID and D. TimeTo> = I have a problem with this solution, I have found a row number spool out of the EXISTS query in the query plan, which is COUNT (c *.) I have left the actual number of the question that this approach has been explained that this vision The best to be around On As is reduced by substantially reducing the cost of the query with a row count spool, it also increases a little bit like the cost of execution count query time. Edit further: The end goal is to put it into a process, so table variables and temp tables are also possible tools to use.
On each line, include its successor overlapping row on the same ID value (where it exists) .
Now join the RHS in the LHS Reservative Set for each line Contributions to the time taken by ID: isnull (RHS. Time Frame, LHS Time-To) - LHS As time-value Elpsay, you must give the correct answer by ID .
Note:
- Where there is no overlapping successor line, that calculation is just > > LHS TimTau - LHS Timeframe
- Where there is an overlapping successor line, it will be pure to calculate on it. (RHS.TimeFrom - LHS.TimeFrom) + (RHS.TimeTo - RHS.TimeFrom) < / Code>
that RHS.TimeTo - LHS.TimeFrom
Comments
Post a Comment