Solved

Navigating a multi level pigs ear of specified depth

Posted on 2008-06-19
9
431 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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Overview: This article:       (a) explains one principle method to cross-reference invoice items in Quickbooks®       (b) explores the reasons one might need to cross-reference invoice items       (c) provides a sample process for creating a M…
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.
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …
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…

930 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