View previous topic :: View next topic |
Author |
Message |
nag77 Beginner
Joined: 03 Jan 2006 Posts: 11 Topics: 8
|
Posted: Fri Feb 24, 2006 12:27 am Post subject: Adjustment Amounts |
|
|
Hi,
I have to get the sum of the amounts for an account number till the current year. The conditions are that all amounts with Indicator = 'C' should be added and all the amounts with Indicator = 'D' should be subtracted.
Can u suggest a query for these conditions.
thanks
Nag |
|
Back to top |
|
 |
kolusu Site Admin

Joined: 26 Nov 2002 Posts: 12382 Topics: 75 Location: San Jose
|
Posted: Fri Feb 24, 2006 6:18 am Post subject: |
|
|
nag77,
Try this untested sql
Code: |
SELECT SUM(CASE WHEN IND = 'C' THEN AMT
WHEN IND = 'D' THEN AMT * -1
ELSE 0
END)
FROM TABLE;
|
Hope this helps...
Cheers
Kolusu _________________ Kolusu
www.linkedin.com/in/kolusu |
|
Back to top |
|
 |
shekar123 Advanced
Joined: 22 Jul 2005 Posts: 528 Topics: 90 Location: Bangalore India
|
Posted: Thu Mar 02, 2006 1:40 am Post subject: |
|
|
Nag,
I guess you are looking for a scenario like below:
Code: |
CREATE TABLE SHEKAR.AMOUNTS
(
INDICATOR CHAR(1) NOT NULL,
AMOUNT INTEGER NOT NULL
)
IN DBTEMP.TBTEMP;
INSERT INTO SHEKAR.AMOUNTS VALUES ('C',1000);
INSERT INTO SHEKAR.AMOUNTS VALUES ('D',500);
INSERT INTO SHEKAR.AMOUNTS VALUES ('C',1000);
INSERT INTO SHEKAR.AMOUNTS VALUES ('C',100);
INSERT INTO SHEKAR.AMOUNTS VALUES ('D',1000);
INSERT INTO SHEKAR.AMOUNTS VALUES ('D',500);
INSERT INTO SHEKAR.AMOUNTS VALUES ('C',500);
INSERT INTO SHEKAR.AMOUNTS VALUES ('D',1000);
INSERT INTO SHEKAR.AMOUNTS VALUES ('C',1000);
INSERT INTO SHEKAR.AMOUNTS VALUES ('C',1111);
SELECT SUM(CASE WHEN INDICATOR = 'C' THEN AMOUNT
WHEN INDICATOR = 'D' THEN AMOUNT * -1
ELSE 0
END)
FROM SHEKAR.AMOUNTS;
|
OUTPUT
Code: |
-------------- C -------------------- ---------- D ----------
1711 [(1000 + 1000 + 100 + 500 + 1000 + 1111) - (500 + 1000 + 500 + 1000)]
|
_________________ Shekar
Grow Technically |
|
Back to top |
|
 |
|
|