Hi, Here is the example query. Query1:
Query 2 output:
I Need to write union for the query 1 and query 2 but the result set of the query 1 should come first. If i write the union query as below,
The output comes as below.
I need a output of query 1 to come first. Please help any question let me know.
It looks like there are two things preventing you from getting the output you're after.
Currently your final output is 1 2
You're wanting to get
...in that order. To achieve this you're going to need something like
If you don't want the sort column coming through in the final output, this might be a good option:
In this instance you'll get the right answer using UNION instead of UNION ALL, because the sort_order column we added differentiates between the output from q1 and q2, so why did I bother? Because UNION ALL is much cheaper than UNION. In order to get the distinct list that UNION outputs it has to sort the data to find the duplicates. This extra work comes at a price, especially in long lists.
answered Sep 06, 2014 at 10:05 PM