title: SQL Query - Improvement Needed
body: <p>I have written an extremely ugly SQL Query and to be honest I am quite embarrassed to post it here... But I need some help regarding the 4 SELECT statements that are the same. Is there a better way to do this without creating that table?</p>
  
  <p>I am not sure if the contents of the huge SELECT is relevant to answering this question. In case it is, I will try to put that query up. (But since I highly simplified the entire example that might be quite difficult to do).</p>
  
  <p>Any help is very much appreciated.</p>
  
  <p>(This question is a follow-up to another question I had asked here: <a href="https://stackoverflow.com/questions/9889951/comparing-consecutive-rows-in-ms-access">Comparing &quot;Consecutive&quot; Rows in Ms Access</a> )</p>
  
  <pre><code>SELECT t2.* FROM 
    (SELECT t.ID, t.SubID, t.Time, t.Value, t.value2, t.value
     FROM 
  </code></pre>
  
  <p>(
  HUGE SELECT HERE with 3 joins) t</p>
  
  <pre><code>   WHERE t.Time=4)  AS t2 
             LEFT JOIN 
     (SELECT t.ID, t.SubID, t.Time, t.Value, t.value2, t.value
      FROM
  </code></pre>
  
  <p>(
  HUGE SELECT HERE with 3 joins) t</p>
  
  <pre><code>    WHERE t.Time=3)  AS t1 
           ON t2.Id = t1.Id
           WHERE t2.value&lt;&gt;t1.value
           OR t2.value2&lt;&gt;t1.value2 
           OR t1.Id Is Null
  </code></pre>
  
  <p>UNION ALL</p>
  
  <pre><code>   SELECT t1.* FROM
     (SELECT t.ID, t.SubID, t.Time, t.Value, t.value2, t.value
      FROM
  </code></pre>
  
  <p>(
  HUGE SELECT HERE with 3 joins) t</p>
  
  <pre><code>     WHERE t.Time=3) AS t1
             LEFT JOIN
      (SELECT t.ID, t.SubID, t.Time, t.Value, t.value2, t.value
      FROM
  </code></pre>
  
  <p>(
  HUGE SELECT HERE with 3 joins) t</p>
  
  <pre><code>    WHERE t.Time=4) AS t2
            ON t1.Id=t2.Id
            WHERE t1.value&lt;&gt;t2.value
           OR t1.value2&lt;&gt;t2.value2 
            OR t2.Id Is Null
  </code></pre>
  
  <p>ORDER BY Id, EntryNo;
  </p>