To get row from select query in order

  • Posted - 01/20/2014 : 00:58:39 Show Profile Email Poster Edit Topic Reply with Quote Delete Topic Nuke Spam!

    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

  • Hi.

    Maybe like this:

    with t as(

    select 'aaa@xxx.com' EmailAddress, 'ravi' EmployeeName, 5 UNo, 8989898989 MobileNumber

    union

    select 'bbb@xxx.com', 'ramesh', 7, 9898989898

    union

    select 'ariv@gmail.com', 'arivu', 13, 8989898989)

    select case when Uno=7 then 1 when Uno=13 then 2 when UNo=5 then 3 end as ord, EmailAddress, EmployeeName, UNo, MobileNumber

    from t

    order by ord

  • ramachandran narayanan (1/19/2014)


    ...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....

    What are the business rules for the display order?

    “Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

    For fast, accurate and documented assistance in answering your questions, please read this article.
    Understanding and using APPLY, (I) and (II) Paul White
    Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden

  • o.fimin (1/19/2014)


    Hi.

    Maybe like this:

    with t as(

    select 'aaa@xxx.com' EmailAddress, 'ravi' EmployeeName, 5 UNo, 8989898989 MobileNumber

    union

    select 'bbb@xxx.com', 'ramesh', 7, 9898989898

    union

    select 'ariv@gmail.com', 'arivu', 13, 8989898989)

    select case when Uno=7 then 1 when Uno=13 then 2 when UNo=5 then 3 end as ord, EmailAddress, EmployeeName, UNo, MobileNumber

    from t

    order by ord

    Or just this, if you don't need an [ord] column in the output:

    with t as(

    select 'aaa@xxx.com' EmailAddress, 'ravi' EmployeeName, 5 UNo, 8989898989 MobileNumber

    union

    select 'bbb@xxx.com', 'ramesh', 7, 9898989898

    union

    select 'ariv@gmail.com', 'arivu', 13, 8989898989)

    select EmailAddress, EmployeeName, UNo, MobileNumber

    from t

    order by case when Uno=7 then 1 when Uno=13 then 2 when UNo=5 then 3 end

    Jason Wolfkill

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply