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 > Special Sorting of Fields with SQL

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 11-29-04, 15:56
Analyzer Analyzer is offline
Registered User
 
Join Date: Apr 2004
Posts: 38
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
Reply With Quote
  #2 (permalink)  
Old 11-29-04, 16:14
urquel urquel is offline
Registered User
 
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?
Reply With Quote
  #3 (permalink)  
Old 11-29-04, 16:37
Analyzer Analyzer is offline
Registered User
 
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
Reply With Quote
  #4 (permalink)  
Old 11-29-04, 18:58
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
Code:
... order by case when Field1='Personal' then 0 else 1 end, Field1
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #5 (permalink)  
Old 11-30-04, 04:39
Analyzer Analyzer is offline
Registered User
 
Join Date: Apr 2004
Posts: 38
Ok. It worked.Thank you Rudy

Analyzer
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