my $BaseQuery " select count(*) from owner"; my $WhereClause = " where rent_car = '?'"; # Get the total number of rows in the table. my $sth = $dbh->prepare ( $BaseQuery ); my $Total = $sth->fetchrow_array(); # Initialize a total in order to be able to calculate # "Other" responses" my $SoFar = 0; # Prepare the longer query. $sth = $dbh->prepare ( $BaseQuery . $WhereClause ); # Loop through valid responses. my %Responses = ( Y => "Yes", N => "No" ); foreach ( keys %Responses ) { $sth->execute ( $_ ); my $ThisChoice = $sth->fetchrow_array(); $SoFar += $ThisChoice; my $Result = sprintf ( "%3.1f%%", $ThisChoice / $Total ); print qq ( $Responses{ $_ }$Result ); } # Figure out how many "Other" responses there were. my $Result = sprintf ( "%3.1f%%", ( $Total - $SoFar ) / $Total ); print qq ( Other$Result );