Home » SQL & PL/SQL » SQL & PL/SQL » Query (9i)
Query [message #641875] Wed, 26 August 2015 08:16 Go to next message
glmjoy
Messages: 187
Registered: September 2011
Location: KR
Senior Member
I have one column for Debit and Credit. I want to display query Debit - Credit
Here is
My table structure
Create Table FT (Document_No CHAR(5),Document_Type Char (2), Account_Code Char(5),
Amount Number)

insert into FT Values('00001','JV','A0001',7500);
insert into FT Values('00002','JV','A0005',-4500);
insert into FT Values('00001','EI','A0001',-6500);
insert into FT Values('00002','EI','A0005',7500);

I want to Minus Amount Positive as debit and Amount Negative as Credit
Debit - Credit


Select Account_Code, (Amount - Amount) from FT
it gives answer
ACCOU (AMOUNT-AMOUNT)
----- ---------------
A0001 0
A0005 0
A0001 0
A0005 0

But I want Answer

A0001 (7500 - 6500) = 1000
A0001 (7500 - 6500) = 1000

Re: Query [message #641877 is a reply to message #641875] Wed, 26 August 2015 08:45 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
What should results be when data is like below?

Create Table FT (Document_No CHAR(5),Document_Type Char (2), Account_Code Char(5),
Amount Number)

insert into FT Values('00001','JV','A0001',7500);
insert into FT Values('00002','JV','A0001',-250);
insert into FT Values('00003','JV','A0001',-200);
insert into FT Values('00004','JV','A0001',-950);
insert into FT Values('00005','JV','A0001',-750);
insert into FT Values('00006','JV','A0001',2500);
Re: Query [message #641878 is a reply to message #641875] Wed, 26 August 2015 08:49 Go to previous messageGo to next message
John Watson
Messages: 9003
Registered: January 2010
Location: Global Village
Senior Member
You need to use aggregation: GROUP BY and SUM
Re: Query [message #641879 is a reply to message #641878] Wed, 26 August 2015 08:51 Go to previous messageGo to next message
John Watson
Messages: 9003
Registered: January 2010
Location: Global Village
Senior Member
You also need to use [code] tags, please read How to use [code] tags and make your code easier to read
Re: Query [message #641880 is a reply to message #641877] Wed, 26 August 2015 09:03 Go to previous message
CraigB
Messages: 386
Registered: August 2014
Location: Utah, USA
Senior Member
I question your table design. It is not good practice to use the value of the AMOUNT field to determine if it is a Credit or Debit. Debits are not always positive and Credits are not always negative. Your design doesn't allow for this. Plus, what if you want to see ALL Credits? How would you get those from your table? You could use...
WHERE AMOUNT > 0 /* Debits */

but, how can you be sure all positive amounts are Debits? By adding a column to your table, like "AMT_TYPE" and recording if it is a Debit or Credit, you can then join your table with itself to produce the results you want.

Craig...
Previous Topic: Add +1 to Date Field (Newbie to SQL and Oracle)
Next Topic: Finding matching values
Goto Forum:
  


Current Time: Thu Aug 06 09:48:42 CDT 2026