Solved

SQL - order result set when using union

Posted on 2016-08-24
2
37 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
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

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

919 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now