用分组查询并结合sum、max聚合函数来一句语句就ok了:select a.cardno,sum(b.money) as 总金额,max(b.time) as 最新刷卡时间 from employee as a,balance200411 as b where a.cardno=b.cardno group by a.cardno
试下这个吧: select cardno,max(time),(select sum(money1) from Balance200411 as a where not exists (select * from Balance200411 where cardno=a.cardno and time>a.time)) from Balance200411 group by cardno
FROM Balance200411
GROUP BY CardNo
ORDER BY CardNo这样可以取得 balance200411中每个卡的最新数据,但我想同时也显示money字段时,却提醒money没有包含在聚合中,有什么办法吗?谢谢了
9 9 9:1 2
10 1 9:01 3
10 2 10:01 4我分别取得卡号对应的最新 MONEY,然后把所有取得的MONEY累加如上例 把 充号为 1 及4的MONEY累加谢谢您的回复
select cardno,max(time),(select sum(money1) from Balance200411 as a where not exists (select * from Balance200411 where cardno=a.cardno and time>a.time)) from Balance200411 group by cardno