我如何计算SQL中的运行余额

时间:2017-03-21 06:29:01

标签: sql derby running-total

我正在使用嵌入式德比数据库,我想通过计算借方和贷方金额来添加运行余额列,所以请告诉我解决方案代码和图片也可以在下面找到

(正如您在图像平衡中看到的总计不准确)

SELECT 
V_DATE,FLAG,V_NUM,V_NARATION,
sum(VDTL.DR_AMOUNT) AS DEBIT,
sum(VDTL.CR_AMOUNT) AS CRIDIT,
sum(dr_amount)-sum(cr_amount) as total
FROM VOUCHARDETAIL AS VDTL
INNER JOIN VOUCHARMASTER AS VMST
ON VDTL.DTL_NUM =VMST.MST_NUM
WHERE (V_DATE BETWEEN '12-03-2017' AND '14-03-2017') AND (AC_CODE=60030002) 
GROUP BY
V_DATE,FLAG,V_NUM,V_NARATION
ORDER BY
V_DATE

enter image description here

2 个答案:

答案 0 :(得分:0)

这可能是一种可能的解决方案。

<p:commandButton value="Cancel" onclick="PF('wizard').loadStep(PF('wizard').cfg.steps[0], true);"
oncomplete="PF('wizard').hide(); />

答案 1 :(得分:0)

试试这个)

 select sel.*, sel.total + LAG(sel.total,1,0) over (order by Sel.V_DATE) as running_balance from (SELECT 
    V_DATE,FLAG,V_NUM,V_NARATION,
    sum(VDTL.DR_AMOUNT) AS DEBIT,
    sum(VDTL.CR_AMOUNT) AS CRIDIT,
    sum(dr_amount)-sum(cr_amount) as total,
    FROM VOUCHARDETAIL AS VDTL
    INNER JOIN VOUCHARMASTER AS VMST
    ON VDTL.DTL_NUM =VMST.MST_NUM
    WHERE (V_DATE BETWEEN '12-03-2017' AND '14-03-2017') AND (AC_CODE=60030002) 
    GROUP BY
    V_DATE,FLAG,V_NUM,V_NARATION
    ORDER BY
    V_DATE) Sel
    order by Sel.V_DATE