我有这张桌子
反式
id
user_id
amt
desc
bal
created
样本数据是
id user_id desc amt bal created
1 1 credit 12.00 12.00 2013-07-02 00:00:00
2 1 credit 44.00 56.00 2013-07-09 00:00:00
3 1 debit 11.00 45.00 2013-07-18 00:00:00
4 2 credit 11.00 11.00 2013-07-12 01:17:08
5 2 credit 45.00 56.00 2013-07-13 01:17:08
6 2 debit 15.00 41.00 2013-07-14 01:17:08
7 3 credit 33.00 33.00 2013-07-15 01:17:52
8 3 credit 45.00 78.00 2013-07-16 01:17:52
9 3 debit 25.00 53.00 2013-07-17 01:17:52
现在我只需要从每个用户中选择当前余额,所以 outout 应该 输出 id user_id desc amt bal created 3 1 debit 11.00 45.00 2013-07-18 00:00:00 6 2 debit 15.00 41.00 2013-07-12 01 :17:08 9 3 学分 25.00 53.00 2013-07-12 01:17:52
这是我试过的
SELECT *
FROM trans
WHERE created
IN (
SELECT MAX( created )
FROM trans
GROUP BY user_id
)
这为每个用户提供了两条记录,而不是每条记录。我究竟做错了什么?
目标是选择最高金额(最高和递减)
如果两个日期和时间相同,则可以,但每个不返回两条记录