Solved

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

Posted on 2012-04-03
2
170 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
2 Comments
 
LVL 23

Accepted Solution

by:
wdosanjos earned 500 total points
Comment Utility
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
Comment Utility
Just a small change:
   join Departments d2 on d2.TreeCode Like '%*' + d1.TreeCode + '*%'

Thank you!
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

As they say in love and is true in SQL: you can sum some Data some of the time, but you can't always aggregate all Data all the time! Introduction: By the end of this Article it is my intention to bring the meaning and value of the above quote to…
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now