Solved

Navigating a multi level pigs ear of specified depth

Posted on 2008-06-19
9
433 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

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

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!

Question has a verified solution.

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

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
It’s been over a month into 2017, and there is already a sophisticated Gmail phishing email making it rounds. New techniques and tactics, have given hackers a way to authentically impersonate your contacts.How it Works The attack works by targeti…
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

776 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