Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Database > SQL Language
|
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 17th, 2004, 06:44 PM
Registered User
 
Join Date: Sep 2004
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
 
Old September 19th, 2004, 03:07 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
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.
 
Old September 19th, 2004, 08:13 PM
Friend of Wrox
 
Join Date: Jun 2003
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





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





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