Results 1 to 4 of 4
  1. #1
    Join Date
    Sep 2004
    Posts
    3

    Question Unanswered: 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

  2. #2
    Join Date
    Sep 2002
    Location
    UK
    Posts
    5,171
    Provided Answers: 1
    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

  3. #3
    Join Date
    Sep 2004
    Posts
    3

    Thumbs up Works-Thank you!

    Thank you very much Tony!

  4. #4
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    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
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •