Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: Perl/SQL query - grouping 'one to many' records

by duckyd (Hermit)
on Dec 10, 2008 at 00:47 UTC ( [id://729309]=note: print w/replies, xml ) Need Help??


in reply to Perl/SQL query - grouping 'one to many' records

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

Replies are listed 'Best First'.
Re^2: Perl/SQL query - grouping 'one to many' records
by pcdj_d (Initiate) on Dec 11, 2008 at 09:14 UTC
    Thanks duckyd, that worked a treat!

    One further question though - my results come out in a seemingly random order and cannot be sorted in any way. I believe this is due to the nature of hashes, and you can't guarantee in which order they are returned.

    Is there any simple way of modifying this so that I can have some control over how the results are sorted when displayed on screen? Something as simple as sorting by $i_id would be good!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://729309]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (2)
As of 2024-04-26 01:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found