sql - Determine how long a phase takes, looking at current row and next row -
I am working on a project for work, using the data logging type system currently SQL Server 2008, if Upgrade if necessary. We are currently taking 1 minute interval of equipment, we are collecting all types of information, I am only concerned about time, and step number.
I am trying to determine the length of time, the machine spends in a certain step before going out. I have a way that I use it currently. Although it takes a long time to run (5+ hours)
I currently have a visual set that looks like this.
with with steps (select *, ROW_NUMBER ()) at the time (depending on the time) from the RONM FOR dbo.Data_All. Time, step Y On Steps as Inner Join Steps from the [Production Stage] Step Rowlum = y. Rome + 1 and phase. [Production phase] & lt; & Gt; Y [production phase]
By doing this, I start to break, at the time that he entered that stage, it will take about 30 seconds to run, so it should not be bad . Realize that we have more than 250,000 records, which are seeing every day, and growing.
| Time | Step #. ----------------------------- | 7-25-2014 14:32 | 11 | | 7-25-2014 15:32 | 13. | 7-25-2014 15:40 | 14 | | 7-25-2014 15:42 | 15 | | 7-25-2014 15:50 | 8 |
Then I'm running a query that to see, to get me, phase, time, next step time, and period This is a question which is taking longer, so I am looking for a way to do it fast. Choose CTI (time), select row number = = ROW_NUMBER (), (order by time), [Time_In_Step from the production phase] [current row]. [Production phase], [current line] [time time] Time, [next line]. Time] [next line] on the [next row] as the [minimum period] from the CTE [current line] to join the CAT [minimum period]. Row_Numbers = [current line] .ow_numbers + 1
After doing this, I get the following, which is what I want. This is just a quick way to do this
| Time | Step #. Next Step Time Duration in the minimum. -------------------------------------------------- --------------- | 7-25-2014 14:32 | 11 | 7-25-2014 15:32 | 60 | | 7-25-2014 15:32 | 13. 7-25-2014 15:40 | 8 | | 7-25-2014 15:40 | 14 | 7-25-2014 15:42 | 2 | | 7-25-2014 15:42 | 15 | 7-25-2014 15:50 | 8 | | 7-25-2014 15:50 | 8 | Etc .. | DateDiff
Any ideas about how to optimize this? Please tell me what you are trying to do, you need more ideas about it.
I cross apply
:
Select c. [Production phase], c.Time, NTEM AS [N Step Time], Dedifff (Mile, Sea Time, N. Time) AS [Minimum Period] Time To Apply Intestate C Cross (Select Top 1 N. * Time_In_Step N From where c.time & lt; n.time) n; An index display will help in
time
.
Comments
Post a Comment