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 Query needed
Goto page Previous  1, 2
 
Post new topic   Reply to topic   printer-friendly view    MVSFORUMS.com Forum Index -> Database
View previous topic :: View next topic  
Author Message
kolusu
Site Admin
Site Admin


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

PostPosted: Thu Jun 02, 2005 2:33 pm    Post subject: Reply with quote

Rahul,

Try this Sql. I assumed that your ACT_NO column in table CCCC is defined as Smallint/integer.

Code:

SELECT E.*                                               
  FROM (SELECT BBBB.POLICY_NO                             
              ,D.ACT_NO           ACT_NO                 
              ,SUM(BBBB.BASIC_AMT +  BBBB.CHARG_AMT) +   
               SUM(D.T_AMT) / SUM(D.T_CNT) TOTAL         
          FROM BBBB                                       
              ,(SELECT CCCC.POLICY_NO                     
                      ,CCCC.ACT_NO                       
                      ,SUM(AAAA.TRAN_AMT) T_AMT           
                      ,INT(1)             T_CNT           
                  FROM CCCC                               
                      ,AAAA                               
                 WHERE AAAA.STAT_TYPE = 'OK'             
                   AND CCCC.ACT_NO    = AAAA.ACT_NO       
                 GROUP BY CCCC.POLICY_NO                 
                         ,CCCC.ACT_NO) D                 
         WHERE BBBB.POLICY_NO = D.POLICY_NO               
         GROUP BY BBBB.POLICY_NO                         
                 ,D.ACT_NO                               
        UNION                                             
        SELECT A.POLICY_NO                               
              ,INT(0)             ACT_NO                 
              ,A.T_AMT TOTAL                             
          FROM (SELECT POLICY_NO                         
                      ,SUM(BASIC_AMT +  CHARG_AMT) T_AMT 
                  FROM BBBB                               
                 WHERE POLICY_NO NOT IN (SELECT POLICY_NO
                                           FROM CCCC)     
                                                         
                 GROUP BY POLICY_NO) A ) E               
 WHERE E.TOTAL < 0                                       
;     


Hope this helps...

Cheers

Kolusu

Ps: I ain't gonna explain the query for you. Try to understand the new query after the UNION clause. It is actually simple query.
_________________
Kolusu
www.linkedin.com/in/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
Goto page Previous  1, 2
Page 2 of 2

 
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