Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re^2: AVG returns COUNT

by ChemBoy (Priest)
on Aug 05, 2005 at 22:14 UTC ( [id://481379]=note: print w/replies, xml ) Need Help??


in reply to Re: AVG returns COUNT
in thread AVG returns COUNT

Why not just use SQL?

my $r = $dbh->selectall_arrayref("SELECT avg(...), count(1) FROM prosp +ects"); my ($avg,$count) = @{$r->[0]};

update: Just as you were saying. Not sure how I managed that bad a mis-read while retaining enough brain cells to type straight... <blush>



If God had meant us to fly, he would *never* have given us the railroads.
    --Michael Flanders

Replies are listed 'Best First'.
Re^3: AVG returns COUNT
by DrAxeman (Scribe) on Aug 06, 2005 at 01:17 UTC
    I tried this, and I think I am missing something. Here is what my script looks like:
    #!/usr/bin/perl #use strict; use warnings; use DBI; use Text::CSV; # Connect to the database, (the directory containing our csv file( +s)) my $dbh = DBI->connect("DBI:CSV:f_dir=.;csv_eol=\n;"); # Associate our csv file with the table name 'results' $dbh->{'csv_tables'}->{'results'} = { 'file' => 'test.csv'}; # my ($avg) = $dbh->selectrow_array("SELECT avg(ERWWCOMMUNITIES_Mem +ory_Pages_sec) FROM results"); my ($r) = $dbh->selectall_arrayref("SELECT avg(ERWWCOMMUNITIES_Mem +ory_Pages_sec), count(1) FROM results"); my ($agv,$count) = @{$r->[0]}; # print ("$avg,\n"); print ("@r \n");
    My output is
    Possible unintended interpolation of @r in string at ./runsql.pl line +41. Name "main::r" used only once: possible typo at ./runsql.pl line 41. SQL ERROR: Bad table or column name '1' starts with non-alphabetic cha +racter! Execution ERROR: No command found!. Use of uninitialized value in array dereference at ./runsql.pl line 18 +.
    Sorry to keep bothering with this.

      You are correct—you're missing the use strict that you commented out from the beginning of the file. :-)

      Had you left it in, and left uncommented both print lines, you would have gotten (and probably did get) these errors:

      Global symbol "$avg" requires explicit package name at runsql.pl line +20. Global symbol "@r" requires explicit package name at runsql.pl line 21 +.

      What those messages are telling you is that you have a variable you didn't declare using our, use vars or my. If you think they're wrong, you probably have done one of two things: tried to use an array or hash in place of a reference to one, or made a typo. In this case, conveniently enough, you have one of each. :-) If you comment out print ("@r \n") (by which you presumably meant print "@{$r} \n") and change my $agv to my $avg, you should see those errors go away (after you put strict back in so they show up, of course).

      Then you just have the SQL error, which is annoying—I would class it as a bug in the DBD module, but in any case, it should go away if you change count(1) to count(*), or possibly count(ERWWCOMMUNITIES_Memory_Pages_sec). Or, since you don't really need it anyway, just take it out entirely.

      And if you don't know what I mean by a reference to an array or hash, take a quick look at perlref and the always-popular References quick reference, and you shall be enlightened. :-)

      Good luck!



      If God had meant us to fly, he would *never* have given us the railroads.
          --Michael Flanders

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (None)
    As of 2024-04-25 00:23 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found