Results 1 to 3 of 3
  1. #1
    Join Date
    May 2006
    Posts
    16

    Unanswered: 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 ?

  2. #2
    Join Date
    Feb 2008
    Location
    Japan
    Posts
    3,483
    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
    ;

  3. #3
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •