[SQL Server] Use ORDER BY in query UNION
First of all I must say that I am a newbie when it comes to SQL Server 2008.
«UNION does nothing other than stack the data of multiple result sets in one result sets.
While using the UNION you have to consider few things like: column name, number and aliases should be mentioned in first part of the SELECT.»
Here is my problem:
ORDER BY clause will sort the complete result sets, it will use the column name of first SELECT statement and should be write down after the last SELECT statement.
I can not use ORDER BY in this query UNION ALL.
This is my error:
[Err] 42000 - [SQL Server] ORDER BY items must appear in the select list if the statement contains a UNION, INTERSECT or EXCEPT operator.
Please check my code below.
If you have link for similar task, please give it me.
Can you explain any one or any sample code related this.
Your help would be very appreciated.
Thanks in advance for your time and hints.
COALESCE ([MAT], 'Tot') AS [MAT],
WHEN 'mol' THEN
WHEN 'lip' THEN
WHEN 'mre' THEN
WHEN 'irt' THEN
WHEN 'pmc' THEN
WHEN 'mal' THEN
WHEN 'ras' THEN
WHEN 'uot' THEN
WHEN 'lac' THEN
WHEN 'bup' THEN