-1

I have a 'transaction' table created in SQL like this:

TransactionID    Date           AccountNumber   Type    Amount  
657520           02-07-1999     016901581432    Debit   16000  
657524           02-07-1999     016901581432    Debit   13000  
657538           09-07-1999     016901581432    Credit  11000  
657548           18-07-1999     016901581432    Credit  15500  
657519           02-07-1999     016901581433    Debit   12000  
657523           02-07-1999     016901581433    Credit  11000  
657529           03-07-1999     016901581433    Debit   15000  
657539           10-07-1999     016901581433    Credit  10000  
657541           11-07-1999     016901581434    Debit   12000  
657525           03-07-1999     016901581434    Debit   15000  
657533           05-07-1999     016901581434    Credit  12500  

I have to find the total debit amount and total credit amount for each account using database. My code is like this:

#!/usr/bin/perl
use DBI;
use strict;
use warnings;
print "content-type:text/html\n\n";
$dbh = DBI->connect('dbi:___','prithvi','*password*') or die "Couldn't connect";
my %trans;
my $tran = $dbh->prepare("SELECT * FROM `transaction` LIMIT 0 , 11");
$tran->execute;
while(my @row = $tran->fetchrow_hash) 
{
    my $tran = join ',', @row;
    $trans{$tran[2]}{$tran[3]} += $tran[4];
}
foreach my $acno(sort keys %trans) 
{
print "Total Amount deposited and total amount credited for Account Number $acno is Rs.$trans{$acno}{'Debit'} and Rs.$trans{$acno}{'Credit'}\n";
}
$tran->finish;
$dbh->disconnect;

Where am I going wrong?

7
  • 1
    What is the exact error you are getting ? Commented Oct 3, 2012 at 6:27
  • I am not getting any error but I am not getting the proper output Commented Oct 3, 2012 at 6:44
  • Add use strict and use warnings , it might point to a problem. Commented Oct 3, 2012 at 7:36
  • A few more beginner tips: You don't need to print the content-type line if it is not in a CGI environment. You also haven't declared $dbh, which strict will tell you about. It's generally a good idea to name things like they are named in the documentation. Most people call their statement handles (the result of $dbh->prepare) $sth or $sth_something. Commented Oct 3, 2012 at 8:45
  • 1
    The edits you made to this question render the answers you received completely obsolete. Consider showing your original code instead? Or at least post the latest as an update at the bottom? This is bordering on too localized now, and may be closed as such. Commented Oct 3, 2012 at 15:10

2 Answers 2

1

If you add use strict and use warnings you'll probably get some feedback about needing to declare @tran in the code below:

while(my @row = $tran->fetchrow_hash) 
{
    my $tran = join ',', @row;
    $trans{$tran[2]}{$tran[3]} += $tran[4];
}

$tran is 657520,02-07-1999,016901581432,Debit,16000 when you try and use it as an array.

Use Data::Dumper to show what you put into %trans at the end:

use Data::Dumper;
print Dumper(\%trans);

Did you mean:

while(my @row = $tran->fetchrow_array) 
{
    $trans{$row[2]}{$row[3]} += $row[4];
}
Sign up to request clarification or add additional context in comments.

1 Comment

Oh.Yes.You are right.I got the output.Thank you so much everyone
0

Use SQL to get this answer not code.

SELECT AccountNumber, Type, SUM(Amount) FROM transaction GROUP BY AccountNumber, Type;

5 Comments

I want to get the output from Perl script itself. Thats why I am asking help.
I dont no whether its possible or no.
Can you please tell me what is the single SQL query for finding both Debit Amount and Credit Amount? Thanks in advance.
You should accept an answer and ask a new question. See the comments on this question. However, I've edited this answer to what you're asking.
Thank you so muc Mr.RobEarl. Finally I got the output as required from your help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.