Solved

SQL - order result set when using union

Posted on 2016-08-24
2
47 Views
Last Modified: 2016-08-24
When using union all, how can I order result set of how it gets displayed?  For example

Right now it returned

Descripton    SalesType        Customercode  Jan      Feb       Mar       Apr      May      Jun      Jul      Aug      Oct      Nov      Dec      Total
Sale            Direct             1             $          $      $
Manager            Resell             1             $          $      $
Inter            Extr             1             $          $      $
Exter            EU             1             $          $      $

I want to return like or any order of result set

Descripton    SalesType        Customercode  Jan      Feb       Mar       Apr      May      Jun      Jul      Aug      Oct      Nov      Dec      Total


Inter            Extr             1             $          $      $
Exter            EU             1             $          $      $
Manager            Resell             1             $          $      $
Sale            Direct             1             $          $      $


Table sample

column names

Descripton    SalesType        Customercode  Jan      Feb       Mar       Apr      May      Jun      Jul      Aug      Oct      Nov      Dec      Total


select *from
(
select * from #temp_table1
union all
select * from #temp_table2
union all
select * from #temp_table3
union all
select * from #temp_table4


a group by Descripton,SalesType,Customercode,Jan,Feb ,Mar Apr,May,Jun,Jul,Aug,Oct,Nov,Dec,Total
0
Comment
Question by:jagr12
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 65

Accepted Solution

by:
Jim Horn earned 500 total points
ID: 41769238
To pull off a customized ordering you'll need to include an ORDER BY with a CASE block, like this..
<total air code, look at the last line>
select * from (
   select * from #temp_table1
   union all 
   select * from #temp_table2
   union all 
   select * from #temp_table3
   union all 
   select * from #temp_table4) a
group by Descripton,SalesType,Customercode,Jan,Feb ,Mar Apr,May,Jun,Jul,Aug,Oct,Nov,Dec,Total
ORDER BY CASE Descripton 
   WHEN 'Inter' THEN 1 
   WHEN 'Exter' THEN 2 
   WHEN 'Manager' THEN 3 
   WHEN 'Sale' THEN 4 
   ELSE 5 END

Open in new window


For a tutorial on CASE blocks check out SQL Server CASE Solutions.
0
 

Author Comment

by:jagr12
ID: 41769284
Jim,
Thank you very much. This is exactly what I need.
0

Featured Post

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question