title: sql server query order by with union and replacing multiple columns' null value
body: <p>I have a query to get records from 4 different tables. I am using <code>union</code> to get the final records. My problem is I don't have same number and type of columns in all 4 tables so I am making dummy columns with <code>null</code> value. The problem I am facing is in sorting. I am sorting records using datetime desc, but some times when dates are same then I want the secondry sort column on alerts that can be of High, Medium or Null types. I want Null columns on bottom but I am getting on top. So I tried to use <code>ISNULL(column_name, 'aaaa')</code> in <code>ORDER BY</code> clause, but I am not getting actual result. Can any one help? </p>
  
  <pre><code>SELECT CreatedDt as 'Date/Time' , null AS [Alert_Type] FROM TblA
  union  
  SELECT CreatedDt as 'Date/Time' , null AS [Alert_Type] FROM TblB
  union  
  SELECT CreatedDt as 'Date/Time' , null AS [Alert_Type] FROM TblC
  union
  SELECT CreatedDt as 'Date/Time' , Alert_Type FROM TblD
  ORDER BY CreatedDt, ISNULL (Alert_Type,'aa') DESC
  </code></pre>
  
  <p>The error is </p>
  
  <pre><code>ORDER BY items must appear in the select list if the statement contains a UNION, INTERSECT or EXCEPT operator.
  </code></pre>
  
  <p>Thank you..</p>