I have a 'transaction' table created in SQL like this:
TranID Date AccNum Type Amount ChequeNo DDNo
657520 02-07-1999 0181432 Debit 16000 465774
657524 02-07-1999 0181432 Debit 13000 569086
657538 09-07-1999 0181432 Credit 11000
657548 18-07-1999 0181432 Credit 15500
657519 02-07-1999 0181432 Debit 12000
657523 02-07-1999 0181432 Credit 11000
657529 03-07-1999 0181433 Debit 15000 466777
657539 10-07-1999 0181433 Credit 10000
657541 11-07-1999 0181433 Debit 12000
657525 03-07-1999 0181433 Debit 15000 569999
657533 05-07-1999 0181433 Credit 12500
The question is: Query data from transaction table and calculate total amount debited by cheque, dd and by cash for each account and store the result in a spreadsheet. My script is like this:
#!/usr/bin/perl
use strict;
use warnings;
use DBI;
use Spreadsheet::WriteExcel;
my $dbh = DBI->connect('dbi:mysql:database:3306','prithvi','prithvi') or die $dbh->errstr;
my $sth = $dbh->prepare("SELECT `AccNum`,`Type`,`Amount`,`ChequeNo`,`DDNo` FROM `transaction`");
$sth->execute or die $sth->errstr;
my $workbook = Spreadsheet::WriteExcel->new('query_result.xls');
my $worksheet = $workbook->add_worksheet();
my $row = 0;
my $col = 0;
my %h;
$worksheet->write_row($row++,$col,['Account Number','Cheque Debit','DD Debit','Cash Debit']);
while(my @data = $sth->fetchrow_array)
{
next unless($data[1] eq 'Debit');
my $result = $data[3] ? "ChequeNo" : $data[4] ? "DDNo" : "Cash";
$h{$data[0]}{$result} += $data[2];
$worksheet->write_row($row++,$col,\@data);
}
$sth->finish;
$dbh->disconnect;
I am not getting the proper output. Where am I going wrong? Please help. Thanks in advance. I have not got the answer for this question i.e., mainly storing the result in a spreadsheet. Please do not close this before answering. This is a very kind request to all of you.
strictandwarnings. But there's no question in the post, and you want us to write complete code. That's not what we do here. We can help you if you run into problems, but SO is not a code writing service. Also, as @RobEarl said, you should accept answers in your previous questions, or no-one will help you.<td>Account</td><td>Number-Type-Total</td><td>Debit</td><td>Amount</td>016901581432-Debit-29000... and so on. That's hardly a spreadsheet.