[英]How to select LAST record in group by using mysql
I am trying to select the LAST of the record in group by in sql.我正在尝试在 sql 中选择 group by 中记录的最后一个。 In my case I use this code就我而言,我使用此代码
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
and the data of the selected column is this所选列的数据是这样的
so I basically trying to get the last balance amount.所以我基本上试图获得最后的余额。 how can i select the last amount in sql?如何在sql中选择最后一个金额?
the whole table content here : tbl_ledger整个表格内容在这里:tbl_ledger
The following query gives you the record that has the latest trans_date
for each c_id
:以下查询为您提供每个c_id
具有最新trans_date
的记录:
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
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.