If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > ANSI SQL > Sorting in UNION ALL

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 11-08-07, 19:49
shri7 shri7 is offline
Registered User
 
Join Date: Nov 2007
Posts: 2
Sorting in UNION ALL

Hi

I have following query


SELECT 'ALL'
UNION ALL
SELECT DISTINCT Emp_Name

FROM

EMP
How do I sort the result ?
Thanks
Reply With Quote
  #2 (permalink)  
Old 11-08-07, 20:28
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
by adding an ORDER BY clause

if you don't give the column an alias name in the first subselect, you can use the ordinal column number --
Code:
SELECT 'ALL' 
UNION ALL 
SELECT DISTINCT Emp_Name 
  FROM EMP 
ORDER
    BY 1
alternatively, if you reverse the subselects, you can use the column name --
Code:
SELECT DISTINCT Emp_Name 
  FROM EMP 
UNION ALL 
SELECT 'ALL' 
ORDER
    BY Emp_Name
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 11-08-07, 21:23
shri7 shri7 is offline
Registered User
 
Join Date: Nov 2007
Posts: 2
Thanks for the quick reply. It worked
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On