Results 1 to 5 of 5
  1. #1
    Join Date
    Apr 2004
    Posts
    38

    Unanswered: Special Sorting of Fields with SQL

    Hi there,

    I would like to sort some fields by alphabetically but there is a problem.I need a special sorting feature.My field contains :

    "Select * from MyTable order by Field1"

    Field1
    --------
    1. bottle
    4. bottle
    6. bottle
    2. bottle
    Personal
    3. bottle
    5. bottle

    When i sort this field, my SQL server (PostgreSQL) sorts alphabetically (naturally) :

    Field1
    --------
    1. bottle
    2. bottle
    3. bottle
    4. bottle
    5. bottle
    6. bottle
    Personal

    But i need to sort fields like this :

    Field1
    --------
    Personal
    1. bottle
    2. bottle
    3. bottle
    4. bottle
    5. bottle
    6. bottle

    So How should i sort this specifically?

    Analyzer

  2. #2
    Join Date
    Aug 2004
    Posts
    330
    You could use a CASE statement to generate a field to sort on. Does PostgreSQL support the CASE statement?

  3. #3
    Join Date
    Apr 2004
    Posts
    38
    Hi,

    Thank you for your great reply but i don't know how to use Case statement.I found these examples but i can not solve it.Could you elaborate, or better to show th usage of "case" in my example please?

    http://www.extremeexperts.com/SQL/Ar...SEinORDER.aspx
    http://www.sqlteam.com/item.asp?ItemID=2209

    Analyzer

  4. #4
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    Code:
    ... order by case when Field1='Personal' then 0 else 1 end, Field1
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

  5. #5
    Join Date
    Apr 2004
    Posts
    38
    Ok. It worked.Thank you Rudy

    Analyzer

Posting Permissions

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