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 |
rico_bano
Starting Member
35 Posts |
Posted - 2007-04-23 : 10:04:52
|
im tying to order a result set by using a case statement however i have run into a small problem. the code is belowwhen @orderBy = 1 then classification1, [date], title, type ascwhen @orderBy = 2 then classification1 desc, [date], title, type ascwhen @orderBy = 3 then classification2 [date], title, type ascwhen @orderBy = 4 then classification2 desc, [date], title, type ascwhen @orderBy = 5 then [date] classification1, title, type ascwhen @orderBy = 6 then [date] desc, classification1, title, type ascwhen @orderBy = 7 then title, classification1, [date], type ascwhen @orderBy = 8 then title desc, classification1, [date], type ascwhen @orderBy = 9 then type,classification1, [date], title ascwhen @orderBy = 10 then type desc,classification1, [date], title ascim getting a syntax error on the first line on the first comma.Any ideasThanks |
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-04-23 : 10:11:38
|
You can't do that. You have to use CASE WHEN for each of the column in the ORDER LIST. And you have to ensure that the datatype is same or you have to convert from one to another. Note : The code will not be pretty.case @orderby when 1 then classification1when 2 then classification1when 3 then classification2...when 10 then typeend,case @orderbywhen 1 then [date]when 2 then [date]...end KH |
 |
|
rico_bano
Starting Member
35 Posts |
Posted - 2007-04-23 : 10:14:53
|
eishhhhh that looks messy any ideas how i can dynamically order my results in a better way. Ive tried creating a sql string and executing that but my statement is big and it become very difficult |
 |
|
khtan
In (Som, Ni, Yak)
17689 Posts |
|
dinakar
Master Smack Fu Yak Hacker
2507 Posts |
Posted - 2007-04-23 : 10:28:30
|
Better yet, if you can move the order by to your front end, move it.************************Life is short. Enjoy it.************************http://weblogs.sqlteam.com/dinakar/ |
 |
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-04-23 : 10:34:28
|
quote: Originally posted by dinakar Better yet, if you can move the order by to your front end, move it.************************Life is short. Enjoy it.************************http://weblogs.sqlteam.com/dinakar/
Good idea  KH |
 |
|
rico_bano
Starting Member
35 Posts |
Posted - 2007-04-23 : 10:55:41
|
front end may be an option, but im using pagination and would require a lot of re - working.This one has me totally stumped |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-04-23 : 10:58:27
|
Move to SQL Server 2005.There you have the ROW_NUMBER() function with PARTITION and inline ORDER BY.Peter LarssonHelsingborg, Sweden |
 |
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-04-23 : 10:59:07
|
then take a look at the link i posted. KH |
 |
|
Lamprey
Master Smack Fu Yak Hacker
4614 Posts |
Posted - 2007-04-23 : 13:54:07
|
quote: Originally posted by Peso Move to SQL Server 2005.There you have the ROW_NUMBER() function with PARTITION and inline ORDER BY.Peter LarssonHelsingborg, Sweden
Peter, do you have a "good" way to do a dynamic order by in 2005? I was just messing around, semi-based on OP question and hacked out a quick order by:DECLARE @T TABLE(a int, b int, c int)INSERT @TSELECT 1, 6, 8UNION SELECT 2, 5, 9UNION SELECT 3, 4, 7UNION SELECT 8, 1, 7DECLARE @OrderBy INTSET @OrderBy = 4SELECT *, CASE WHEN @OrderBy = 2 THEN ROW_NUMBER() OVER(ORDER BY b) WHEN @OrderBy = 3 THEN ROW_NUMBER() OVER(ORDER BY c) WHEN @OrderBy = 4 THEN ROW_NUMBER() OVER(ORDER BY c, b) WHEN @OrderBy = 5 THEN ROW_NUMBER() OVER(ORDER BY c, a) --- etc.. ELSE ROW_NUMBER() OVER(ORDER BY a ASC) END AS RowNumberFROM @TORDER BY RowNumber But, I'm always looking for a better way.. |
 |
|
|
|
|
|
|