• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 635
  • Last Modified:

SQL sum


I have the following three fields with sample data in Oracle database. The data in amount field are all positive. How to make amount to be negative if credit_debit='C' and get the sum of amount field base on tranx_no?

tranx_no   credit_debit            Amount
 001                 C                        80
 001                 D                      100
 001                 C                        20

Thanks in advance.
1 Solution
slightwv (䄆 Netminder) Commented:
Just use a CASE statement.


select tranx_no, credit_debit, sum(case when credit_debit='C' then amount*-1 else amount end) amount from table_name group by tranx_no, credit_debit;
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now