Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Ordering out of middle 4

Status
Not open for further replies.

webmigit

Programmer
Aug 3, 2001
2,027
US
I'm the new programmer on a big website

Problem is that the guy who built it didn't centralize the databases.. so I have about 300 virtually identical databases that cannot be merged..

Due to ND (non-disclosure) issues, I have to be a little vague here...

Let's say that this company distributes Toys.. well in that form Milton Bradley has paid my boss to list all their products on top... I know which products are milton bradley's... And I want to use a query to sort that..

Is there a way to say like...

Select * from games
order by where gameMaker='MB'.. so that they appear on top?

Tony Hicks

ALFII.com
---------------------
If this post answered or helped to answer your question, please reply with such so that forum members with a similar question will know to use this advice.
 
Code:
Select * from games
order by case when gameMaker='MB' then 0 else 1 end, 
  gameMaker
 
Hi,

Try this....

Select * from games
order by
Case gameMaker When 'MB' Then 1 Else 2 End

Hope it helps.....

Sunil
 
You could run a case statement in your select:

select
Toys.*,
case when Manufacturer = 'MB' then 1 else 2 end As Manufacturer_OrderBy
from
Toys
Order by Manufacturer_OrderBy

The syntax might be a little off. I don't have SQL Query Analyzer on my home computer, but it should close.

H


 
Though it was sunila's that I used to make it work, HBelk's later response seems cleaner.. I'll try it tomorrow but you all get a *

ALFII.com
---------------------
If this post answered or helped to answer your question, please reply with such so that forum members with a similar question will know to use this advice.
 
You might consider that someday your boss may have you change which one is first or have another company be second. I would put in a new column called order by and set the sort order by using this column. In the long run it will be easier to maintain and queries using the sort should be faster without having to resolve the Case statement every time.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top