I have a query:
select * from (select top 100 percent ... from [table]
where [field1] is not null
order by [field1]) a
union all
select * from (select top 100 percent ... from [table]
where [field1] is null
order by [field2]) b
The subqueries are ordering their results correctly. I want the exact results of the first subquery, THEN the exact results of the second subquery. However, I’m actually getting the concatenated results ordered in some odd way that I can’t even discern a pattern from. I have [field2] values scattered about the result set apparently at random.
How can I get the first query results on top, and the second query results on bottom?
Edit: To clarify, I want the top query ordered by its own conditions, and then I want the bottom query ordered by its own conditions.
I am not sure, whether or not UNION ALL is meant to preserve the order of rows.
You can force the order you want by using the following statement: