How to make a SQL query for last transaction of every account?

前端 未结 4 1007
心在旅途
心在旅途 2020-12-28 08:59

Say I have a table \"transactions\" that has columns \"acct_id\" \"trans_date\" and \"trans_type\" and I want to filter this table so that I have just the last transaction f

4条回答
  •  南方客
    南方客 (楼主)
    2020-12-28 09:33

    select t.acct_id, t.trans_type, tm.trans_date
    from transactions t
    inner join (
        SELECT acct_id, max(trans_date) as trans_date  
        FROM transactions 
        GROUP BY acct_id;
    ) tm on t.acct_id = tm.acct_id and t.trans_date = tm.trans_date
    

提交回复
热议问题