2017-03-15 158 views
-1

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 

result

+0

Are you sure 66,80 + 452,00 = 548,80 ??在什麼基地? – mathguy

+0

然後:請閱讀並按照說明進行操作。 https://stackoverflow.com/help/mcve如果你的問題是關於某一列的累計總和,爲什麼我們需要閱讀一個四十行查詢,有三個連接的表,誰知道有多少列?如果您需要幫助,請先做好自己的工作。 – mathguy

+0

@mathguy抱歉,其482,00而不是452,00。 我不知道我怎麼可以更清楚我的懷疑沒有發佈整個SQL查詢。 – kelsen

回答

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分析查詢以獲取更多幫助。祝你好運。 -Tom

+0

SUM(SUM(D.VL_SALDOTOTAL + DECODE(B.TP_OPERACAO,' C',DECODE( B.TP_DOCUMENTO,'3',B.VL_LANCTO,0),0)) - SUM(DECODE(B. (B.TP_OPERACAO,'D',DECODE(B.TP_DOCUMENTO,'9',0)) - SUM(DECODE(B.TP_OPERACAO,'D',DECODE(D.TP_OPERACAO,'D' B.VL_LANCTO,0),0)))和VL_SALDOTOTAL一樣,它實際上是累積VL_SALDOTOTAL,但是這個問題也是你提到的子查詢的D.VL_SALDOTOTAL是482的固定值,並且每一行都被再次添加。 – kelsen