非常感谢你帮我解决的一个问题
http://topic.csdn.net/u/20080611/14/3b430866-2592-4267-9ea6-a2f41ebbac7e.html
还是有一个问题还是解决不了,望能帮解决.先谢了.
这是上一贴的结果/*结果--------------------------------------------------------------------------
月    日    科目编码        摘要       借方金额        贷方金额        方向   余额          
---- ---- ----------- -------- ----------- ----------- ---- ----------- 
                 1001 上期结转     NULL        NULL        借            700 
03   01          1001 职员借款     NULL                200 借            500 
03   01          1001 差旅费       NULL                100 借            400
03               1001 本期合计     NULL                300 借           -300 
                 1001 当前累计     NULL        NULL        借            400 --清除------------------------------------------------------------------------*/本期合计及当前累计都没有问题<我指的是在我的实际数据中,行数非常多,你给的例子没有出现该问题>,有时会出现借位,也就是如上例的"职员借款"和"差旅费"的余额不对位,如职员借款的变成400而差旅费为500,也不是每次都出现.

解决方案 »

  1.   

    嗯,改改:
    /******************************************************************************/
    /*回复:20080612001总:00069                                                   */
    /*主题:会计平衡                                                                          */
    /*作者:二等草                                                                            */
    /******************************************************************************/set nocount on--数据--------------------------------------------------------------------------
     
    declare @t table([日期] datetime,[摘要] varchar(8),[科目ID] int,[科目编码] int,[科目名称] varchar(8),[余额方向] varchar(2),[借方金额] int,[贷方金额] int)
     insert into @t select '2008-1-1','销售收款',1,1001,'现金','借',900,null
     insert into @t select '2008-1-2','进货付款',1,1001,'现金','借',null,300 
     insert into @t select '2008-2-1','职员还款',1,1001,'现金','借',100,null
     insert into @t select '2008-3-1','职员借款',1,1001,'现金','借',null,100
     insert into @t select '2008-3-1','差旅费',1,1001,'现金','借',null,200
     insert into @t select '2008-3-1','职员借款',1,1001,'现金','借',null,300
     insert into @t select '2008-3-1','....',2,1002,'银行存款','借',1000,null
     insert into @t select '2008-3-1','....',3,2121,'应付账款','贷',null,1200--代码--------------------------------------------------------------------------
    declare @qcye int,@km int,@qmye int
    declare @bd datetime,@ed datetime
    select @km = 1001,@bd = '2008-03-01',@ed = '2008-03-31'select @qcye = isnull(sum(借方金额)-sum(贷方金额),0) from @t where 科目编码= @km and 日期 < @bd
    create table #(id int identity,月 char(2),日 char(2),科目编码 int
                   ,摘要 varchar(8),借方金额 int,贷方金额 int,方向 char(2),余额 int)
    insert #(月,日,科目编码,摘要,借方金额,贷方金额,方向,余额)
           select 月=right(month(日期)+100,2),日=right(day(日期)+100,2),科目编码,摘要,借方金额,贷方金额,方向='借',余额=0
           from @t where 科目编码=@km and 日期 between @bd and @ed
           order by 日期select @qmye = @qcyeupdate # set 余额=@qmye,@qmye=isnull(借方金额,0)-isnull(贷方金额,0)+@qmyeinsert # select '','',@km,'上期结转',null,null,'借',@qcyeinsert # select right(month(@ed)+100,2),'',@km,'本期合计',sum(借方金额),sum(贷方金额),'借',isnull(sum(借方金额),0)-isnull(sum(贷方金额),0)
             from # insert # select '','',@km,'当前累计',null,null,'借',@qmye
    select * from # order by 
     case 摘要 when '上期结转' then 1
               when '本期合计' then 3
               when '当前累计' then 4
               else  2 
     end,iddrop table #go/*结果--------------------------------------------------------------------------
    月    日    科目编码        摘要       借方金额        贷方金额        方向   余额          
    ---- ---- ----------- -------- ----------- ----------- ---- ----------- 
                     1001 上期结转     NULL        NULL        借            700 
    03   01          1001 职员借款     NULL                200 借            500 
    03               1001 本期合计     NULL                200 借           -200 
                     1001 当前累计     NULL        NULL        借            500 --清除------------------------------------------------------------------------*/
      

  2.   

    update # set 余额=@qmye,@qmye=isnull(借方金额,0)-isnull(贷方金额,0)+@qmye学习了,感觉MSSQL优势就在这里,oracle就没有这样的处理,循环处理变量
      

  3.   

    按你的思路再加这个就没有问题了
    CONSTRAINT [PK_ByTempSubjectList] PRIMARY KEY CLUSTERED 
       ([AutID] ASC) ON [PRIMARY]) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]