I think the problem is mostly due to how you're storing the results as you fetch them from the database. If you use a hash keyed on incident.id and push the results for each action in, then the data will already be in the format you want to print it in. Here's an example:
#!/usr/local/bin/perl use strict; use warnings; my @query = ( { i_id => 1, summary => 's 1', a_id => 1, description => 'a 1', }, { i_id => 1, summary => 's 1', a_id => 2, description => 'a 2', }, { i_id => 1, summary => 's 1', a_id => 3, description => 'a 3', }, { i_id => 2, summary => 's 2', a_id => 4, description => 'a 4', }, { i_id => 2, summary => 's 2', a_id => 5, description => 'a 5', }, ); my %results; # analagous to your while ( my $href = $sth->fetchrow_hashref() ){ while( my $href = shift @query ){ # treat each entry in %results as a hash ref unless( $results{ $href->{ i_id } } ){ # save the summary information in the summary key $results{ $href->{ i_id } }->{ summary } = $href->{ summary }; } # push a hash ref of action information into { actions } push @{ $results{ $href->{ i_id } }->{ actions } }, { a_id => $href->{ a_id }, desc => $href->{ description }, }; } # now it's grouped by incident, and you can print it: foreach my $i_id ( keys %results ){ print "Incident: $i_id - $results{ $i_id }->{ summary }\n"; foreach my $action ( @{ $results{ $i_id }->{ actions } } ){ print " $action->{ a_id }: $action->{ desc }\n"; } }
note that the last foreach, where the printing happens, is pretty straightforward - loop over the keys (one for each incident), print the incident info, and then loop through the actions for each. The output looks like this:
Incident: 1 - s 1 1: a 1 2: a 2 3: a 3 Incident: 2 - s 2 4: a 4 5: a 5

In reply to Re: Perl/SQL query - grouping 'one to many' records by duckyd
in thread Perl/SQL query - grouping 'one to many' records by pcdj_d

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.