• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 458
  • Last Modified:

Navigating a multi level pigs ear of specified depth

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
CABHugh
Asked:
CABHugh
  • 5
  • 3
1 Solution
 
CABHughAuthor Commented:
The heading should say "Unspecified Depth" sorry!
0
 
chapmandewCommented:
Do you already have the table built?  If so, can you post the structure?  Are you using SQL 2005 or 2000?
0
 
Rey Obrero (Capricorn1)Commented:
see if the example here helps

http://allenbrowne.com/ser-06.html
0
Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

 
CABHughAuthor Commented:
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
 
CABHughAuthor Commented:
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
 
chapmandewCommented:
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
 
CABHughAuthor Commented:
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
 
chapmandewCommented:
Sounds good...just let me know if you have questions.
0
 
CABHughAuthor Commented:
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

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now