如何从信用卡和借记列

时间:2017-02-13 06:24:12

标签: sql-server-2008

enter image description here 我使用以下查询来计算运行余额。它对我来说非常合适。我需要日期明智的报告看起来像输出2。

Select  
t2.tentryno,t2.tseqid,t2.glcode,t2.descript,t2.Debit,t2.Credit,t2.entrydate,
(Select SUM(Debit-Credit) From balancesheet As t1 Where 
t1.glcode=t2.glcode  and t1.tseqid  <= t2.tseqid 
)as Amount
 From balancesheet  As t2 where glcode='01-04-0003'
 order by glcode,tseqid,entrydate

表示输出1和输出2 plz检查图像链接。

Plz帮帮我

1 个答案:

答案 0 :(得分:1)

请试试这个:

Select
  t.tentryno,t.tseqid,t.glcode,t.descript,t.Debit,t.Credit,t.entrydate, 
  (Select SUM(x.Debit-x.Credit) From balancesheet x 
    Where x.glcode = t.glcode 
    and (
       x.entrydate < t.entrydate 
       or 
       (x.entrydate = t.entrydate and x.tentryno <= t.tentryno))
  ) as Amount 
From balancesheet As t
where t.glcode = '01-04-0003' 
order by t.glcode, t.entrydate, t.tentryno

由于您没有提供架构和数据,我必须在我的测试系统中执行此操作,因此您可能会发现一些&#34;拼写错误&#34;。