Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to retrieve employee id of managers

Posted on 2014-11-03
10
Medium Priority
?
173 Views
Last Modified: 2014-11-16
Hello,
I'm working on an application that I inherited.  There is an employee table that lists all the employees as well as the managers.  The problem is that the table uses a the last name of the manager as a key field rather than a manager id field.  I'm added a mgr_id field.  I am now trying to populate the mgr_id field by executing a query that returns the employee id of each manager and using that employee id field named CUID to populate the mgr_id field that I added.  I started off by writing a sql query that returns that information to verify that I was selecting the correct value.  However, it is still retreiving only the cuid of the employee and not of the manager.

SELECT Employee.CUID, Employee.Mgr_LName
FROM Employee
WHERE Employee.[Mgr_LName] In (SELECT LName FROM Employee);
0
Comment
Question by:chtullu135
[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
  • 5
  • 4
10 Comments
 
LVL 5

Accepted Solution

by:
ReneD100 earned 2000 total points
ID: 40419726
SELECT E.CUID, E.Mgr_LName, M.CUID
FROM Employee E, Employee M
WHERE E.Mgr_LName=M.LName

Open in new window

The E.CUID is the Employee ID, the M.CUID is the Managers' CUID
0
 

Author Closing Comment

by:chtullu135
ID: 40419822
Thanks Rene.  I was starting to write a recursive query and you saved me quite a bit of time.
0
 
LVL 39

Expert Comment

by:PatHartman
ID: 40419827
You need to specify a join type or you will end up with a non-updatable query.

SELECT E.CUID, E.Mgr_LName, M.CUID
FROM Employee E Inner Join Employee M On E.Mgr_LName=M.LName

Of course, this update will be problematic if you have multiple employees/managers with the same last name so I would check that first by creating a duplicates query to look for employee records that duplicate last names.

Select LName, Count(*) As RecCount
From Employee
Group By LName
Having Count(*) > 1;
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:chtullu135
ID: 40420080
Thanks Pat
0
 
LVL 39

Expert Comment

by:PatHartman
ID: 40420083
I guess you didn't need an updateable query.
0
 

Author Comment

by:chtullu135
ID: 40422497
Hello Pat,

In this case, the solution posted by Rene works.  However, I'm using your solution for another problem.  To be honest, I much prefer the join syntax as it more clearly delineates the where condition.
0
 
LVL 39

Expert Comment

by:PatHartman
ID: 40422822
Access treats the two queries very differently.  Rene's query produces a Cartesian Product where every row from table 1 is matched against every row from table 2 and the recordset is then whittled down by the criteria.  That is why the query is not updateable.  you can also run into memory issues if the two tables have more than a small number of rows.  1,000 rows x 10,000 rows = 10 million rows!!! before the criteria is applied.  So, unless you really want a Cartesian Product, I would never use Rene's query.  Since every row is joined to every other row, no indexes or other shortcuts can be employed by the query engine.
0
 

Author Comment

by:chtullu135
ID: 40433080
In my case, the solution posted by renee works since I didn't need an updatable query.  But I do see your point and will use that approach.  However since Renee first, provided me with the solution that worked for the problem I was working I gave Renee the points, before I saw your answer.  I'm not sure if I can reasign points at this stage.
0
 
LVL 39

Expert Comment

by:PatHartman
ID: 40433343
You could but there is no need to do so.  I just wanted you to understand why you shouldn't use Cartesian Products lightly.
0
 

Author Comment

by:chtullu135
ID: 40445913
Thanks Pat.  I appreciate your clarification.
0

Featured Post

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

In earlier versions of Windows (XP and before), you could drag a database to the taskbar, where it would appear as a taskbar icon to open that database.  This article shows how to recreate this functionality in Windows 7 through 10.
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

715 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