Solved

Navigating a multi level pigs ear of specified depth

Posted on 2008-06-19
9
436 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 120

Expert Comment

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

http://allenbrowne.com/ser-06.html
0
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 

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
 

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

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)

Question has a verified solution.

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

Suggested Solutions

A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

820 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