I have a situation where I have a table of incidents and a table of actions. The relationship is one incident has many actions.

I require the need to display via a web page, a list of incidents IDs and their relevant actions thus:

Indicent 1
- Action 1
- Action 2
- Action 3
Indicent 2
- Action 4
Incident 3
- Action 5
- Action 6

Currrently, I just retrieve a set of actions and can sort them by incident ID. However I would like each action grouped by incident, and the incident ID to be displayed just the once before each group of actions.

I currently have in one subroutine:
my $sql = " SELECT incident.id AS i_id, incident.severity, incident.summary, action.id AS a_id, action.incident_id, action.description, action.note +s FROM incident, action WHERE incident.id = action.incident_id AND action.due_date > $date (a specified date) AND action.due_date < current_date() ORDER BY sort_date ASC " $sth->prepare($sql); $sth->execute(); while (my $href =$sth->fetchrow_hashref()) { my $details={}; $details->{'Incident ID'} = $$href{'i_id'}; $details->{'Action ID'} = $$href{'a_id'}; $details->{'Severity'} = $$href{'severity'}; $details->{'Summary'} = $$href{'summary'}; and so on... push(my @results, $details); } return @results
And in another subroutine:
my @results = "first subroutine" foreach my $result (@results) { my $output = $result->{'Incident ID'}.", "; $output .= $result->{'Action ID'}.", "; $output .= $result->{'Severity'}.", "; $output .= $result->{'Summary'}.", "; and so on... $counter++ } return $output;
This will return something along the following lines:

1, 1, Severe, A severe incident
1, 2, Severe, A severe incident
1, 3, Severe, A severe incident
2, 4, Minor, A minor incident
2, 5, Minor, A minor incident

I would like something like:

1, Severe, A severe incident
-> 1 (action id, with further columns from the action table)
-> 2
-> 3
2, Minor, A minor incident
-> 4
-> 5

What's the easiest way to do this?

In reply to 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.