I would not use the cut-and-paste method that you've used -- make an array of the response that you want to count, and count those up. I would also count up all of the responses. What's left over is the 'other'.

Something like this (not tested code):

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 ( <tr><td>$Responses{ $_ }</td><td>$Result</td></tr> ); } # Figure out how many "Other" responses there were. my $Result = sprintf ( "%3.1f%%", ( $Total - $SoFar ) / $Total ); print qq ( <tr><td>Other</td><td>$Result</td></tr> );

--t. alex

"There was supposed to be an earth-shattering kaboom!" --Marvin the Martian


In reply to Re: Perl - MySQL query question by talexb
in thread Perl - MySQL query question by peppiv

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.