title: Improving SQL Server subquery performance
body: <p>I have this view from Microsoft SQL Server 2008:</p>
  <pre><code>SELECT     
      Badgenumber, Date, 
      MIN(CONVERT(VARCHAR(16), CAST(CheckIn AS TIME), 100)) AS [CHECK IN], 
      MAX(CONVERT(VARCHAR(16), CAST(CheckOut AS TIME), 100)) AS [CHECK OUT]
  FROM         
      (SELECT     
           Badgenumber, 
           CONVERT(varchar, CHECKTIME, 105) AS Date, 
           CHECKTIME AS CheckIn, NULL AS CheckOut
       FROM          
           dbo.View_ChronoInOut
       WHERE      
           (CHECKTYPE = 'I')
       UNION ALL
       SELECT     
           Badgenumber, CONVERT(varchar, CHECKTIME, 105) AS Date,  
           NULL AS CheckIn, CHECKTIME AS CheckOut
       FROM         
           dbo.View_ChronoInOut AS View_ChronoInOut_1
       WHERE     
           (CHECKTYPE = 'O')
   ) AS fixTable
  GROUP BY 
      Badgenumber, Date
  </code></pre>
  <p>It takes 8-12 seconds when fetching data per badgenumber.</p>
  <p>Can anyone help me improve this query's performance?</p>