 |
| Access Discussion of Microsoft Access database design and programming. See also the forums for Access ASP and Access VBA. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the Access section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

August 3rd, 2005, 07:59 AM
|
|
Authorized User
|
|
Join Date: Jun 2003
Posts: 59
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Grouping problem
Hi all.
I have the following problem.
I have a table of contacts with a PK of contactID and various fields including ManagerID.
Some of these contacts are managers in which case this field is 0. otherwise it contains the ContactID of the manager.
What I want to do is query/report so that the manager is shown along with their subordinates.
Does this make sense and can anyone help?
Andy G
|
|

August 3rd, 2005, 08:36 AM
|
|
Friend of Wrox
|
|
Join Date: Sep 2003
Posts: 155
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Try this:
SELECT tblMgt.ContactName AS Manager, tblContacts.ContactName AS SubOrdinate
FROM tblContacts
RIGHT JOIN tblContacts AS tblMgt ON tblContacts.ManagerID = tblMgt.ContactID
WHERE tblMgt.ManagerID = 0
This gives you a list of all of the Managers and any subordinates they have.
|
|

August 3rd, 2005, 12:50 PM
|
|
Authorized User
|
|
Join Date: Jun 2003
Posts: 59
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Hi
Thanks for yur help but it doesn't solve the problem.
I only have 1 table called tblContacts and if the contact is not a manager ie works for a manager the manager id field is set to the contactID of the manager. If you are a manager then your manager ID is 0.
Andy
|
|

August 3rd, 2005, 01:14 PM
|
|
Friend of Wrox
|
|
Join Date: Oct 2004
Posts: 564
Thanks: 0
Thanked 4 Times in 4 Posts
|
|
This would probably work better with multiple tables and a different structure, but for now, one quick and dirty solution would be:
SELECT tblContacts.Name AS Manager, tblContacts_1.Name AS Subordinate
FROM tblContacts AS tblContacts_1 INNER JOIN tblContacts ON tblContacts_1.ManagerID=tblContacts.ContactID;
That should give you two columns, the first with the manager name and the second with the subordinate. Mine came up in order, but if not, you may have to order the table by the ContactID field on tblContacts.
Hope that helps
Mike
Mike
EchoVue.com
|
|

August 3rd, 2005, 03:22 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 1,151
Thanks: 2
Thanked 14 Times in 14 Posts
|
|
Odd structure. You probably want TWO tables. One of managers with their IDs and one of just employees with the manager ID who they work for. Having all in one table is odd because if each manager gets ID of zero, then how do you relate THAT manager to his/her subordinate? All the other managers are zero, too!
Greg Serrano
Michigan Dept. of Environmental Quality, Air Quality Division
|
|

August 3rd, 2005, 05:56 PM
|
|
Authorized User
|
|
Join Date: Jun 2003
Posts: 59
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Thanks Guys
I've changed the data structure, bit of tidying up the old data, all well, hopefully will make everything easier.
Andy
|
|

August 4th, 2005, 01:46 AM
|
|
Friend of Wrox
|
|
Join Date: Sep 2003
Posts: 155
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
didnt bother to try the code then? It does only use one table, since that is all you specified... tblContacts self joins tblContacts renamed as tblMgt purely to make it look pretty and to help you understand it in future.
If you check out Echovue's code it is pretty much the same as mine but the second table is renamed tblContacts_1 (default for access)... which could (in my experience) become ambiguous, so tblMgt may be more suitable. Echovue's code does differ slightly from mine in so far as mine is using a different kind of join... mine will return all employees with any subordinates they have, Echovue's should return only those employees with subordinates.
|
|

August 4th, 2005, 01:59 AM
|
|
Authorized User
|
|
Join Date: Jun 2003
Posts: 59
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Hi
Yes I did try the code, both examples, and realised that you had aliased the table name only after trying to get it to work.
But both of you said that the structure was wrong, so thought it best to change it to save any problems later.
Thanks again.
|
|

August 4th, 2005, 06:27 AM
|
|
Friend of Wrox
|
|
Join Date: Sep 2003
Posts: 155
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
sorry - bit tetchy this morning!
|
|

August 4th, 2005, 10:36 AM
|
|
Authorized User
|
|
Join Date: Jun 2004
Posts: 51
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
leehambly's code was good stuff. I simulated it, and it worked nicely. I would opt for keeping the original structure and using his code. Sounds to me like you will be complicating your life by splitting stuff up. Besides, I've seen "Manager/Employee" examples in Wrox books that use your original structure.
K.I.S.S.
"Life is a Database"
|
|
 |