D.VL_SALDOTOTAL等於452,00。我希望SALDOTOTAL的累積效果如下圖所示:66,80 + 482,00 = 548,80 - > 107,00 + 548,80 = 655,80,但它總計SALDODIA + 482,00每排。這是可能的簡單的SQL或我需要一個存儲過程?Oracle sql累計結果
select B.NR_CTAPES, A.DS_TITULAR, B.TP_MANUTENCAO as DS_MANUTENCAO, B.DT_MOVIM, B.TP_DOCUMENTO as DS_DOCUMENTO, B.TP_OPERACAO as DS_OPERACAO, B.NR_NF, B.VL_LANCTO, B.CD_HISTORICO as DS_HISTORICO, (SUM (DECODE(B.TP_OPERACAO,'C', DECODE(
B.TP_DOCUMENTO, '3', B.VL_LANCTO, 0),0)) - SUM (DECODE(B.TP_OPERACAO,'D', DECODE(
B.TP_DOCUMENTO, '3', B.VL_LANCTO, 0),0)) - SUM (DECODE(B.TP_OPERACAO,'D', DECODE(B.TP_DOCUMENTO, '9', B.VL_LANCTO, 0),0))) as VL_SALDODIA, COALESCE(D.VL_SALDOTOTAL + (SUM (DECODE(B.TP_OPERACAO,'C', DECODE(B.TP_DOCUMENTO, '3', B.VL_LANCTO,
0),0)) - SUM (DECODE(B.TP_OPERACAO,'D', DECODE(
B.TP_DOCUMENTO, '3', B.VL_LANCTO, 0),0)) - SUM (DECODE(B.TP_OPERACAO,'D', DECODE(B.TP_DOCUMENTO, '9', B.VL_LANCTO, 0),0))),0) as VL_SALDOTOTAL
FROM VR_FCC_CTAPES A
LEFT OUTER JOIN VR_FCC_MOVLIQ B ON
B.NR_CTAPES = A.NR_CTAPES
AND B.TP_MANUTENCAO = A.TP_MANUTENCAO
AND B.CD_EMPRESA = A.CD_EMPRESA
LEFT OUTER JOIN (
select (SUM (DECODE(C.TP_OPERACAO,'C', DECODE(C.TP_DOCUMENTO, '3', C.VL_LANCTO, 0),0)) - SUM (DECODE(C.TP_OPERACAO,'D', DECODE(C.TP_DOCUMENTO, '3', C.VL_LANCTO, 0),0)) - SUM (DECODE(C.TP_OPERACAO,'D', DECODE(C.TP_DOCUMENTO, '9',
C.VL_LANCTO, 0),0))) as
VL_SALDOTOTAL, C.NR_CTAPES, C.TP_DOCUMENTO, C.TP_MANUTENCAO, C.CD_EMPRESA, C.DT_MOVIM, C.CD_HISTORICO
FROM VR_FCC_MOVLIQ C
WHERE C.IN_ESTORNO ='F'
GROUP BY C.NR_CTAPES, C.TP_DOCUMENTO, C.TP_MANUTENCAO, C.CD_EMPRESA, C.DT_MOVIM, C.CD_HISTORICO
) D ON
D.NR_CTAPES = B.NR_CTAPES
AND D.TP_DOCUMENTO = B.TP_DOCUMENTO
AND D.TP_MANUTENCAO = B.TP_MANUTENCAO
AND D.CD_EMPRESA = B.CD_EMPRESA
AND D.DT_MOVIM = TO_DATE(@DT_INICIAL) - 1
AND D.CD_HISTORICO = B.CD_HISTORICO
WHERE
B.TP_MANUTENCAO IN ('4','6')
AND B.TP_DOCUMENTO IN ('3','9')
AND B.IN_ESTORNO ='F'
AND B.CD_HISTORICO NOT IN ('77','835','836','840','75','857','837','830','855','856','833','832','838','1074','1073')
AND B.CD_EMPRESA IN ('1','2')
AND B.DT_MOVIM between @DT_INICIAL and @DT_FINAL
GROUP BY B.NR_CTAPES, A.DS_TITULAR, B.TP_MANUTENCAO, B.DT_MOVIM, B.TP_DOCUMENTO, B.TP_OPERACAO, B.NR_NF, B.VL_LANCTO, B.CD_HISTORICO, D.VL_SALDOTOTAL
ORDER BY B.DT_MOVIM, B.TP_OPERACAO
Are you sure 66,80 + 452,00 = 548,80 ??在什麼基地? – mathguy
然後:請閱讀並按照說明進行操作。 https://stackoverflow.com/help/mcve如果你的問題是關於某一列的累計總和,爲什麼我們需要閱讀一個四十行查詢,有三個連接的表,誰知道有多少列?如果您需要幫助,請先做好自己的工作。 – mathguy
@mathguy抱歉,其482,00而不是452,00。 我不知道我怎麼可以更清楚我的懷疑沒有發佈整個SQL查詢。 – kelsen