0

这是我第一次使用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'

我想要的结果和结果:

在此处输入图像描述

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

没有数据,

我只希望它有数据,新行包含上一期期初余额的 acct_cd,因此新行数据仅包含上期期初余额的 acct_cd,期初余额 = 0,期末余额 = 期初余额

有人可以帮忙吗?我真的很感谢你的关注。

4

0 回答 0