假设“试算表 1617”是一个汇总表,在 GL 帐户代码和文档日期上具有唯一索引,您可以手动计算导致特定条目的借方和贷方总和,如下所示:
create table "Trial Balance 1617" ("Document Date" date, "GL Account Code" varchar(25),
"Debit Amount" numeric(12,2), "Credit Amount" numeric(12,2))
create table "GL Master Account" ("GL Account Code" varchar(25), "Opening Balance" numeric(12,2))
insert into "GL Master Account" values ('A3010101B058', '8110339.14')
insert into "Trial Balance 1617" values ('4/1/2016', 'A3010101B058', 5332269.28, 0)
insert into "Trial Balance 1617" values ('4/2/2016', 'A3010101B058', 741674.9, 0)
insert into "Trial Balance 1617" values ('4/4/2016', 'A3010101B058', 570253.96, 0)
insert into "Trial Balance 1617" values ('4/5/2016', 'A3010101B058', 0, -148839.52)
SELECT
t1."Document Date" as txn_dt,
t1."GL Account Code" as gl_code,
t1."Debit Amount" as debit,
t1."Credit Amount" as credit,
case
when not exists
(select * from "Trial Balance 1617" where "GL Account Code" = t1."GL Account Code" and "Document Date" < t1."Document Date")
then t2."Opening Balance"
else
t2."Opening Balance" + (select sum("Debit Amount") from "Trial Balance 1617" where "GL Account Code" = t1."GL Account Code" and "Document Date" < t1."Document Date")
- (select sum("Credit Amount") from "Trial Balance 1617" where "GL Account Code" = t2."GL Account Code" and "Document Date" < t1."Document Date")
- (select sum("Credit Amount") from "Trial Balance 1617" where "GL Account Code" = t2."GL Account Code" and "Document Date" < t1."Document Date")
end as op_bal,
case
when not exists
(select * from "Trial Balance 1617" where "GL Account Code" = t1."GL Account Code" and "Document Date" < t1."Document Date")
then t2."Opening Balance"
else
t2."Opening Balance" + (select sum("Debit Amount") from "Trial Balance 1617" where "GL Account Code" = t1."GL Account Code" and "Document Date" < t1."Document Date")
- (select sum("Credit Amount") from "Trial Balance 1617" where "GL Account Code" = t2."GL Account Code" and "Document Date" < t1."Document Date")
- (select sum("Credit Amount") from "Trial Balance 1617" where "GL Account Code" = t2."GL Account Code" and "Document Date" < t1."Document Date")
end + t1."Debit Amount" + t1."Credit Amount" as closing_bal
FROM "Trial Balance 1617" t1
INNER JOIN "GL Master Account" t2 ON t1.[GL Account Code] = t2.[GL Account Code]
WHERE t1."GL Account Code" = 'A3010101B058'
返回:
txn_dt gl_code debit credit op_bal closing_bal
2016-04-01 A3010101B058 5332269.28 0.00 8110339.14 13442608.42
2016-04-02 A3010101B058 741674.90 0.00 13442608.42 14184283.32
2016-04-04 A3010101B058 570253.96 0.00 14184283.32 14754537.28
2016-04-05 A3010101B058 0.00 -148839.52 14754537.28 14605697.76
如果你可以使用 CTE,你可以通过在单独的查询中计算期初余额来清理它:
;with cte_op_balance as
(
SELECT
t1."Document Date" as txn_dt,
t1."GL Account Code" as gl_code,
case
when not exists
(select * from "Trial Balance 1617" where "GL Account Code" = t1."GL Account Code" and "Document Date" < t1."Document Date")
then t2."Opening Balance"
else
t2."Opening Balance" + (select sum("Debit Amount") from "Trial Balance 1617" where "GL Account Code" = t1."GL Account Code" and "Document Date" < t1."Document Date")
- (select sum("Credit Amount") from "Trial Balance 1617" where "GL Account Code" = t2."GL Account Code" and "Document Date" < t1."Document Date")
- (select sum("Credit Amount") from "Trial Balance 1617" where "GL Account Code" = t2."GL Account Code" and "Document Date" < t1."Document Date")
end as op_bal
FROM "Trial Balance 1617" t1
INNER JOIN "GL Master Account" t2 ON t1.[GL Account Code] = t2.[GL Account Code]
WHERE t1."GL Account Code" = 'A3010101B058'
)
SELECT
t1."Document Date" as txn_dt,
t1."GL Account Code" as gl_code,
t1."Debit Amount" as debit,
t1."Credit Amount" as credit,
c.op_bal,
c.op_bal + t1."Debit Amount" + t1."Credit Amount" as closing_bal
FROM "Trial Balance 1617" t1
INNER JOIN "GL Master Account" t2 ON t1.[GL Account Code] = t2.[GL Account Code]
INNER JOIN cte_op_balance c ON t1.[GL Account Code] = c.gl_code and t1.[Document Date] = c.txn_dt
WHERE t1."GL Account Code" = 'A3010101B058'