in reply to Re^2: Combine data in table
in thread Combine data in table

I would recommend using the "SELECT ... GROUP BY" SQL statement -- it's very conscise and exactly what you want, and leverages the fact you're using DBD/CSV ..


As for your code in this post, though, you have my $row = "IP,Partition,PartitionSize,PartitionFree"; which just sets some string as the value for $row .. you need it to be all the data as an array of hashrefs ..
my $rows = $dbh->selectall_arrayref("SELECT * FROM your_table", {Slice +=>{}}, ); my %ips; foreach my $row (@$rows){ $ips{ $row->{colIP} }->{ colPartitionSize } += $row->{colPartitionSi +ze}; $ips{ $row->{colIP} }->{ colPartitionFree } += $row->{colPartitionFr +ee}; } print Dumper \%ips;
(Note that this is inefficient, as well -- would be better to use a $sth and fetch one row at a time instead of sucking in all the rows at once)

Replies are listed 'Best First'.
Re^4: Combine data in table
by DrAxeman (Scribe) on Aug 07, 2005 at 23:25 UTC
    Using this method, how would I print out %ips? I'd like data comming out to like the original data:

    IP,PartitionFree,PartitionSize 1.2.3.4,23452,234322 2.3.4.5,99348,8666623
      here's three different ways.. note that the second two make it trivial to output in a sorted manner (something like sort keys %ips instead of just keys %ips):
      while( my ($ip, $h) = each %ips ){ printf "%s,%d,%d\n", $ip, $h->{colPartitionSize}, $h->{colPartitionF +ree}; } foreach my $ip ( keys %ips ){ my $h = $ips{$ip}; printf "%s,%d,%d\n", $ip, $h->{colPartitionSize}, $h->{colPartitionF +ree}; } printf "%s,%d,%d\n", $_, $ips{$_}->{colPartitionSize}, $ips{$_}->{colP +artitionFree} for keys %ips;
Re^4: Combine data in table
by DrAxeman (Scribe) on Aug 07, 2005 at 19:10 UTC
    I got the Perl loop method to work (mostly for the experience). But now I'm working on the SQL method. My code looks like:

    # 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' => "psinfooutputdiskfile. +csv"}; my @rows = $dbh->selectrow_arrayref("SELECT IP, SUM(PartitionSize), SU +M(PartitionFree) FROM results GROUP BY IP"); print ("@rows\n"); # Just here to view output

    I'm getting

    SQL ERROR: Can't find table names in FROM clause! Execution ERROR: No command found!.

    Not sure what is wrong here.

    it I modify the SELECT statement to:
    my @rows = $dbh->selectrow_arrayref("SELECT IP FROM results");

    It works. But if I try to include a GROUP BY or SUM I get the error.
      Note first that you probably want my $rows = $dbh->selectall_arrayref( ... ) instead of selectrow_arrayref()

      are those column names right? should they be "colIP", "colPartitionSize", "colPartitionFree" instead?

      Besides that, the SQL looks fine .. do the statements "SELECT IP FROM results" and "SELECT IP FROM results GROUP BY IP" and "SELECT SUM(PartitionSize), SUM(PartitionFree) FROM results" work?
        #!/usr/bin/perl use strict; use warnings; use DBI; use Data::Dumper; # 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' => "psinfooutputdiskfile. +csv"}; my @rows = $dbh->selectall_arrayref("SELECT IP FROM results GROUP BY I +P"); print Dumper @rows;
        Fails. If I take the GROUP BY off, everything is OK. Same with the SUM statements.

        "SELECT IP FROM results" works
        "SELECT IP FROM results GROUP BY IP" and "SELECT SUM(PartitionSize), SUM(PartitionFree) FROM results" fail.