http://expert.csdn.net/Expert/topic/2609/2609577.xml?temp=.3955652
请看看此贴 能有点提示

解决方案 »

  1.   

    我用的是DELPHI+SQL 不知道这个SQL怎么写了
      

  2.   

    要做到楼主的这一点,前提是你要根据一个字段来排出记录的先后顺序,假设是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) 
      

  3.   

    至于前面的union相信难不倒你的。