?
Solved

List of all employees where I'm the manager of their department or some department above

Posted on 2012-04-03
2
Medium Priority
?
177 Views
Last Modified: 2012-04-04
Problem:
I want a list of all employees where I'm the manager of their department or some department above (in MS SQL)
I can be a manager of one of more departments

Table 1: EMPLOYEES: columns employeecode, name, departmentcode
Table 2: MANAGERS:  columns employeecode, departmentcode (employeecode is manager of departmentcode)
Table 3: DEPARTMENTS: columns departmentcode, treecode
      treecode is a parent-child structure for each department, like
                 *TOP*parent*parent*parent*thisDepartment
      example: department 0310 has treecode *TOP*B05*B0003*0310*
      (0310's parent is B0003, B0003's parent is B05 and so on)

Example: if I am the manager of departments B05 and C01, I want all employees in any department having treecode LIKE '%*B05*%' or '%*C01*%' (MS SQL)
0
Comment
Question by:fmsol
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 23

Accepted Solution

by:
wdosanjos earned 2000 total points
ID: 37802748
Please try the following:
Select * 
  From Employees
Where DepartmentCode in
    (Select d2.DepartmentCode From Managers m
        join Departments d1 on d1.DepartmentCode = m.DepartmentCode
        join Departments d2 on d2.TreeCode Like d1.TreeCode + '%'
        where m.EmployeeCode = <MyEmployeeCode>)

Open in new window


I hope this helps.
0
 

Author Closing Comment

by:fmsol
ID: 37805009
Just a small change:
   join Departments d2 on d2.TreeCode Like '%*' + d1.TreeCode + '*%'

Thank you!
0

Featured Post

Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In database programming, custom sort order seems to be necessary quite often, at least in my experience and time here at EE. Within the realm of custom sorting is the sorting of numbers and text independently (i.e., treating the numbers as number…
Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question