dbTalk Databases Forums  

multiple column sort

comp.databases.ms-sqlserver comp.databases.ms-sqlserver


Discuss multiple column sort in the comp.databases.ms-sqlserver forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
m
 
Posts: n/a

Default multiple column sort - 01-07-2010 , 06:52 AM






Hello,

I need to provide sorting ability across multiple column.

Data which would be sorted are retrieved with stored procedure.

I have two solutions:

1. if statement
depending of sort type I must create diferent select statement

in case many columns there would be so many variations (also, there must be
considered sort direction ASC or DESC what increase variations double)

2. dynamic sql

Is there any additional way, which one is better?

Please advice,

Best regards

Reply With Quote
  #2  
Old   
Mark D Powell
 
Posts: n/a

Default Re: multiple column sort - 01-07-2010 , 09:08 AM






On Jan 7, 7:52*am, "m" <mirosla... (AT) gmail (DOT) com> wrote:
Quote:
Hello,

I need to provide sorting ability across multiple column.

Data which would be sorted are retrieved with stored procedure.

I have two solutions:

1. if statement
depending of sort type I must create diferent select statement

in case many columns there would be so many variations (also, there must be
considered sort direction ASC or DESC what increase variations double)

2. dynamic sql

Is there any additional way, which one is better?

Please advice,

Best regards
Generally speaking static SQL is preferable to dynamic SQL. However,
there are times when dynamic SQL comes in handy. If you have only a
couple of possible orderings I would go with static SQL. If you have
numerous possible desired orderings with varying order by column lists
then I would build the order by clause in the procedure logic.

Just make sure you are not trying to do too much in a single procedure
and that all the SQL's in question are really just variations of one
basic query rather than trying to code logic to accomodate several
different basic queries that just happen to use the same source
tables.

HTH -- Mark D Powell --

Reply With Quote
  #3  
Old   
Plamen Ratchev
 
Posts: n/a

Default Re: multiple column sort - 01-07-2010 , 10:17 AM



You can also use CASE expressions. For example, if the data type of the columns is the same:

ORDER BY CASE @column
WHEN 'column1' THEN column1
WHEN 'column2' THEN column2
WHEN 'column3' THEN column3
END

If the data types are different then you have to use multiple CASE expressions:

ORDER BY CASE @column WHEN 'column1' THEN column1 END,
CASE @column WHEN 'column2' THEN column2 END,
CASE @column WHEN 'column3' THEN column3 END

But using dynamic SQL may be best as appropriate indexes on the ordering columns can be used more efficiently.

--
Plamen Ratchev
http://www.SQLStudio.com

Reply With Quote
  #4  
Old   
m
 
Posts: n/a

Default Re: multiple column sort - 01-08-2010 , 06:16 AM



Thank you.

"Mark D Powell" <Mark.Powell2 (AT) hp (DOT) com> wrote

On Jan 7, 7:52 am, "m" <mirosla... (AT) gmail (DOT) com> wrote:
Quote:
Hello,

I need to provide sorting ability across multiple column.

Data which would be sorted are retrieved with stored procedure.

I have two solutions:

1. if statement
depending of sort type I must create diferent select statement

in case many columns there would be so many variations (also, there must
be
considered sort direction ASC or DESC what increase variations double)

2. dynamic sql

Is there any additional way, which one is better?

Please advice,

Best regards
Generally speaking static SQL is preferable to dynamic SQL. However,
there are times when dynamic SQL comes in handy. If you have only a
couple of possible orderings I would go with static SQL. If you have
numerous possible desired orderings with varying order by column lists
then I would build the order by clause in the procedure logic.

Just make sure you are not trying to do too much in a single procedure
and that all the SQL's in question are really just variations of one
basic query rather than trying to code logic to accomodate several
different basic queries that just happen to use the same source
tables.

HTH -- Mark D Powell --

Reply With Quote
  #5  
Old   
m
 
Posts: n/a

Default Re: multiple column sort - 01-08-2010 , 06:17 AM



Thank you.

"Plamen Ratchev" <Plamen (AT) SQLStudio (DOT) com> wrote

Quote:
You can also use CASE expressions. For example, if the data type of the
columns is the same:

ORDER BY CASE @column
WHEN 'column1' THEN column1
WHEN 'column2' THEN column2
WHEN 'column3' THEN column3
END

If the data types are different then you have to use multiple CASE
expressions:

ORDER BY CASE @column WHEN 'column1' THEN column1 END,
CASE @column WHEN 'column2' THEN column2 END,
CASE @column WHEN 'column3' THEN column3 END

But using dynamic SQL may be best as appropriate indexes on the ordering
columns can be used more efficiently.

--
Plamen Ratchev
http://www.SQLStudio.com

Reply With Quote
Reply




Thread Tools
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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.