Solved

SQL - order result set when using union

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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Viewers will learn how the fundamental information of how to create a table.

746 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

10 Experts available now in Live!

Get 1:1 Help Now