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 |
chandran.nr
Starting Member
4 Posts |
Posted - 2014-01-20 : 00:58:39
|
HiI have table called 'UserDetails'. If I execute below select query it should display in order of uno= 7,13,5 but i get in order ofuno=5,7,13.Please help me to get in order of uno= 7,13,5Select EmailAddress,EmployeeName,UNo, MobileNumber from UserDetails where (UNo=7 or UNo=13 or UNo=5 ) group by uno,emailaddress,employeename,uno,mobilenumberResult i am getting asEmailAddress EmployeeName UNo MobileNumber -----------------------------------------------------------aaa@xxx.com ravi 5 8989898989bbb@xxx.com ramesh 7 9898989898ariv@gmail.com arivu 13 8989898989Thanks and RegardsChandran |
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2014-01-20 : 02:41:48
|
order byCASEWHEN uno = 7 THEN 1WHEN uno = 13 THEN 2WHEN uno = 5 THEN 3END Too old to Rock'n'Roll too young to die. |
 |
|
chandran.nr
Starting Member
4 Posts |
Posted - 2014-01-20 : 07:43:17
|
Hi if i get query like belowSelect EmailAddress,EmployeeName,UNo, MobileNumber from UserDetails where (UNo=13 or UNo=5 or UNo=7 ) group by uno,emailaddress,employeename,uno,mobilenumberfor the above query it will beorder byCASEWHEN uno = 13 THEN 1WHEN uno = 5 THEN 2WHEN uno = 7 THEN 3ENDI want to get dynamically.every query will be having different valuesPlease help me to get order by valuesThankschandran |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2014-01-20 : 08:07:12
|
quote: Originally posted by chandran.nr Hi if i get query like belowSelect EmailAddress,EmployeeName,UNo, MobileNumber from UserDetails where (UNo=13 or UNo=5 or UNo=7 ) group by uno,emailaddress,employeename,uno,mobilenumberfor the above query it will beorder byCASEWHEN uno = 13 THEN 1WHEN uno = 5 THEN 2WHEN uno = 7 THEN 3ENDI want to get dynamically.every query will be having different valuesPlease help me to get order by valuesThankschandran
then only option is to have a table with all your uno values and corresponding integer value to designate the sequence number while sorting. Once the table is created,in above query just add a join to table on uno field and add sequencenumber in ORDER BY to get it in your required order.Select EmailAddress,EmployeeName,UNo, MobileNumberfrom UserDetails uinner join mappingtable ton t.uno = u.unowhere (u.UNo=13 or u.UNo=5 or u.UNo=7 ) group by uno,emailaddress,employeename,uno,mobilenumber, sequencenumber order by sequencenumber ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
 |
|
|
|
|
|
|