self-recursive query...

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

As most of you might know that Nothwind - employee table contains a
self-recursive field - ReportTo
John ReportTo Peter
Peter Report To Mary
Mary Report To Sussan

Can anyone tell me how am i able to query all the informtion showing
something like
the above...
Thanks, it's been drving me crazy...
Ed
 
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Well, in the Northwind db you could use a query like this:

SELECT Employees.EmployeeID, Employees.LastName,
Boss.EmployeeID, Boss.LastName
FROM Employees INNER JOIN Employees As Boss
ON Employees.ReportsTo = Boss.EmployeeID

If you want to see a hierarchical listing like this:

Susan +
+ Mary +
+ Peter +
+ John

There are 2 ways I can think of to do this:

1) Create a VBA routine that loops thru the recordset linking employees.
There are a lot of examples in the Google Groups archives. Try news
group comp.databases.ms-access.

2) Use the Joe Celko book _SQL for Samrties_, Morgan Kaufmann
Publishers, 2000. Look at the chapters on Adjacency List Model of Trees
In SQL (chp 28) and Nested Set Model of Trees in SQL (chp 29).

There are discussions of adjacency lists and nested sets in
comp.databases.ms-access Google group archive and all over the web.
Google for "adjacency lists" and "nested set tree."

--
MGFoster:::mgf00 <at> earthlink <decimal-point> net
Oakland, CA (USA)

-----BEGIN PGP SIGNATURE-----
Version: PGP for Personal Privacy 5.0
Charset: noconv

iQA/AwUBQRU8U4echKqOuFEgEQIMqgCfdpW0UtYVreJYyY2bJqzABXMRpIUAoJWr
KnKtZSdMbQwwfuUV0P4G1YTd
=XOH3
-----END PGP SIGNATURE-----
 
Back
Top