D.VL_SALDOTOTAL等于452,00。我希望SALDOTOTAL累积如下图所示:66,80 + 482,00 = 548,80 - > 107,00 + 548,80 = 655,80,但每行总计SALDODIA + 482,00。这是可能的简单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
答案 0 :(得分:2)
这是一个厚厚的阅读,但我认为你想要的是
sum(sum_column) over ([PARTITION BY group_column(s)] [order by order_column(s)]).
我认为您应该停止尝试在现有查询中累积总计,然后将其作为子查询括在括号中。从巨大的子查询中选择所需的列,并在列旁边添加您希望保持运行总计的列:
sum(COLUMN_OF_VALUES_THAT_NEED_TO_BE_CUMULATIVELY_TOTALED) over
(order by DT_MOVIM, TP_OPERACAO) as VL_SALDOTOTAL
我认为,即使这个特定表达式不起作用,这也是您正在寻找的功能。研究Oracle分析查询以获得更多帮助。祝你好运。 - 汤姆