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.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 to get row from select query in order

Author  Topic 

chandran.nr
Starting Member

4 Posts

Posted - 2014-01-20 : 00:58:39
Hi

I 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 of
uno=5,7,13.
Please help me to get in order of uno= 7,13,5


Select EmailAddress,EmployeeName,UNo, MobileNumber
from UserDetails where (UNo=7 or UNo=13 or UNo=5 ) group by uno,emailaddress,employeename,uno,mobilenumber

Result i am getting as
EmailAddress EmployeeName UNo MobileNumber
-----------------------------------------------------------
aaa@xxx.com ravi 5 8989898989
bbb@xxx.com ramesh 7 9898989898
ariv@gmail.com arivu 13 8989898989

Thanks and Regards
Chandran

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2014-01-20 : 02:41:48
order by
CASE
WHEN uno = 7 THEN 1
WHEN uno = 13 THEN 2
WHEN uno = 5 THEN 3
END



Too old to Rock'n'Roll too young to die.
Go to Top of Page

chandran.nr
Starting Member

4 Posts

Posted - 2014-01-20 : 07:43:17
Hi

if i get query like below


Select EmailAddress,EmployeeName,UNo, MobileNumber
from UserDetails where (UNo=13 or UNo=5 or UNo=7 ) group by uno,emailaddress,employeename,uno,mobilenumber

for the above query it will be

order by
CASE
WHEN uno = 13 THEN 1
WHEN uno = 5 THEN 2
WHEN uno = 7 THEN 3
END

I want to get dynamically.every query will be having different values

Please help me to get order by values

Thanks
chandran



Go to Top of Page

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 below


Select EmailAddress,EmployeeName,UNo, MobileNumber
from UserDetails where (UNo=13 or UNo=5 or UNo=7 ) group by uno,emailaddress,employeename,uno,mobilenumber

for the above query it will be

order by
CASE
WHEN uno = 13 THEN 1
WHEN uno = 5 THEN 2
WHEN uno = 7 THEN 3
END

I want to get dynamically.every query will be having different values

Please help me to get order by values

Thanks
chandran






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, MobileNumber
from UserDetails u
inner join mappingtable t
on t.uno = u.uno
where (u.UNo=13 or u.UNo=5 or u.UNo=7 )
group by uno,emailaddress,employeename,uno,mobilenumber, sequencenumber
order by sequencenumber


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
   

- Advertisement -