The actual rows have further more levels. The key importance here is perfomance.
Can you put this all into a database? Once there, gets a lot easier (and faster if indexed/structured properly).
users user_id first_name last_name user_age -- !!!! really should be stored as birthdate or birthyear parent_user_rlt parent_id child_id SELECT grand.user_id as grandparent_id, parent.user_id as parent_id, child.user_id as child_id, child.first_name as child_first, child_last_name as child_last FROM users as grand LEFT JOIN parent_user_rlt as g2p ON g2p.parent_id = grand.user_id LEFT JOIN users as parent ON parent.user_id = g2p.child_id LEFT JOIN parent_user_rlt as p2c ON p2c.parent_id = parent.user_id LEFT JOIN users as child ON child.user_id = g2p.child_id ;
If you use that query to construct a temp table called "grandchildren", then your answers become things like:
SELECT distinct parent_id FROM grandchildren; SELECT grandparent_id, count(distinct parent_id) FROM grandchildren +GROUP BY grandparent_id; SELECT grandparent_id, parent_id, count(*) as num_kids FROM grandchi +ldren GROUP BY grandparent_id, parent_id; SELECT child_id, child_first, child_last FROM grandchildren;

In reply to Re: Hash Ref: Need to find out how many children I have :-) by davidrw
in thread Hash Ref: Need to find out how many children I have :-) by rupesh

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.