要做到楼主的这一点,前提是你要根据一个字段来排出记录的先后顺序,假设是ID1,则 select case when 借方+贷方=0 then '初期值' when 借方=0 and 贷方>0 then '初期-贷' when 借方>0 and 贷方=0 then '初期+借' when 借方>0 and 贷方>0 then '初期+借-贷' end as 判断余额, 借方,贷方,(select sum(借方-贷方) from table1 where id1<t1.id1)+t2.初始值 as 余额 from table1 t1 left join (select 初始值 from table2)
select case when 借方+贷方=0 then '初期值'
when 借方=0 and 贷方>0 then '初期-贷'
when 借方>0 and 贷方=0 then '初期+借'
when 借方>0 and 贷方>0 then '初期+借-贷' end as 判断余额,
借方,贷方,(select sum(借方-贷方) from table1 where id1<t1.id1)+t2.初始值 as 余额 from table1 t1 left join (select 初始值 from table2)