我正在尝试在sql中选择group by中的记录的LAST。 就我而言,我使用此代码
SELECT c_id,Max(transaction_num),Max(trans_date) trans_date,doc_type,amount as balance
from tbl_ledger
where doc_type = 'B'
group
by c_id
所以我基本上试图获得最后的余额。如何选择sql中的最后一个金额?
以下查询为您提供了每个trans_date
具有最新c_id
的记录:
select t.*
from tbl_ledger t
where
doc_type = 'B'
and trans_date = (
select max(t1.trans_date)
from tbl_ledger t1
where t1.doc_type = 'B' and t1.c_id = t.c_id
)