MVSFORUMS.com Forum Index MVSFORUMS.com
A Community of and for MVS Professionals
 
 FAQFAQ   SearchSearch   Quick Manuals   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

SQL to relate table rows

 
Post new topic   Reply to topic   printer-friendly view    MVSFORUMS.com Forum Index -> Database
View previous topic :: View next topic  
Author Message
Celeste Locklin
Beginner


Joined: 04 May 2006
Posts: 26
Topics: 10
Location: USA

PostPosted: Thu Sep 15, 2011 1:12 pm    Post subject: SQL to relate table rows Reply with quote

My requirement is to add the amts that correspond to the cds where the cd is one of the following values: 'C','D','K','3'

My table contains the following rows:
Code:

PMT_CD_1   CHAR(2)         K       
PMT_CD_2   CHAR(2)         9       
PMT_CD_3   CHAR(2)         D       
PMT_CD_4   CHAR(2)                 
PMT_CD_5   CHAR(2)                 

PMT_AMT_1      DEC(11,2)       106.84     
PMT_AMT_2      DEC(11,2)       125.74     
PMT_AMT_3      DEC(11,2)       103.16     
PMT_AMT_4      DEC(11,2)       0.00       
PMT_AMT_5      DEC(11,2)       0.00       

Using the table above, the amount that would be produced would be 106.84+103.16 =210.00. Is there a way to do this with SQL ?
Back to top
View user's profile Send private message
kolusu
Site Admin
Site Admin


Joined: 26 Nov 2002
Posts: 12376
Topics: 75
Location: San Jose

PostPosted: Thu Sep 15, 2011 1:50 pm    Post subject: Reply with quote

Celeste Locklin,

Are they 5 different rows or data corresponding to 1 just 1 row of the table?

I am guessing it is just a single row
Code:

CD1 CD2 CD3 CD4 CD5   AMT1    AMT2    AMT3    AMT4    AMT5
K    9   D          106.84  125.74  103.16    0.00    0.00


If that is the case then you can use the following untested sql
Code:

SELECT CASE WHEN PMT_CD1 IN ('C','D','K','3')     
            THEN PMT_AMT_1 ELSE DEC(0) END +       
       CASE WHEN PMT_CD2 IN ('C','D','K','3')     
            THEN PMT_AMT_2 ELSE DEC(0) END +       
       CASE WHEN PMT_CD3 IN ('C','D','K','3')     
            THEN PMT_AMT_3 ELSE DEC(0) END +       
       CASE WHEN PMT_CD4 IN ('C','D','K','3')     
            THEN PMT_AMT_4 ELSE DEC(0) END +       
       CASE WHEN PMT_CD5 IN ('C','D','K','3')     
            THEN PMT_AMT_5 ELSE DEC(0) END         
  FROM Table
  ;               
Back to top
View user's profile Send private message Send e-mail Visit poster's website
Celeste Locklin
Beginner


Joined: 04 May 2006
Posts: 26
Topics: 10
Location: USA

PostPosted: Fri Sep 16, 2011 3:45 pm    Post subject: Reply with quote

you are the best ! I had to do a little tweaking, adding sum, digits, and dec() but it works
Code:

SELECT DIGITS(SUM                                         
      (CASE WHEN PMT_CD_1 IN ('C ','D ','K ','3 ')
            THEN DEC(PMT_AMT_1) ELSE DEC(0) END +     
       CASE WHEN PMT_CD_2 IN ('C','D','K','3')     
            THEN DEC(PMT_AMT_2) ELSE DEC(0) END +     
       CASE WHEN PMT_CD_3 IN ('C','D','K','3')     
            THEN DEC(PMT_AMT_3) ELSE DEC(0) END +     
       CASE WHEN PMT_CD_4 IN ('C','D','K','3')     
            THEN DEC(PMT_AMT_4) ELSE DEC(0) END +     
       CASE WHEN PMT_CD_5 IN ('C','D','K','3')     
            THEN DEC(PMTAMT_5) ELSE DEC(0) END ) )   
        FROM  table                       
      WHERE    . . .             

thanks a billion
Back to top
View user's profile Send private message
kolusu
Site Admin
Site Admin


Joined: 26 Nov 2002
Posts: 12376
Topics: 75
Location: San Jose

PostPosted: Fri Sep 16, 2011 4:08 pm    Post subject: Reply with quote

Celeste Locklin,

I don't see a reason to have DEC(PMT_AMT_1) in the case statement when PMT_AMT_1 is already defined as DEC. You can directly refer like I showed.

Kolusu
Back to top
View user's profile Send private message Send e-mail Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic   printer-friendly view    MVSFORUMS.com Forum Index -> Database All times are GMT - 5 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


MVSFORUMS
Powered by phpBB © 2001, 2005 phpBB Group