sql server - SQL Query with conditional order by for a specific condition -


this question has answer here:

here how data looks like

companyname   |    companycode -----------        -----------                    b190   arbot              b213  infra              a946 michalestest       b207 mycompany          alerf mycompany          lerf snelsy             a857 snelsy             a954 

i want sort list companyname = 'mycompany' first in list associated companycode sorted in asc.

and other items listed after 'mycompany' companyname sorted in asc. entries null companyname can @ end of list.

i’ve tried far below query didn’t expected result. following link here

select [companyname], [companycode]  [dbo].cond_orderby_test     order case companyname when 'mycompany' companyname     else companyname end, companycode 

i expect final result set below

companyname   |    companycode -----------        ----------- mycompany           alerf      mycompany           lerf       arbot               b213   infra               a946    michalestest        b207  snelsy              a857 snelsy              a954                      b190 

i’m sure there should elegant way i'm not getting it. appreciated.

when face kind of situation, this:

select [companyname]      , [companycode]      , sortorder = case when companyname null 3                         when companyname = 'mycompany' 1                         else 2                     end   [dbo].cond_orderby_test  order 3 asc, companyname asc 

as ordering same field, sql server not guess order want...

try :)


Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -