Welcome to the dBforums forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions, articles and access our other FREE features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload your own photos and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact contact support.

If you prefer not to see double-underlined words and corresponding ads, place your cursor
here for ContentLink opt out.

Go Back  dBforums > Data Access, Manipulation & Batch Languages > ANSI SQL > Special ordering

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 09-30-04, 13:51
rickwat rickwat is offline
Registered User
 
Join Date: Sep 2004
Posts: 3
Question Special ordering

I would like to query one table and select one customer with a specific ID (Customer A) and then obtain all the other customers from the table. The presentation order must be Customer A followed by all the other customers in order of birth date (asc). Customer A's birthdate shouuld not be included in the ordering because he needs to be at the top of the list. Any suggestions would be appreciated. Thanks!

This does not give me the correct order but shows conceptually what I am trying to get at:

Select
SESSIONID, CUSTOMERNAME, CUSTOMERID, CUSTOMERDOB
FROM CUSTOMER
WHERE CUSTOMERID = 1234567894
UNION
Select
SESSIONID, CUSTOMERNAME, CUSTOMERID, CUSTOMERDOB
FROM CUSTOMER
ORDER BY CUSTOMERDOB ASC
Reply With Quote
  #2 (permalink)  
Old 09-30-04, 13:53
andrewst andrewst is offline
Moderator.
 
Join Date: Sep 2002
Location: UK
Posts: 4,874
Select
SESSIONID, CUSTOMERNAME, CUSTOMERID, CUSTOMERDOB, 1 DUMMY
FROM CUSTOMER
WHERE CUSTOMERID = 1234567894
UNION
Select
SESSIONID, CUSTOMERNAME, CUSTOMERID, CUSTOMERDOB, 2 DUMMY
FROM CUSTOMER
ORDER BY DUMMY, CUSTOMERDOB ASC
__________________
Tony Andrews
http://tonyandrews.blogspot.com
Reply With Quote
  #3 (permalink)  
Old 09-30-04, 14:36
rickwat rickwat is offline
Registered User
 
Join Date: Sep 2004
Posts: 3
Thumbs up Works-Thank you!

Thank you very much Tony!
Reply With Quote
  #4 (permalink)  
Old 09-30-04, 16:03
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 13,539
don't forget to use UNION ALL instead of UNION because there can be no dupes between the two subselects (because ot the DUMMY column)

this will save an unnecessary sort prior to the ORDER BY sort
__________________
r937.com | rudy.ca

pre-order my book Simply SQL from Amazon
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

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