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
 

 

Query to retrieve only the first 2 rows of distinct accounts

 
Post new topic   Reply to topic    IBMMAINFRAMES.com Support Forums -> DB2
View previous topic :: :: View next topic  
Author Message
annu zacson

New User


Joined: 19 Aug 2008
Posts: 15
Location: Cochin

PostPosted: Tue Nov 22, 2011 3:44 pm    Post subject: Query to retrieve only the first 2 rows of distinct accounts
Reply with quote

Hi,

I have a query to retrieve the card members from a table in the sorted order as specified below. I would like to re-iterate the query so that the SQL returns only the first 2 rows of distinct card accounts.
The query As is :

Code:
    SELECT                                                 
             ACCT_NO,                                               
             CM_SUPP_NO,                                             
             HIST_UPD_DT_TM_DA                                       
        FROM  C0B.T3333 A                                           
        WHERE ISO_CTRY_ALPH3_CD      = 'NZD'                         
        AND CARD_ACCT_NO IN ( SELECT DISTINCT CARD_ACCT_NO           
        FROM C0B.T3333 B                                             
        WHERE                                                       
        (       CB_ACC_STAT_CD       = 'E' OR                       
                CAIS_STATUS_CD       = 'F' OR                       
                RGSTRY_DA_FLAG_CD  = 'E' ) AND                       
        DAY  (HIST_UPD_DT_TM_DA)  = 17           AND                 
        MONTH(HIST_UPD_DT_TM_DA)  = 11           AND                 
        YEAR (HIST_UPD_DT_TM_DA)  = 2011   AND                       
        A.UPD_DT_TM_DA  = B.UPD_DT_TM_DA)                           
        ORDER BY CARD_ACCT_NO,HIST_UPD_DT_TM_DA DESC                 
        ;   

Result:

Code:
Card1   E   updt TS   -- row 1
Card1   E   updt TS   -- row 2
card1   E   updt TS   -- row 3
Card2   E   updt TS   -- row 4
Card2   E   updt TS   -- row 5
Card2   E   updt TS   -- row 6


Result required after the query change:

Code:
Card1   E   updt TS   -- row 1
Card1   E   updt TS   -- row 2
Card2   E   updt TS   -- row 4
Card2   E   updt TS   -- row 5

Could you please help to write a query so that the result will get populated as mentioned above?
Regards,
Annu
Back to top
View user's profile Send private message

GuyC

Senior Member


Joined: 11 Aug 2009
Posts: 1278
Location: Belgium

PostPosted: Tue Nov 22, 2011 4:38 pm    Post subject:
Reply with quote

what the following query does is :
1) Get all distinct Card_acct_no with some flags that are updated on a certain day => Table B2
2) for those cards get the latest two updated rows in 'NZD'
Code:
select a2.* from
( SELECT CARD_ACCT_NO
FROM C0B.T3333 B
WHERE ( CB_ACC_STAT_CD = 'E' OR
        CAIS_STATUS_CD = 'F' OR
        RGSTRY_DA_FLAG_CD = 'E' )
  AND hIST_UPD_DT_TM_DA between '2011-11-17-00.00.00.000000' and '2011-11-17-23.59.99.999999'
  group by card_acct_no) B2
, table (
SELECT ACCT_NO, CM_SUPP_NO, HIST_UPD_DT_TM_DA
FROM C0B.T3333 A
WHERE ISO_CTRY_ALPH3_CD = 'NZD'
  and A.CARD_ACCT_NO = B.CARD_ACCT_NO
ORDER BY CARD_ACCT_NO,HIST_UPD_DT_TM_DA DESC
fetch first 2 rows only) A2
Back to top
View user's profile Send private message
annu zacson

New User


Joined: 19 Aug 2008
Posts: 15
Location: Cochin

PostPosted: Wed Nov 23, 2011 9:11 am    Post subject:
Reply with quote

Hi,

The query worked. Thanks a lot!!

Regards,
Annu
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