Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Database > SQL Language
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
SQL Language SQL Language discussions not specific to a particular RDBMS program or vendor.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Language section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old September 17th, 2004, 06:44 PM
Registered User
 
Join Date: Sep 2004
Location: , , .
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default order by In clause items?

Is there a fast way to order a Select result set to be the same as the In clause used in the Where clause?

For example:
SELECT * FROM tCompanies
WHERE idCompany IN (987, 321, 654)
ORDER BY (987, 321, 654)

It seems this would be a somewhat common need.

I can envision a stored procedure using a temp table with an extra intSortBy column, an array with the In clause numbers and sort number, and a loop to populate the intSortBy column. This would be fine for small result sets, but probably too time-consuming for large sets.

thanks in advance,

Conrad
Reply With Quote
  #2 (permalink)  
Old September 19th, 2004, 03:07 PM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

It seems you are confused about what ORDER BY is for. You order by a column or set of columns. I think your intent is to get the results with a custom order of 987, 321, 654. In order to do this, you have to do a bit of trickery. Fortunately when SQL builds its result set, it processes the order by after building the columns, so you can create some custom column results then order by that. In this case, we can use to the SQL CASE statement to create your "intSortBy" column. This should work:

SELECT *, CASE idCompany WHEN 987 THEN 1 WHEN 321 THEN 2 WHEN 654 THEN 3 END AS Order
FROM tCompanies
WHERE idCompany IN (987, 321, 654)
ORDER BY Order

Now, 987 will be 1st, 321 2nd, and 654 3rd. Add columns to the ORDER BY clause as needed, and add cases to the CASE as needed.
Reply With Quote
  #3 (permalink)  
Old September 19th, 2004, 08:13 PM
Friend of Wrox
 
Join Date: Jun 2003
Location: Bangalore, KA, India.
Posts: 2,480
Thanks: 0
Thanked 1 Time in 1 Post
Default

Conrad,

But, are those values within IN(...) fixed? If fixed, then this is the a good solution to achieve that. If thats going to be dynamic(say using a subquery there to generate list of Ids), then this would not apply, as one wouldn't be sure how many are to come and what the values are, to decide the number of cases and expressions within it.

Hope that helps.
Cheers!

_________________________
- Vijay G
Strive for Perfection
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
ERROR 145: ORDER BY ITEMS... SELECT DISTINCT... tbaquero SQL Server 2000 7 May 8th, 2008 02:30 AM
Order by clause priyan.viji SQL Language 2 December 17th, 2007 02:45 AM
re arrrange nulls in order by clause mat41 SQL Language 13 August 7th, 2007 08:10 PM
Order by clause mateenmohd SQL Server 2000 4 April 6th, 2004 06:48 AM
displaying 6 items only having 20 items Lakshmi KS VB Components 1 February 17th, 2004 10:34 AM



All times are GMT -4. The time now is 01:19 PM.


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.