IBM Mainframe Forum Index
 
Log In
 
IBM Mainframe Forum Index Mainframe: Search IBM Mainframe Forum: FAQ Register
 

Query regarding modifying a SQL


IBM Mainframe Forums -> DB2
Post new topic   Reply to topic
View previous topic :: View next topic  
Author Message
rexx77

New User


Joined: 14 Apr 2008
Posts: 78
Location: Mysore

PostPosted: Tue Apr 28, 2015 11:35 am
Reply with quote

Hi All,

Greetings.!!

Currently I have a query which extracts data from the DB2 table and sends to GUI. I need to do arithmetic operation in one of the query for the change of requirement. below is the snippet of the query
Code:

SELECT  1
     ,  C_LBL
     ,  Q_CV_RSTR_OPT_1
     ,  Q_CV_RSTR_OPT_2
     ,  Q_CV_RSTR_OPT_3
  FROM  Table1
 WHERE  condition 1
   ...
   and  condition n
   AND C_LBL NOT LIKE :LT-UA

UNION

SELECT  1
     ,  'OTHERS'
     ,SUM(Q_CV_RSTR_OPT_1)
     ,SUM(Q_CV_RSTR_OPT_2)
     ,SUM(Q_CV_RSTR_OPT_3)
  FROM  Table1
 WHERE condition 1
   ...
   and condition n
   AND C_LBL  LIKE :LT-UA


Table data looks something looks below
Code:

     C_LBL             QTY_1                  QTY_2         QTY_3   
---------------- -------------------- --------------------        --------------------
********************************* TOP ****************************   ****************************
BRRW                            0.000                    0.000                  0.000                 
CLNT                            0.000                    0.000         0.000
CRED                            0.000                    0.000         0.000
ISSU                            0.000                    0.000         0.000
SPLT                            0.000                    0.000         0.000
ABC                             0.000                    0.000         0.000
UA-XX                            0.000                    0.000         0.000
UA-YY                            0.000                    0.000         0.000
UA-ZZ                            0.000                    0.000         0.000
UA-AA                           0.000                    0.000         0.000
UA-BB                             0.000                    0.000         0.000
SPLITADJ                             0.000                    0.000         0.000





Now I will get one more record which is SPLITADJ in the C_LBL column. I need to deduct this value from all other UA-% and send to GUI.
Earlier I will just sum all the UA values and send to GUI, but now I need to deduct the SPLITADJ from the UA- records sum and send the results to GUI. Other Label records(other than UA) should be summed as usual

Appreciate your ideas on how to do this ?
Back to top
View user's profile Send private message
RahulG31

Active User


Joined: 20 Dec 2014
Posts: 446
Location: USA

PostPosted: Tue Apr 28, 2015 6:48 pm
Reply with quote

I think instead of:
Code:
SELECT  1
     ,  'OTHERS'
     ,SUM(Q_CV_RSTR_OPT_1)
     ,SUM(Q_CV_RSTR_OPT_2)
     ,SUM(Q_CV_RSTR_OPT_3)

You may need to write:
Code:
SELECT  1
     ,  'OTHERS'
     ,(Select SUM(Q_CV_RSTR_OPT_1) from Table1 where C_LBL like UA-% - Select QTY1 from Table1 where C_LBL = 'SPLITADJ')
     ,(Select SUM(Q_CV_RSTR_OPT_2) from Table1 where C_LBL like UA-% - Select QTY2 from Table1 where C_LBL = 'SPLITADJ')
     ,(Select SUM(Q_CV_RSTR_OPT_3) from Table1 where C_LBL like UA-% - Select QTY3 from Table1 where C_LBL = 'SPLITADJ')


This may not be the best possible solution but this is what I can think of
.
Back to top
View user's profile Send private message
chandan.inst

Active User


Joined: 03 Nov 2005
Posts: 275
Location: Mumbai

PostPosted: Wed Apr 29, 2015 10:48 am
Reply with quote

Hi,

Try below query, this is untested

Code:
SELECT  1
     ,  'OTHERS'
     ,SUM(CASE WHEN C_LBL = 'SPLITADJ' THEN (-1 *  Q_CV_RSTR_OPT_1) ELSE Q_CV_RSTR_OPT_1 END)
     ,SUM(CASE WHEN C_LBL = 'SPLITADJ' THEN (-1 *  Q_CV_RSTR_OPT_2) ELSE Q_CV_RSTR_OPT_2 END)
     ,SUM(CASE WHEN C_LBL = 'SPLITADJ' THEN (-1 *  Q_CV_RSTR_OPT_3) ELSE Q_CV_RSTR_OPT_3 END)
  FROM  Table1
 WHERE condition 1
   ...
   and condition n
   AND ( C_LBL  LIKE :LT-UA OR C_LBL = 'SPLITADJ')



Let us know if anything missing here

Regards,
Chandan
Back to top
View user's profile Send private message
rexx77

New User


Joined: 14 Apr 2008
Posts: 78
Location: Mysore

PostPosted: Wed Apr 29, 2015 12:04 pm
Reply with quote

Thanks Chandan and Rahul for your ideas.. I am able to achieve the desired results. Much appreciated.
Back to top
View user's profile Send private message
View previous topic :: :: View next topic  
Post new topic   Reply to topic View Bookmarks
All times are GMT + 6 Hours
Forum Index -> DB2

 


Similar Topics
Topic Forum Replies
No new posts Modifying Date Format Using DFSORT DFSORT/ICETOOL 9
No new posts RC query -Time column CA Products 3
No new posts Dynamically pass table name to a sele... DB2 2
No new posts Query on edit primary command CLIST & REXX 5
No new posts Query on edit primary command CLIST & REXX 1
Search our Forums:

Back to Top