Solved

Navigating a multi level pigs ear of specified depth

Posted on 2008-06-19
9
428 Views
Last Modified: 2010-04-21
Hi guys.

We are building a database of Email addresses which contains several layers of groups.

Emails sent to a group are forwarded to the lower level  groups and so on to the final actual recipients.

We can build a table that records this structure either with a pigs ear or with a link table (if some people are included in more than one group)

Now..... We need a query (SQL Server or Access 2003) that will allow us to report on the structure from any level.  We need to be able to enter the code for a specific group any where in the heirachy of groups and then be presented with a list of all the bottom end recipients without having the intermediate levels appearing in the list.

So our issues are:
- How to write a query that drills down until it reaches the bottom without knowing how many layers it has to handle.

- How to list all of the bottom level entities without listing the intermediate levels.

Gulp.

Hugh


0
Comment
Question by:CABHugh
  • 5
  • 3
9 Comments
 

Author Comment

by:CABHugh
ID: 21822124
The heading should say "Unspecified Depth" sorry!
0
 
LVL 60

Expert Comment

by:chapmandew
ID: 21822152
Do you already have the table built?  If so, can you post the structure?  Are you using SQL 2005 or 2000?
0
 
LVL 119

Expert Comment

by:Rey Obrero
ID: 21822170
see if the example here helps

http://allenbrowne.com/ser-06.html
0
 

Author Comment

by:CABHugh
ID: 21822493
Chapmandew,

We are using Access 2003 front-ending SQL Server 2005.  The query(s), views, stored procedures, whatever can be written in either as appropriate.

There is not really a structure, just two fields one as Primary key and one as Foreign key plus one or two fields with referencing information like names, EMail addresses, etc.

The groups are likely to be ALL, Executive Level, Management Level, Regional Manager Level, Branch Level, Sub Branch Level and Recipients.  There will be several records at each level each with a heirachy below them.

We anticipate that some people will be in more than one grouping, possibly also at more than one level.  Which is why we are more likely to use a link table to resolve the many-to-many situations that cannot be handled by a normal pigs ear.

Regards

Hugh
0
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 

Author Comment

by:CABHugh
ID: 21822958
Capricorn1,

It is all about 'sex' these days....

I checked out your example.  It is similar to the structure we need.  However, as it says in the article, you need to add a layer for each 'generation' you want in the family tree.

We are trying to build some logic that does not know how many layers there are.  We want it to drill down until it hits the bottom layer.  This will probably be identified when there are no more "forwarding" links in the result set.

Regards

Hugh
0
 
LVL 60

Accepted Solution

by:
chapmandew earned 500 total points
ID: 21822992
YOu could use a common table expresion in 2005...something like this:

USE AdventureWorks;
GO
WITH DirectReports(ManagerID, EmployeeID, EmployeeLevel) AS
(
    SELECT ManagerID, EmployeeID, 0 AS EmployeeLevel
    FROM HumanResources.Employee
    WHERE ManagerID IS NULL
    UNION ALL
    SELECT e.ManagerID, e.EmployeeID, EmployeeLevel + 1
    FROM HumanResources.Employee e
        INNER JOIN DirectReports d
        ON e.ManagerID = d.EmployeeID
)
SELECT ManagerID, EmployeeID, EmployeeLevel
FROM DirectReports ;
GO


0
 

Author Comment

by:CABHugh
ID: 21823231
Chapmandew,

I am giving serious thought to your example from AdventureWorks.

Currently we do not have a "level" field, but might be way forward.

I could see that we could select the highest "level" and then use this in the query to idntify the bottom.

I have just been populating a prototype of the two tables, so I the logic and see where we get to.

Regards

Hugh
0
 
LVL 60

Expert Comment

by:chapmandew
ID: 21823292
Sounds good...just let me know if you have questions.
0
 

Author Closing Comment

by:CABHugh
ID: 31468769
With a little modification, which actually removed some code, this solution worked a treat.  I would never have worked it out on my own.

It spirals down to as many levels as the Users create in the data.

I am very grateful to chapmandew
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
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…
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

760 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

22 Experts available now in Live!

Get 1:1 Help Now