Thread: how to add 'BALANCE' column
- 04-08-2009 04:56 AM #1
how to add 'BALANCE' column hi,
I am working with a query and successfully get the Debit side (from Table A) and Credit side (from Table B) but i am unable to make the balance column (not exist physically in any table). Any body can help me in this regard. I am using SQL and Oracle 6i Report builder.
I want to get the following output.
Example:
Date Debits Credits Balance 01JAN 100 100 02JAN 50 50 03JAN 100 -50 04JAN 200 150
- Club-Oracle Complementary E-Books and Magazines
Get your free Complementary Copy of Oracle Magazine
You can also browse the Free Magazines and E-Books section to see the complete list of free magazines, e-books and Whitepapers.
- 04-08-2009 05:08 AM #2
Re: how to add 'BALANCE' column well share your query please, and if possible the table structure
Learn Oracle with Oracle forum. Check out The Technology Blog
- 04-08-2009 05:20 AM #3
Re: how to add 'BALANCE' column Following is the exact query.
Code sql:SELECT rcta.TRX_DATE AS T_R_DATE, rcta.TRX_NUMBER AS T_R_NUMBER,
sum(rctla.EXTENDED_AMOUNT) DR, NULL AS CR
FROM RA_CUSTOMER_TRX_ALL rcta,
RA_CUSTOMER_TRX_LINES_ALL rctla
WHERE rcta.BILL_TO_CUSTOMER_ID = :C_ID
AND rcta.TRX_DATE <= to_date(:TR_DATE)
AND rctla.CUSTOMER_TRX_ID = rcta.CUSTOMER_TRX_ID -- join
GROUP BY rcta.TRX_NUMBER, rcta.TRX_DATE
UNION
SELECT acra.RECEIPT_DATE AS T_R_DATE,acra.RECEIPT_NUMBER AS T_R_NUMBER, NULL AS DR, acra.AMOUNT AS CR
FROM AR_CASH_RECEIPTS_ALL acra
WHERE acra.PAY_FROM_CUSTOMER = :C_ID
AND acra.RECEIPT_DATE <= to_date(:TR_DATE)
AND acra.STATUS IN ('APP','UNAPP')
ORDER BY T_R_DATE ASC
- 04-08-2009 10:28 AM #4
Re: how to add 'BALANCE' column You can use Analytical function (SUM) for the same purpose.
Code sql:SQL> SELECT DATE_D,DEBITS, CREDITS
2 FROM DB_CR_BAL;
DATE_D DEBITS CREDITS
--------- ---------- ----------
01-JAN-09 100
02-JAN-09 50
03-JAN-09 100
04-JAN-09 200
05-JAN-09 150
SQL>
SQL> SELECT DATE_D,DEBITS, CREDITS,
2 SUM(NVL(DEBITS,0)-NVL(CREDITS,0)) OVER (ORDER BY DATE_D) BAL
3 FROM DB_CR_BAL ;
DATE_D DEBITS CREDITS BAL
--------- ---------- ---------- ----------
01-JAN-09 100 100
02-JAN-09 50 50
03-JAN-09 100 -50
04-JAN-09 200 150
05-JAN-09 150 0
SQL>
Raj.
- 04-08-2009 11:09 AM #5
Re: how to add 'BALANCE' column but as you can see in my query, i am using UNION is it possible with UNION ?
- 04-08-2009 11:18 AM #6
Re: how to add 'BALANCE' column Yes. But you have to use the SUM Analytical function over the Union result as below.
Code sql:SELECT T_R_DATE,T_R_NUMBER, DR, CR ,
SUM(NVL(DR,0) - NVL(CR,0)) OVER (ORDER BYT_R_DATE,T_R_NUMBER) BALANCE
FROM (
SELECT rcta.TRX_DATE AS T_R_DATE, rcta.TRX_NUMBER AS T_R_NUMBER,
sum(rctla.EXTENDED_AMOUNT) DR, NULL AS CR
FROM RA_CUSTOMER_TRX_ALL rcta,
RA_CUSTOMER_TRX_LINES_ALL rctla
WHERE rcta.BILL_TO_CUSTOMER_ID = :C_ID
AND rcta.TRX_DATE <= to_date(:TR_DATE)
AND rctla.CUSTOMER_TRX_ID = rcta.CUSTOMER_TRX_ID -- join
GROUP BY rcta.TRX_NUMBER, rcta.TRX_DATE
UNION
SELECT acra.RECEIPT_DATE AS T_R_DATE,acra.RECEIPT_NUMBER AS T_R_NUMBER, NULL AS DR, acra.AMOUNT AS CR
FROM AR_CASH_RECEIPTS_ALL acra
WHERE acra.PAY_FROM_CUSTOMER = :C_ID
AND acra.RECEIPT_DATE <= to_date(:TR_DATE)
AND acra.STATUS IN ('APP','UNAPP')
)
Raj.
- 04-10-2009 06:47 AM #7
Re: how to add 'BALANCE' column It really works, thank you so much.
Similar Threads
-
Row to Column Transformation
By rana.rajnikant in forum SQL PL/SQLReplies: 3Last Post: 11-11-2009, 10:01 AM -
query an xmlType column
By kowalsky in forum SQL PL/SQLReplies: 1Last Post: 01-27-2009, 03:58 AM


LinkBack URL
About LinkBacks
Reply With Quote


