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

SQL Query help


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

Active User


Joined: 28 Aug 2008
Posts: 205
Location: Hyderabad

PostPosted: Fri Apr 17, 2009 2:25 pm
Reply with quote

Hi All,

can any one help me with the below requirement:

I have two table: Table1 and Table2

Table1 has customer card# and organisation to which it is linked and the linked date
Table1
card#
org_id
linke_dt
active_status

Table2 has transation details done by customer.

Table2
pedriod_key
card#
org_id
store#
purchase_amt

and the Requirement is :

[1] I need all the card# which are active and the link date should be between the supplid date. for example between 20090301 and 20090331 (YYYYMMDD).

[2] Also need the transaction done by the above card# between Period_key between supplied dates which is same as above.

[3] Also there might be chance that Card# might not have done any transaction between the period_key but are active and linked the date period. So i also need the card# putting the Null or 0 to the purchase amount.

I prepared a query with left outer join but it is not working.

Code:

     SELECT  A.CARD_NUM
            ,A.ORG_ID                                       
            ,COALESCE(A.LINK_SOURCE_CD,' ')                     
            ,COALESCE(B.GROUP_ID,0)                             
            ,COALESCE(B.CONTRIBUTION_AMT,0)                     
            ,COALESCE(B.PURCHASE_PTS_AMT,0)                     
            ,A.CREATE_TMSTP                                 
     FROM Table1  A LEFT OUTER JOIN                   
          Table2  B                     
     ON A.CARD_NUM= B.CARD_NUM
     WHERE  (A.CREATE_TMSTP BETWEEN                         
            '01-MAR-09' AND '31-MAR-09')
       AND  A.ACTIVE_STATUS = 'A'                             
       AND  A.ORG_ID  = B.ORG_ID                           
       AND  B.GROUP_ID IN (40299,128258)                   
       AND  (B.PERIOD_KEY BETWEEN                           
            '01-MAR-09' AND  '31-MAR-09')


Back to top
View user's profile Send private message
ashimer

Active Member


Joined: 13 Feb 2004
Posts: 551
Location: Bangalore

PostPosted: Fri Apr 17, 2009 2:34 pm
Reply with quote

remove the join condition A.ORG_ID = B.ORG_ID , your requirement does not mention anything about using this col for join .... all details are on card# right ?
Back to top
View user's profile Send private message
arvind.m

Active User


Joined: 28 Aug 2008
Posts: 205
Location: Hyderabad

PostPosted: Fri Apr 17, 2009 2:41 pm
Reply with quote

one card# can link with different org_id. So transaction for card# which is in table 1 and 2 and also the org_id matching.
Back to top
View user's profile Send private message
ashimer

Active Member


Joined: 13 Feb 2004
Posts: 551
Location: Bangalore

PostPosted: Fri Apr 17, 2009 2:45 pm
Reply with quote

as per your requirement 1 where is the date range on link_date ?
Please let us know what problem are you facing ?
Back to top
View user's profile Send private message
arvind.m

Active User


Joined: 28 Aug 2008
Posts: 205
Location: Hyderabad

PostPosted: Fri Apr 17, 2009 2:53 pm
Reply with quote

Sorry the Create_tmspt is the link date here and the other (period_key) date is transaction done date range.


Problem i'm facing:

the query is running good. But when i give the date range 20090301 and 20090331 i'm not getting anything in the result set. But when i run seperate query on table1 there are card# linked on the date range.

So i need help to modify query/or new query to get the desired result.
Back to top
View user's profile Send private message
ashimer

Active Member


Joined: 13 Feb 2004
Posts: 551
Location: Bangalore

PostPosted: Fri Apr 17, 2009 3:04 pm
Reply with quote

Try the below query

Code:

SELECT  A.CARD_NUM
            ,A.ORG_ID                                       
            ,COALESCE(A.LINK_SOURCE_CD,' ')                     
            ,COALESCE(B.GROUP_ID,0)                             
            ,COALESCE(B.CONTRIBUTION_AMT,0)                     
            ,COALESCE(B.PURCHASE_PTS_AMT,0)                     
            ,A.CREATE_TMSTP                                 
     FROM Table1  A LEFT OUTER JOIN                   
          Table2  B                     
     ON A.CARD_NUM= B.CARD_NUM
       AND  A.ORG_ID  = B.ORG_ID   

     WHERE  (A.CREATE_TMSTP BETWEEN                         
            '01-MAR-09' AND '31-MAR-09')
       AND  A.ACTIVE_STATUS = 'A'                             
                         
       AND  B.GROUP_ID IN (40299,128258)                   
       AND  (B.PERIOD_KEY BETWEEN                           
            '01-MAR-09' AND  '31-MAR-09')
                         
Back to top
View user's profile Send private message
arvind.m

Active User


Joined: 28 Aug 2008
Posts: 205
Location: Hyderabad

PostPosted: Fri Apr 17, 2009 3:10 pm
Reply with quote

no records fetched.
Back to top
View user's profile Send private message
ashimer

Active Member


Joined: 13 Feb 2004
Posts: 551
Location: Bangalore

PostPosted: Fri Apr 17, 2009 3:14 pm
Reply with quote

Check whether the date format is correct ... post the query which gave results from table A ...
Back to top
View user's profile Send private message
arvind.m

Active User


Joined: 28 Aug 2008
Posts: 205
Location: Hyderabad

PostPosted: Fri Apr 17, 2009 3:32 pm
Reply with quote

Code:

select * from Table1
where create_tmstp between '01-mar-09' and '31-mar-09' and ACTIVE_status='A'
Back to top
View user's profile Send private message
dbzTHEdinosauer

Global Moderator


Joined: 20 Oct 2006
Posts: 6966
Location: porcelain throne

PostPosted: Fri Apr 17, 2009 3:57 pm
Reply with quote

does:
Code:

SELECT *
FROM TABLE2
WHRE  PERIOD_KEY BETWEEN  '01-MAR-09' AND  '31-MAR-09'

select anything when executed?
Back to top
View user's profile Send private message
arvind.m

Active User


Joined: 28 Aug 2008
Posts: 205
Location: Hyderabad

PostPosted: Fri Apr 17, 2009 4:30 pm
Reply with quote

No there are no records selected from Table2.
Back to top
View user's profile Send private message
dbzTHEdinosauer

Global Moderator


Joined: 20 Oct 2006
Posts: 6966
Location: porcelain throne

PostPosted: Fri Apr 17, 2009 4:37 pm
Reply with quote

Quote:
No there are no records selected from Table2.


well since that is line from your sql,
maybe therein lies the problem
Back to top
View user's profile Send private message
arvind.m

Active User


Joined: 28 Aug 2008
Posts: 205
Location: Hyderabad

PostPosted: Fri Apr 17, 2009 4:40 pm
Reply with quote

Yes. But my requirement is that if Card# is linked in the date range and have not done any transaction should also come in the result set. For this reason i used LEFT OUTER JOIN.

Can any please provide me a SQL with my requirement mention above.
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 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
No new posts Issue with EXEC CICS QUERY SECURITY c... CICS 6
Search our Forums:

Back to Top