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: 6968
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: 6968
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 Need DB2 query to fetch previous row ! Chandan1993 DB2 10 Sat Jun 03, 2017 10:43 am
No new posts JCL to Set Return code based on DB2 S... vinu78 DB2 17 Mon Mar 13, 2017 9:47 pm
No new posts SQL query not working in Cobol program. CuriousMainframer COBOL Programming 14 Wed Feb 22, 2017 5:56 pm
No new posts Omegamon for DB2 query ashek15 DB2 0 Wed Feb 15, 2017 11:25 am
No new posts Query on DB2 WLM stored procedures ashek15 DB2 8 Sun Feb 12, 2017 11:13 pm


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