I have a table Employees with Managers and Reps in it. I would want a stored procedure that can get Manager 1 (alphabetic order) and then all Reps under that Manager1 (in alphabetic order) and then Manager 2 and all Reps under the Manager2.
There is a Column ‘Manager’ for all Employees with EmployeeID of Manager in that column for each Rep and null for Managers. And there is also a column ismanager which will be 1 for Managers and 0 for reps.
I tried doing it but the logic is somewhere incorrect. this is in SQL Server 2005.
Thank you in advance!!
Select * from Employees
groupby IsManager, EmployeeID
What you want to do is order by the manager id and then order by ismanager desc.
Since the manager does not have his own id in the manager column you have use a case statement to fix that. Here are two examples of how to do it in SQL:
or
nb — not tested might have typos.