Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
I have a transaction table 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
My question is: Query the data from transaction table and calculate the total amount debited by cheque, dd and cash for each account and store the result in a spreadsheet? My code 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`,`D +DNo` 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

In reply to SQL Database table to Perl Spreadsheet script by dinkar

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, 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, 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.
  • Log In?

    What's my password?
    Create A New User
    and not a whimper to be heard...

    How do I use this? | Other CB clients
    Other Users?
    Others making s'mores by the fire in the courtyard of the Monastery: (4)
    As of 2018-04-20 16:42 GMT
    Find Nodes?
      Voting Booth?