SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Help needed - sorting
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

groston
Yak Posting Veteran

61 Posts

Posted - 03/23/2013 :  00:47:27  Show Profile  Reply with Quote
I have a SQL table whose data looks like this:

Parm1 Parm2
V1 V11
V1 V12
V2 V21
V2 V22
V3 V31
V3 V32

I would like to sort the table, based on a parameter, in the following manner. If the parameter is V1, the table will appear as above. If the parameter is V2, the table will be sorted like this:

Parm1 Parm2
V2 V21
V2 V22
V1 V11
V1 V12
V3 V31
V3 V32

And, if the parameter is V3, the table will be sorted like this:

Parm1 Parm2
V3 V31
V3 V32
V1 V11
V1 V12
V2 V21
V2 V22

I have figured out how to make the rows associated with the parameter appear first:

SELECT *
FROM (
SELECT 1 AS sv, * FROM myTable WHERE Parm1 = @Parm1value
UNION
SELECT 2 AS sv, * FROM myTable WHERE Parm1 <> @Parm1value
) dt
ORDER BY sv

but I cannot then sort the other rows. Can you please offer some guidance.

russell
Pyro-ma-ni-yak

USA
5072 Posts

Posted - 03/23/2013 :  01:27:04  Show Profile  Visit russell's Homepage  Reply with Quote
ORDER BY sv, CONVERT(INT, REPLACE(Parm1, 'V', ''))
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

Sweden
30265 Posts

Posted - 03/23/2013 :  04:35:55  Show Profile  Visit SwePeso's Homepage  Reply with Quote
ORDER BY CASE WHEN Parm1 = @UserParameter THEN 0 ELSE 1 END, Parm1, Parm2



N 56°04'39.26"
E 12°55'05.63"

Edited by - SwePeso on 03/23/2013 04:36:21
Go to Top of Page

nurunseo
Starting Member

Bangladesh
6 Posts

Posted - 03/24/2013 :  02:07:05  Show Profile  Reply with Quote
Hi guys !
I am also new in this forum.
I wanna know in about of this forum...............

http://paneuromix.com/en
http://paneuromix.com/nootropics.html
Go to Top of Page

groston
Yak Posting Veteran

61 Posts

Posted - 03/24/2013 :  21:27:01  Show Profile  Reply with Quote
russell,

Thank you for replying. You took my example too literally - my bad for not being clearer.

SwePeso,

You suggestion works perfectly. Thank you for pointing out the ORDER BY CASE construct to me.
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.03 seconds. Powered By: Snitz Forums 2000