views:

143

answers:

2

Here is a silly question. Lets say I have a query that produces for a list box, and it produces values for three stores

Store A     18
Store B     32
Store C     54

Now if I ORDER BY in the sql statement the only thing it will do is descending or ascending alphabetically but I want a certain order (only because THEY WANT A CERTAIN ORDER) .....so is there a way for me to add something to the SQL to get

Store B
Store C
Store A

i.e. basically row by row what i want. thanks!

+1  A: 

Add a numeric field, "sequencer", to the table which contains the store names. Use the sequencer values to determine your sort order.

SELECT sequencer, store_name FROM YourTable ORDER BY sequencer;

In the list box, set the column width = 0 for the sequencer column.

HansUp
this one I had a hunch.....wasn't sure if you can add something to sql to accomplish like the month concept in crosstab. thanks Hans!
Justin
You could add a Switch function to your SQL as astander suggested. However that approach can be challenging if your query returns more than a few rows. I would just rather change a value in the sequencer field as needed rather than revising a Switch function. If you make the sequencer float data type you can assign fractional values to fit new rows into an existing sequence.
HansUp
@Hans....Dude honestly that solves what I needed perfectly.....once again (probably for the hundredth time on this website) thanks very much Hans!
Justin
+1  A: 

You can do 1 of 2 things.

Either use a SWITCH stament, something like

SELECT Table1.Store, 
       Table1.Val, 
       Switch([Store]="StoreB",1,[Store]="StoreC",2,[Store]="StoreA",3) AS Expr1
FROM Table1
ORDER BY Switch([Store]="StoreB",1,[Store]="StoreC",2,[Store]="StoreA",3);

Or use a secondary order table, that stores the values of the store names, and an order by value.

astander
I was not at all familiar with that switch statement approach. thanks very much! learned something very cool today!
Justin