Sie sind auf Seite 1von 4

TSQL - Is it possible to define the sort order?

4789241

Is it possible to define a sort order for the returned results?

up vote down I would like the sort order to be 'orange' 'apple' 'strawberry' not ascending or descending. vote 1 I Know ORDER BY can do ASC or DESC but is there a DEFINED('orange', 'apple',
favorite

'strawberry') type thing? This will be running on SQL Server 2000.


sql sql-server tsql sql-server-2000 sql-order-by

share|improve this question

edited Jan 25 '11 at 1:56

asked Jan 25 '11 at 1:52

OMG Ponies 103k1192175

Justin808 3,39011649

85% accept rate

feedback

4 Answers
active oldest
4789252

votes
It's incredibly clunky, but you can use a CASE statement for ordering: SELECT * FROM Blah ORDER BY CASE MyColumn WHEN 'orange' THEN 1 WHEN 'apple' THEN 2 WHEN 'strawberry' THEN 3 END Alternately, you can create a secondary table which contains the sort field and a sort

up vote down vote


accept ed

order. TargetValue orange apple strawberry SortOrder 1 2 3

And join your table onto this new table.


share|improve this answer answered Jan 25 '11 at 1:55

LittleBobbyTables 11.1k51738

Hah! Like I'm going to trust any SQL coming from someone called LittleBobbyTables :-) paxdiablo Jan 25 '11 at 2:04 @pax - <insert picture of Admiral Ackbar here> :-) LittleBobbyTables Jan 25 '11 at 2:06 +1 for the table option, always more flexible RichardTheKiwi Jan 25 '11 at 2:26 feedback

4789250

Use a CASE statement: ORDER BY CASE your_col WHEN 'orange' THEN 1 WHEN 'apple' THEN 2 WHEN 'strawberry' THEN 3 END Alternate syntax, with an ELSE: ORDER BY CASE WHEN your_col = 'orange' THEN 1 WHEN your_col = 'apple' THEN 2 WHEN your_col = 'strawberry' THEN 3 ELSE 4 END

up vote down vote

share|improve this answer

answered Jan 25 '11 at 1:55

OMG Ponies 103k1192175 feedback If this is going to be a short-lived requirement, use a case statement. However, if you

4789284

up vote down vote

think it may be around for a while, and it's always going to be orange/apple/strawberry order (or even if not - see below), you may want to think about sacrificing some disk space to gain some speed. Create a new column in your table called or_ap_st and use an insert/update trigger to populate it with the number 1, 2 or 3, depending on the the value of your fruit column. Then index on it. Since the only time the data in that column will change is when the row changes, that's the best time to do it. The cost will then be incurred on a small number of writes rather than a large number of reads, hence amortised over the select statements. Your query will then be a blindingly fast: select field1, field2 from table1 order by or_ap_st; with no per-row functions killing the performance. And, if you want other sort orders as well, well, that's why I called the column or_ap_st. You can add as many other sorting columns as you need.
share|improve this answer answered Jan 25 '11 at 2:00

paxdiablo 227k29329690 feedback


4789261

What I do in that case is ORDER BY CASE WHEN FRUIT = 'Orange' THEN 'A'

up vote down vote

WHEN FRUIT = 'Apple' THEN 'B' WHEN FRUIT = 'Strawberry' THEN 'C' ELSE FRUIT

Das könnte Ihnen auch gefallen