SQL查询期初余额变为结算余额

时间:2013-10-03 06:33:34

标签: sql sql-server

这是我第一次使用stackoverflow,

我有查询问题, 有4个表(左连接)

按年份和期间过滤,还有帐户代码。

这是查询:

select a.fyear,a.aperiod,c.trx_amt
,e.acct_cd,e.active_status

,ISNULL((select 
openingBalance=SUM(a.trx_amt)
from 
gl_pendjnls a
left join gl_jlhdr b on a.idxjlhdr = b.idx
left join v_acctperiod c on b.idxperiod = c.idx
right join v_gl_chart d on  a.idxcoa = d.idx

where c.fyear < = 2013 and c.aperiod < 10
and d.acct_cd = e.acct_cd
group by d.acct_cd
   ),0) as openingBalance

,ISNULL((select 
closingBalance=SUM(a.trx_amt) + c.trx_amt

from 
gl_pendjnls a
left join gl_jlhdr b on a.idxjlhdr = b.idx
left join v_acctperiod f on b.idxperiod = f.idx
left join v_gl_chart d on  a.idxcoa = d.idx

where f.fyear < = 2013 and f.aperiod < 10
and d.acct_cd = e.acct_cd
group by d.acct_cd
   ),0) as closingBalance

from v_acctperiod a
left join gl_jlhdr b on a.idx = b.idxperiod
left join gl_pendjnls c on b.idx = c.idxjlhdr
left join v_gl_chart e on c.idxcoa = e.idx
where e.active_status = 'Y'

and a.fyear = 2013 and a.aperiod = 10
and e.acct_cd = '111-01-201'

我想要的结果和结果:

enter image description here

如果我将a.fyear更改为= 2013和a.period = 11并删除e.acct_cd ='111-01-201'

没有数据,

我希望它有数据, 包含上一期间期初余额的acct_cd的新行, 所以新行数据只包含上一期间期初余额的acct_cd,opensbalance = 0,期初余额=期末余额

有人可以帮忙吗?我非常关注你的注意力。

0 个答案:

没有答案