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
 

 

SQL Query help

 
Post new topic   Reply to topic    IBMMAINFRAMES.com Support Forums -> DB2
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    Post subject: SQL Query help
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    Post subject:
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    Post subject:
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    Post subject:
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    Post subject:
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    Post subject:
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    Post subject:
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    Post subject:
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    Post subject:
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    Post subject:
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    Post subject:
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    Post subject:
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    Post subject:
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    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 Join in SQL Query vickey_dw DB2 2 Tue Jan 17, 2017 12:15 am
This topic is locked: you cannot edit posts or make replies. MQ setup query mohitsaini Java & MQSeries 4 Sun Jan 08, 2017 1:53 am
No new posts DB2 query Using Recursion, Converting... smilewithashu2 DB2 1 Tue Jan 03, 2017 12:50 pm
No new posts SQL query to run through list of valu... Ni3-db2 DB2 14 Wed Dec 14, 2016 9:52 am
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


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