Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can simply widen the range, but this may bring back different records than you intend.</p> <pre><code>SELECT RECORD, EVENT, TIME FROM SO_RecordEvent AS R WHERE TIme BETWEEN '6/26/2012 9:55AM' AND '6/26/2012 11:06AM' </code></pre> <p>This will return all records for anything that has either a start or end time within the range, including the associated records falling outside of the range (In other words records that only have one time in the range - but began before or ended after - it will still show the start or end time outside of it) and allow you you shorten your range.</p> <pre><code> ;WITH X AS ( SELECT RECORD,EVENT,TIME FROM SO_RecordEvent ) SELECT R.RECORD,R.EVENT,R.TIME FROM SO_RecordEvent AS R INNER JOIN X ON R.Record = X.Record WHERE X.TIme BETWEEN '6/26/2012 10:05AM' AND '6/26/2012 11:05AM' GROUP BY R.RECORD,R.EVENT,R.TIME </code></pre> <p><strong>But I think you may really want something like this</strong>, which truly gives you everything that was started during that time, even if it started AND ended outside of the range, as is your record 2 example. </p> <h2><strong>EDIT</strong></h2> <p>Changed logic - instead of addressing situations decided to think about it this way - anything that started within start and end, anything that ended within start and end, and anything that started before and ended after. I think this covers anything that runs during this time (starts before ends within, starts and ends within, starts within and ends after, and starts before and ends after) </p> <pre><code> SELECT X.RECORD,X.TIME AS STARTTIME,Y.TIME AS ENDTIME FROM SO_RecordEvent AS X INNER JOIN SO_RecordEvent Y ON Y.Record = X.Record AND Y.EVENT = 'END' WHERE X.EVENT = 'START' AND ((X.TIME &gt;= '6/26/2012 10:00AM' AND X.TIME &lt;= '6/26/2012 11:00AM') OR (Y.TIME &gt;= '6/26/2012 10:00AM' AND Y.TIME &lt;= '6/26/2012 11:00AM') OR (X.TIME &lt;= '6/26/2012 10:00AM' AND Y.TIME &gt;= '6/26/2012 11:00AM')) </code></pre> <p>Variables to play with:</p> <pre><code> DECLARE @START datetime, @END datetime SET @START = '6/26/2012 10:00AM' SET @END = '6/26/2012 11:00AM' SELECT X.RECORD,X.TIME AS STARTTIME,Y.TIME AS ENDTIME FROM SO_RecordEvent AS X INNER JOIN SO_RecordEvent Y ON Y.Record = X.Record AND Y.EVENT = 'END' WHERE X.EVENT = 'START' AND ((X.TIME &gt;= @START AND X.TIME &lt;= @END) OR (Y.TIME &gt;= @START AND Y.TIME &lt;= @END) OR (X.TIME &lt;= @START AND Y.TIME &gt;= @END)) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload