Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
AskSQLTeam
Ask SQLTeam Question
0 Posts |
Posted - 2000-09-01 : 11:55:25
|
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? Article Link. |
|
SeanTWarner
Starting Member
1 Post |
Posted - 2003-06-11 : 15:56:57
|
A quick/Simple Solution if your set of keys or options are small Although the sort table will support larger sets and will perform better using indexes, the following solution can be implemented and the sort order can be dynamicly passed into a procExamples:-- Note: depending on data, use the '~' , ',' , Pipe or other charater must be concatenated to the sort field when using the charindexSet @SortString = '23~12~98~3~'SELECT ID, yadaFROM foo ORDER BY CharIndex(Convert(varchar(3),id) + '~', @SortString)-- or Set @SortString = 'PA|NJ|AZ|'SELECT State, yadaFROM foo ORDER BY CharIndex(State + '|', @SortString)-- Note: other states will be first (Charindex 0)SELECT State, yadaFROM foo ORDER BY CharIndex(State + '|', @SortString + state + '|')-- Note: other states will be last(Charindex will find it at the end if it is not in the "sort String")-- Northwind table sample-- more complex, let's cover everything!Declare @SortString VarChar(255)Set @SortString= 'RJ|AK|CA|Lara|Québec|'SELECT *FROM CustomersORDER BY CHARINDEX(ISNULL(RTRIM(Region), 'XXX') + '|', @SortString + ISNULL(RTRIM(Region), 'XXX') + '|') -- Add the default sort for any remaining Regions , ISNULL(RTRIM(Region), 'XXX') |
|
|
jsmith8858
Dr. Cross Join
7423 Posts |
Posted - 2003-06-11 : 23:15:41
|
Nice one, Sean.For best performance, parse the string and fill up a table variable or a temp table, along with an identity column which ends up being the order by which you sort. similiar to the CSV-to-table techniques that have been discussed here many times.i.e., declare @t (ID varchar(2), sort int identity)for each token in the string insert into @t (value) values (token) -- note that the identity is incrementingnextthen:select yourdata.*fromyourdatainner join@tonyourdata.ID= @t.IDorder by @t.Sort- Jeff |
|
|
Page47
Master Smack Fu Yak Hacker
2878 Posts |
Posted - 2003-06-12 : 07:04:58
|
And there we are, right back where we started. Jay White{0} |
|
|
viraldesai
Starting Member
1 Post |
Posted - 2009-11-11 : 23:46:08
|
i have a table #temp1(id, Name groupname ) and record like this 1 R1 S2 R3 S3 R2 S4 R4 D5 R5 D6 R6 K7 R7 K8 R8 L9 R9 L10 R10 L11 R11 Kand i want to display record based on user defind sorting order e.g.1 R4 D2 R5 D3 R6 K4 R7 K5 R11 K6 R1 S7 R3 S8 R2 S9 R8 L10 R9 L11 R10 L |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2009-11-11 : 23:49:50
|
viraldesai,Have you take a look at the article ? KH[spoiler]Time is always against us[/spoiler] |
|
|
|
|
|
|
|