Query on Parent-Child based table (1 Viewer)

Massoud

Registered User.
Local time
Today, 06:18
Joined
Jul 11, 2002
Messages
37
Hi
I have a parent-child base table (accounts) with the following fields:
- id
- parentid
- name

every record's parentid is either 0 ( which means it is in the most upper level) or another record's id (which means it is the other record's child)
different levels of records are being used. I mean some accounts are used in the most 1st level with no childs, some in the second level, ....

I need a query to list the accounts which are not parent to any othe records, regardless of what level they are defined.

Thanks again for the help.
 

ByteMyzer

AWF VIP
Local time
Yesterday, 22:18
Joined
May 3, 2004
Messages
1,409
Try something like:
Code:
SELECT A1.*
FROM accounts AS A1 LEFT JOIN accounts AS A2 ON A1.id = A2.parentid
WHERE A2.parentid IS NULL;
 

KenHigg

Registered User
Local time
Today, 01:18
Joined
Jun 9, 2004
Messages
13,327
I think you'll need to queries to do this;

Query one is a simple totals query where you group parentid's.

In the second query bring in the original table and the first query and do a link on table!id to query1!parentid and have the second query only return rows where the parentid field from the 1st query is null...

:)
ken
 

Massoud

Registered User.
Local time
Today, 06:18
Joined
Jul 11, 2002
Messages
37
Thank you very much ByteMyzer . Fast and exact.
 

Users who are viewing this thread

Top Bottom