title: Finding Start and Stop events in the log table
body: <p>There is a table with start and stop logs.Between start and stop, there may be other different events that are not known in advance.</p>
  
  <p>It is necessary to output in a separate field so that from start to stop was for example 1, and from stop to start 0</p>
  
  <pre><code>DECLARE @Mytable TABLE (
    [EventId] [int] IDENTITY(1,1) ,
    [EventName] varchar(64) ,
    [EventTime] datetime NOT NULL
  )
  
  INSERT INTO @Mytable
  VALUES
  ('Start','2019-08-31 13:09:48.000'),
  ('EVENT A','2019-08-31 13:10:32.000'),
  ('EVENT B','2019-08-31 15:45:04.000'),
  ('EVENT B','2019-08-31 15:51:09.000'),
  ('EVENT A','2019-08-31 15:55:41.000'),
  ('EVENT C','2019-08-31 15:57:14.000'),
  ('Stop','2019-08-31 15:58:12.000'),
  ('EVENT D','2019-08-31 16:17:04.000'),
  ('EVENT E','2019-08-31 16:19:09.000'),
  ('EVENT W','2019-08-31 16:21:41.000'),
  ('EVENT Y','2019-08-31 16:27:14.000'),
  ('Start','2019-08-31 16:30:48.000')
  
  SELECT * FROM @Mytable ORDER BY [EventTime]
  </code></pre>
  
  <p>My expectation is</p>
  
  <pre><code>EventId EventName   EventTime            Ex
  1           Start           2019-08-31 13:09:48.000  1
  2           EVENT A         2019-08-31 13:10:32.000  1
  3           EVENT B         2019-08-31 15:45:04.000  1
  4           EVENT B         2019-08-31 15:51:09.000  1
  5           EVENT A         2019-08-31 15:55:41.000  1
  6           EVENT C         2019-08-31 15:57:14.000  1
  7           Stop            2019-08-31 15:58:12.000  0
  8           EVENT D         2019-08-31 16:17:04.000  0
  9           EVENT E         2019-08-31 16:19:09.000  0
  10          EVENT W         2019-08-31 16:21:41.000  0
  11          EVENT Y         2019-08-31 16:27:14.000  0
  12          Start           2019-08-31 16:30:48.000  1
  </code></pre>