views:

30

answers:

3

I want to sort a table in sql server. Condition that i need to fulfill is this I have a table that has some records in it like this

Select One
None
Child
Old
Neutral
..
..
..

i want it to be sorted in such a way that Select One comes up and None comes at the end and remaining gets sorted alphabetically.

Select One
Child
Neutral
Old
..
..
..
None

Is it possible some how to do this in any efficient manner. I have few other fields also associated with it. On behalf of these I want to get those values. These fields are to be filled in comboboxes

I have to do this for 12 tables.

+1  A: 

You could add a sequence field to the table - a tinyint which you could use in an order by clause:

select field_name
  from table_name
  order by [sequence]

Or you could use a case statement to create a sort field:

select field_name, 
  case field_name
    when 'Select One' then 1
    when 'Child' then 2
    when 'Neutral' then 3
    when...
  end as sort_field
 from table_name
 order by sort_field
Ray
@Ray: You was close to solution as given by KM but if you understood my Q right then what you wrote is perfectly bad solution because these fields will grow to n.
Shantanu Gupta
I see what you mean - I missed the alphabetical sorting which KM did correctly
Ray
A: 

As these are values that are going to be used for some selection it is common to store such values in tables of their own.

From your question I gather that you are already doing that.

Just add another field that will define sort order, then you can select and order by that field.

There are ways to do this without extra column (assumin the field names is value)

ORDER BY CASE value 
            WHEN 'None' THEN 'ZZZZZ' 
            WHEN 'Select One' THEN 'AAAAA' 
            ELSE values 
         END

(Replace 'AAAAA' and 'ZZZZZ' with appropriate constants for your domain), but I doubt that this approach will be any better in terms of performance or ease of implementation/maintenance.

Unreason
+3  A: 

Try using a CASE in the ORDER BY like this:

SELECT...

    ORDER BY CASE
                 WHEN YourColumn='Select One' then 1
                 WHEN YourColumn='none' then 3
                 ELSE 2
              END,YourColumn

working example:

DECLARE @YourTable table (YourColumn varchar(20))
INSERT @YourTable VALUES ('Select One')
INSERT @YourTable VALUES ('None')
INSERT @YourTable VALUES ('Child')
INSERT @YourTable VALUES ('Old')
INSERT @YourTable VALUES ('Neutral')

SELECT * FROM @YourTable
ORDER BY CASE
             WHEN YourColumn='Select One' then 1
             WHEN YourColumn='none' then 3
             ELSE 2
          END,YourColumn

OUTPUT:

YourColumn
--------------------
Select One
Child
Neutral
Old
None

(5 row(s) affected)
KM
@KM: thx this worked perfectly. Could you please describe it how case is working in this in order by
Shantanu Gupta
case return an expression, when the column is 'Select One' the case returns integer 1, when the column is 'none' it returns an integer 3, for all other values of column, the case returns an integer 2. The ORDER BY then sorts by two values: the CASE's value and then by the actual column's value, so the 1+'Select One' row sorts to the top, the 2+*any column value* rows sort properly in the middle and the 3+'none' sorts to bottom.
KM
@KM : Thx it worked perfectly and also thx for giving xplanation.
Shantanu Gupta