tags:

views:

614

answers:

5

I have a result from an SQL query that I would like to sort alphabetical, but there are a couple of commonly used results that I would like to float to the top. Is there a simple way I can achieve this either by doing clever SQL or by sorting the (asp.net) datatable once I have it filled?

I know the database ID of the things that I want to keep at the top ahead of time if that makes a difference.

+4  A: 

You could add an addition field called 'Special'. Special doesn't necessarily have to actually exist in the table, you can derive it the query however you see fit. Then sort as follows:

ORDER BY Special DESC, Name ASC

This of course assumes that your non-special rows would be 0, and the alphabetic field is called 'Name'

Greg Dean
+6  A: 

You could define a custom column that sets a "special flag" that you can sort by. Then, sort by the alphabetical column.

SELECT ...
    IsSpecial = CASE WHEN RowID IN (100,534,203) THEN 1 ELSE 0 END
FROM ...
ORDER BY IsSpecial DESC, RowID ASC, Name ASC
EndangeredMassa
+10  A: 

The easiest way to do this is to sort by something that sets those fields aside from the others...

Under the assumption you have access to modify the structure of the table (which you may not), then add a "sticky" field which allows you to mark items as sticky and attach an order to the sticky items if you like. Sort by this field before the regular sort.

As for a trick to do this without that field defined - you would need to sort by some data that you can find in these fields or maintain the ids in a separate table - or a list in your query... not the most ideal way, but it will work.

BenAlabaster
+1 for making this data driven rather than procedure driven
Joel Coehoorn
Agreed--having a column in there that maintains this state is clearer. On the other hand, if you're doing this to generate a total or some calculated statistic, the other answers can help. Or, if you are using temp tables, then this answer works well, too
Michael Haren
I really like this idea, especially the ordering within the "sticky" items. You could use one column for this, IE StickyOrder, and have the sticky items set to 1, 2 or 3 and have the "non-sticky" items all set to value 200 or something. Then you just need a ORDER by StickyOrder ASC, Name ASC
Nathan Koop
@Nathan: Exactly
BenAlabaster
+1  A: 

To add to @EndangeredMassa:

You can also do something similar if you are already doing unions:

SELECT 0 QueryType, Desc, GrandTotal
FROM YourTable

UNION ALL

SELECT 1 QueryType, Desc, LineItems
FROM YourTable

ORDER BY QueryType, Desc

Note: unions may not be ideal in this example...it's just an example!

Michael Haren
That's what I was working to....and fluffed it, too much haste!!
Kev
+4  A: 

Just Order by a case statement that uses whatever rules you specify...

  ...
   Order By Case 
      When [My Rules that designate special] 
       Then 0 Else 1 End
Charles Bretana
That's simpler. I forgot about conditions in WHEN sections.
EndangeredMassa