sql - Self join of time series data -


I need some help that I think should be a very simple self-job query. This is just adding the matching time and end time to two records in one record

I have the following in a table

  Time event 08:00 start 09 : 00 stop 10:30 start 10:45 stop 11:00 start 11:15 start 12:00 stop 12:30 stop  

I want a scene like this

< Pre> starttime stoptime 08:00 09:00 10:30 10:45 11:00 11:15 12:00 12:30

Note that starting a closest match Should or should Time. If there is no matching event for any reason, then it should be kept blank.

Thanks,

Disclaimer: Thinking of a different table design will never be a good idea to join the dates and time. If you have an id of the event that you are starting then you can easily join in trying to find the end of the match of that event.

Try for SQL Server: Enter <@>

  '@Events value (enter '09: 00,' Stop '): @Events table (EVENTTIME FOUR (5), EventType varchar (5)) @Events value (00 ',' Start '08) 'included in the declaration @Events values ​​(such as '10: 30', 'start') @Events value ('10 : 45 ',' Stop ') Enter the @Events value ('11: 00', 'Start') Check @ INSERT Events values, select dt.StartTime ('11: 15 ',' Start ') @Events Enter in value ('12: 00 ',' Stop ') @Events Value (30', 'Stop' '12) from insert dt.StopTime (StartTime, in the form of case Do not select p.EventTime c.EventType! = 'Stop' then and zero c.EventTime as End stopTime, as p.EventTime SortBy @Events P from within P.EventTime & lt Combination @Events C; C.EventTime WHERE p.EventType = 'Start' and c.EventTime = (SELECT MIN (EventTime) F) Rome @Events Where EVENTTIME & gt; p.EventTime) Union Selection as Start Time, p EventTime, p.EventTime SortBy join @Events P Interiors @Events at p.EventTime & gt; C.EventTime ou p.EventType = 'Closed' and c.EventTime = (Selection Max (EVENTTIME) from @Events EVENTTIME & lt; p.EventTime) and c.EventType = 'Stop') by command dt dt.SortBy Output:  
  Start Time Stoptime --------- 30 10:45 11:00 False 11:15 12:00 False 12:30 (5 lines (affected))  

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 -