If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > Last transaction by customer and account

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 07-06-09, 22:08
arlf arlf is offline
Registered User
 
Join Date: May 2006
Posts: 16
Last transaction by customer and account

TRANS_TBL (Customer Transaction Table) :
Cust_Id, Acct_Id, Trans_Id, Trans_Amount, ... , Trans_Date, Trans_Time
(table has no TimeStamp col)

I want to select the last transaction (based on Trans_Date and Trans_Time) for each customer and account.
A customer can have several accounts.

Any help ?
Reply With Quote
  #2 (permalink)  
Old 07-06-09, 23:48
tonkuma tonkuma is offline
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,193
Code:
SELECT Cust_Id, Acct_Id, Trans_Id, Trans_Amount
     , ... 
     , Trans_Date, Trans_Time
  FROM (SELECT t.*
             , ROW_NUMBER()
               OVER(PARTITION BY Cust_Id, Acct_Id
                    ORDER BY Trans_Date DESC, Trans_Time DESC) rn
          FROM TRANS_TBL t
       ) s
 WHERE rn = 1
;
Reply With Quote
  #3 (permalink)  
Old 07-08-09, 15:18
arlf arlf is offline
Registered User
 
Join Date: May 2006
Posts: 16
Thanks so much, Tonkuma. It was used like a subselect to extract data from a join with others tables.
saludos, ALRF.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On