Portal | Manuals | References | Downloads | Info | Programs | JCLs | Master the Mainframes
IBM Mainframe Computers Forums Index
 
Register
 
IBM Mainframe Computers Forums Index Mainframe: Search IBM Mainframe Forum: FAQ Memberlist Usergroups Profile Log in to check your private messages Log in
 

 

Subtracting two columns and finding the result in new column

 
Post new topic   Reply to topic    IBMMAINFRAMES.com Support Forums -> DB2
View previous topic :: :: View next topic  
Author Message
ajay_udl

New User


Joined: 10 Oct 2006
Posts: 29

PostPosted: Fri Jul 24, 2009 5:47 am    Post subject: Subtracting two columns and finding the result in new column
Reply with quote

Hi All,

I'm running some Queries via QMF.

SELECT
CUST_ID,
SUM(CASE TRAN_TYPE_CD WHEN 'D' THEN TRAN_AMT END) AS D_AMT,
SUM(CASE TRAN_TYPE_CD WHEN 'C' THEN TRAN_AMT END) AS C_AMT
FROM TP1.QUERY11_OUT
WHERE CUST_ID IN( SELECT CUST_ID FROM TP1.QUERY15_OUT)
GROUP BY CUST_ID


In the above query I'm getting the amount for D_AMT and C_AMT.
I wanted to find the net of both in a new column
ie; D_AMT - C_AMT = NET.

Is it possible to achieve in the same query or do I need to save this Query and then write another one to achieve this?
How do i achieve this if I'm writing a new one?

thanks in advance!!

Regards
Ajay.
Back to top
View user's profile Send private message

Dsingh29

Active User


Joined: 16 Dec 2008
Posts: 132
Location: IBM

PostPosted: Fri Jul 24, 2009 12:53 pm    Post subject:
Reply with quote

Assuming that your query was
SELECT KEYFIELD, VALUE1, VALUE2 FROM TABLENAME
and you wanted a 4th column containing Value1 less Value2, you could try
SELECT KEYFIELD, VALUE1, VALUE2, VALUE1-VALUE2 AS DIFFERENCE FROM TABLENAME.

If your question meant something else, please clarify.
Back to top
View user's profile Send private message
ashimer

Active Member


Joined: 13 Feb 2004
Posts: 551
Location: Bangalore

PostPosted: Fri Jul 24, 2009 2:54 pm    Post subject:
Reply with quote

Code:


SELECT D_AMT, C_AMT, D_AMT - C_AMT AS NET FROM
(
SELECT
CUST_ID,
SUM(CASE TRAN_TYPE_CD WHEN 'D' THEN TRAN_AMT END) AS D_AMT,
SUM(CASE TRAN_TYPE_CD WHEN 'C' THEN TRAN_AMT END) AS C_AMT
FROM TP1.QUERY11_OUT
WHERE CUST_ID IN( SELECT CUST_ID FROM TP1.QUERY15_OUT)
GROUP BY CUST_ID
) AS A ;

Back to top
View user's profile Send private message
View previous topic :: :: View next topic  
Post new topic   Reply to topic    IBMMAINFRAMES.com Support Forums -> DB2 All times are GMT + 6 Hours
Page 1 of 1

 

Search our Forum:

Similar Topics
Topic Author Forum Replies Posted
No new posts DB2 query Using Recursion, Converting... smilewithashu2 DB2 1 Tue Jan 03, 2017 12:50 pm
This topic is locked: you cannot edit posts or make replies. Db2 SQL query to convert rows into co... subratarec DB2 5 Thu Nov 17, 2016 4:51 pm
No new posts SQZ pipe delimited file with 1 column... Rick Silvers DFSORT/ICETOOL 2 Sun Nov 06, 2016 8:11 pm
This topic is locked: you cannot edit posts or make replies. Rexx to create GDG by scanning JJ result krish.deepu CLIST & REXX 5 Tue Oct 25, 2016 5:32 pm
No new posts Comparing Decimal and CHAR columns rakesh17684 DB2 7 Thu Oct 20, 2016 2:33 am


Facebook
Back to Top
 
Mainframe Wiki | Forum Rules | Bookmarks | Subscriptions | FAQ | Tutorials | Contact Us