|
|
|
SSC-Enthusiastic
      
Group: General Forum Members
Last Login: Monday, November 08, 2010 10:45 PM
Points: 122,
Visits: 70
|
|
Hi all
one more tricky query I have encountered..(@ an interview)..
we have employee table with manager name and salary..
now the question is..write a query to display employee records order by salary and the manager record should be the first record in the result...
Thanks ,
Shekhar
|
|
|
|
|
SSChampion
        
Group: General Forum Members
Last Login: Today @ 10:50 AM
Points: 11,605,
Visits: 27,647
|
|
select 1 as myorderBy,name,salary from sometable where manager='Y' UNION ALL select 2 as myorderBy,name,salary from sometable where manager='N' ORDER BY myorderBy,salary
Lowell
--There is no spoon, and there's no default ORDER BY in sql server either. Actually, Common Sense is so rare, it should be considered a Superpower. --my son
|
|
|
|
|
SSC-Enthusiastic
      
Group: General Forum Members
Last Login: Monday, November 08, 2010 10:45 PM
Points: 122,
Visits: 70
|
|
hey Lowell
that was awe some... thank u very much.. :)
Thanks ,
Shekhar
|
|
|
|
|
SSChampion
        
Group: General Forum Members
Last Login: Today @ 10:50 AM
Points: 11,605,
Visits: 27,647
|
|
another way without the UNION:
select CASE WHEN manager='Y' THEN 1 ELSE 2 END as myorderBy,name,salary from sometable ORDER BY myorderBy,salary
Lowell
--There is no spoon, and there's no default ORDER BY in sql server either. Actually, Common Sense is so rare, it should be considered a Superpower. --my son
|
|
|
|
|
SSC-Enthusiastic
      
Group: General Forum Members
Last Login: Monday, November 08, 2010 10:45 PM
Points: 122,
Visits: 70
|
|
Thank you very much lowell... :)
Thanks ,
Shekhar
|
|
|
|