Wrox Programmer Forums
|
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
 
Old August 3rd, 2005, 07:59 AM
Authorized User
 
Join Date: Jun 2003
Posts: 59
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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
 
Old August 3rd, 2005, 08:36 AM
Friend of Wrox
 
Join Date: Sep 2003
Posts: 155
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to leehambly
Default

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.
 
Old August 3rd, 2005, 12:50 PM
Authorized User
 
Join Date: Jun 2003
Posts: 59
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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
 
Old August 3rd, 2005, 01:14 PM
Friend of Wrox
 
Join Date: Oct 2004
Posts: 564
Thanks: 0
Thanked 4 Times in 4 Posts
Default

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
 
Old August 3rd, 2005, 03:22 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,151
Thanks: 2
Thanked 14 Times in 14 Posts
Send a message via ICQ to SerranoG Send a message via AIM to SerranoG
Default

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
 
Old August 3rd, 2005, 05:56 PM
Authorized User
 
Join Date: Jun 2003
Posts: 59
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks Guys

I've changed the data structure, bit of tidying up the old data, all well, hopefully will make everything easier.

Andy
 
Old August 4th, 2005, 01:46 AM
Friend of Wrox
 
Join Date: Sep 2003
Posts: 155
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to leehambly
Default

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.
 
Old August 4th, 2005, 01:59 AM
Authorized User
 
Join Date: Jun 2003
Posts: 59
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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.
 
Old August 4th, 2005, 06:27 AM
Friend of Wrox
 
Join Date: Sep 2003
Posts: 155
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to leehambly
Default

sorry - bit tetchy this morning!
 
Old August 4th, 2005, 10:36 AM
Authorized User
 
Join Date: Jun 2004
Posts: 51
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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"





Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem of grouping in XSLT LeoMathew XSLT 2 September 11th, 2008 09:38 AM
grouping problem at xslt1.0 joshuaa XSLT 4 July 15th, 2008 08:08 AM
XML Grouping Problem andymidd XSLT 2 July 10th, 2007 02:48 AM
Grouping problem bonekrusher XSLT 3 April 7th, 2007 05:11 PM
grouping and expressions problem parityah BOOK: Professional SQL Server Reporting Services ISBN: 0-7645-6878-7 1 May 2nd, 2005 06:43 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.