sql 分组取最大最小数和sum数据.
【SQL精彩语句】按某一字段分组取最大(小)值所在行的数据
自己的sql. group by 默认是取第一条的.
全量方案:
select a.balance,summ.total from acount a join (select f.帐户表_id, sum(money) total from 帐户表_流水表 f join (select 帐户表_id from 帐户表_流水表 where balance is not NULL order by id desc limit 10) idt on f.帐户表_id=idt.帐户表_id group by f.帐户表_id ) summ on a.id=summ.帐户表_id where a.balance !=summ.total;基于某行之后的方案: 基于group by获取的是第一条数据.
select tt.count,a.id,tt.帐户表_id,a.balance,tt.balance1+money from 帐户表 a join (select count(1) as count, sum(money) as money,balance-money as balance1 ,帐户表_id from 帐户表_流水表 where id > 1386401935 and balance is not NULL group by 帐户表_id limit 111000) tt on a.id=tt.帐户表_id and tt.count >1 and tt.balance1+money !=a.balance ;