Ordering Rows Using a User-Defined Order

By Sean Baird on 01 September 2000 | 5 Comments | Tags: ORDER BY


Jonathan writes: "How would I go about ordering results using a specified order in order to offset and limit the results. Say I have non-unique ids, (23, 12, 98, 3) and I need to return rows that contain these ids in the order I specified above. How could I do this without using any extra queries or sub-queries?

Jonathan -

Thankfully, you didn't say "How can I do this without using any extra tables", because that would have been trickier :o)

Since SQL doesn't natively provide this sort of ordering, you need to use another table that contains your desired key order and join to it. Something like this:

CREATE TABLE Sorter (
      KeyID int,
      SortOrder int
)

INSERT Sorter VALUES (23,1)
INSERT Sorter VALUES (12,2)
INSERT Sorter VALUES (98,3)
INSERT Sorter VALUES (3,4)

SELECT ID, yada
FROM foo
      JOIN Sorter
            ON foo.ID = Sorter.KeyID
ORDER BY Sorter.SortOrder

That should do the trick.
-SQLGuru

Discuss this article: 5 Comments so far. Print this Article. This page has been read 27,103 times.

If you like this article you can sign up for our newsletter. We send it out each week that we post a new article. There's an opt-out link at the bottom of each newsletter so it's easy to unsubscribe at any time.

Email Address:

Email ThisSubscribe to this feedKick itSave to del.icio.usView blog reactions

Related Articles

Dynamic ORDER BY (22 January 2001)

Other Recent Forum Posts

can a fun have out paramter (2 Replies)

How to Compare DATETIME data with null values (2 Replies)

compare of 2 big tables (7 Replies)

complicated update (problem) (4 Replies)

Database consistency problem detected on database (1 Reply)

help needed in get date from datetime (5 Replies)

3 delete queries in one in sp? (11 Replies)

autosum all field value base dates (1 Reply)

Subscribe to SQLTeam.com

Weekly SQL Server newsletter with articles, forum posts, and blog posts via email:

SQLTeam.com Articles via RSS

SQLTeam.com Weblog via RSS

- Advertisement -

- Sponsor's Message -

SQLShare.com Videos

Writing to the Registry from TSQL Using XP_Instance_RegWrite

Reading from the registry to figure out a configuration issue or two is common, but writing to the registry isn't something you'll need to do very often. Still, it's a useful trick to know and we've put it to work showing you how to alter the default folder used for database backups.

Using XP_Instance_RegRead To Get Default Database Path

SQL saves more than a few things to the registry and we can leverage that by reading those settings from TSQL using xp_instance_regread. Easy to use and only one minor quirk to learn, and you'll reading from the registry. Look for the follow up video that demonstrates how to write changes to the registry too.

How to Change the Default Path for New Databases

Typing CREATE DATABASE MyDB gets the job done, but it puts the files in whatever location has been set as the default - which means you should make sure the default is set to a good place, or specify them as part of the CREATE. We'll show you how to adjust the setting (and we've got a video coming up that shows you how to change it from code too!).

Overview of Data Driven Subscriptions in Reporting Services

Subscriptions are a powerful feature of Reporting Services because they allow users to decide which reports to receive. But what do you do when the boss wants a report sent to a group of users, perhaps based on some additional criteria that frequently changes? That's where data driven subscriptions make sense and in our lesson today Devin does a nice overview of the options and requirements.

Using DatePart and DateName

Ever want to get the month out of a date as a number, or as literal text? Many people will do it by parsing the date as a string, but we've got some built in functions that will do it cleanly and consistently.