title: SQL Server query using Union - any good alternate possible?
body: <p>I have a SQL Server query as:</p>
  
  <pre><code>SELECT top 1 vConsentInfo  FROM 
  (           
              SELECT cons.vConsentInfo,cons.dTimeStamp ,logs.iPartnerProfileID 
              FROM H_OutMessageLog logs INNER JOIN H_OutMessageConsent cons on cons.iOutMessageQID = logs.iOutQueueID 
                  WHERE logs.iPatID = 65686 and logs.iPracID = 4
          UNION  
              SELECT cons.vConsentInfo,cons.dTimeStamp,Q.iPartnerProfileID 
              FROM H_OutMessageQueue Q INNER JOIN H_OutMessageConsent cons on cons.iOutMessageQID = Q.iOutQueueID 
                  WHERE Q.iPatID = 65686 and Q.iPracID = 4
  ) A 
  WHERE A.iPartnerProfileID = Prof.IPartnerProfileID
  Order BY dTimeStamp DESC
  </code></pre>
  
  <p>The table works as : a record get inserted into <code>H_OutMessageQueue</code> at the begining; then its inserted into <code>H_OutMessageConsent</code>...
  Now there is a seperate worker process that processes records from <code>H_OutMessageQueue</code> and log them into <code>H_OutMessageLog</code>.... </p>
  
  <p>Can I get rid of this <code>UNION</code> thing ? Please note that this is a sub Query of a larger <code>CTE</code> query.</p>