Hi guys,

sorry to trouble u guys,but can't resist

Actually i have a table (transaction table) in which i have got customer id(can have more than one record with same id) ,acc no, and type (either credit or debit). Now i want to find the balance of the customer by inputting account number

the table will look like

cust id acc no type amount

1010 2323 cr(credit) 2000
1020 2324 dr(debit) 3000
1010 2323 dr 4000
1020 2324 cr 2000

pls help me out

Dani AI

Generated

Two quick corrections that build on the thread: a plain SUM ignores whether a row is a credit or debit (so it can be wrong), and the DECODE trick shown by is an Oracle feature, not available in MySQL. For MySQL you can turn each row into a signed amount at query time and then aggregate.

A portable, readable approach is conditional aggregation. Example MySQL options:

SELECT acc_no,
       SUM(CASE WHEN txn_type = 'cr' THEN amount
                WHEN txn_type = 'dr' THEN -amount
                ELSE 0 END) AS balance
FROM transactions
WHERE acc_no = '2323'
GROUP BY acc_no;

or using MySQL's IF():

SELECT acc_no,
       SUM(IF(txn_type = 'dr', -amount, amount)) AS balance
FROM transactions
WHERE acc_no = '2323'
GROUP BY acc_no;

Practical tips and gotchas:

  • Normalize the type values (use ENUM or constrained values) so comparisons are reliable ('cr' vs 'CR' vs 'credit').
  • Use DECIMAL for money and COALESCE(amount,0) to guard against NULLs.
  • Index acc_no (and consider a composite index if querying ranges by date). For very large tables, consider maintaining a running balance or periodic aggregate table rather than summing all history on every lookup.
  • If strict accuracy under concurrent writes is required, compute balances inside a transaction or update a cached balance atomically.

For reference on the MySQL constructs used above, see the MySQL documentation for CASE and IF (MySQL 8.0). The query patterns above implement the same idea discussed in the thread but use MySQL-native functions and include practical robustness tips.

Recommended Answers

All 2 Replies

customer_id and account_number seem a little redundant to me.

SELECT sum(amount)
FROM transaction_table
WHERE acc_num = "2323";

I think you might need a GROUP BY clause in there as well. Though that won't give you the answer you need, maybe it'll help. I only basic SQL myself. I haven't quite learned PL/SQL yet, which is what I would use to use an IF statement to see whether to add or subtract the amount.

On second thought, I think you can do this with a nested query.

Hi
Thanks for ur help. But ur query wont work as the amount entered in the transaction table will be positive only. So we have to differentiate between the amt credited and debited.

The query u gave will just give me the sum even if the amt is debited

I have got the answer for that

select cus_id, acc_id, sum(decode(acc_type,\'dr\',balance*-1,balance)) from
test_acc

this will put a - sign wherever the type is debit temporarily. It doesnt effect ur original table

As u are a learner,this query will help you in future

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.