Subject | Re: [firebird-support] How write a query with a progressive sum field |
---|---|
Author | Luigi Siciliano |
Post date | 2016-03-07T17:13:48Z |
Il 07/03/2016 17.28, Thomas Steinmaurer ts@...
[firebird-support] ha scritto:
select
DT.DATA,
DT.DOCUMENTO_ID,
DT.NUMERO,
DT.SERIE,
DC.CARICO,
DC.SCARICO,
SUM(dc2.carico - DC2.SCARICO) AS saldo
from
DOC_TESTA DT
JOIN DOC_CORPO DC on DT.ID = DC.DOC_TESTA_ID
left JOIN DOC_CORPO DC2 ON (DC.DOC_TESTA_ID >= DC2.DOC_TESTA_ID)
AND
(DC.ARTICOLO_ID = DC2.ARTICOLO_ID)
WHERE
DC.ARTICOLO_ID = :ID
GROUP BY
DATA,
DOCUMENTO_ID,
NUMERO,
SERIE
ORDER BY
DATA,
DOCUMENTO_ID,
NUMERO,
SERIE
but I have an issue:
if I have a row with CARICO = 0 and SCARICO = 0, SALDO is added or
subtracted by the number present in last row with
CARICO > 0 or SCARICO > 0
if I add "(AND(DC.CARICO + DC.SCARICO) > 0) IN a LEFT JOIN I obtain
almost correct result but the rows that contain "CARICO = 0 and SCARICO
= 0" now contain SALDO = NULL, the others contains correct progressive sum.
What I'm wrong?
Thanks
--
<pre>
Luigi Siciliano
--------------------------
</pre>
--
<pre>
Luigi Siciliano
--------------------------
</pre>
[firebird-support] ha scritto:
> Although not that flexible as an ad-hoc query, but perhaps yourThank you Thomas, I modified my query like this:
> particular use case can be achieved with a selectable stored procedure
> ...
select
DT.DATA,
DT.DOCUMENTO_ID,
DT.NUMERO,
DT.SERIE,
DC.CARICO,
DC.SCARICO,
SUM(dc2.carico - DC2.SCARICO) AS saldo
from
DOC_TESTA DT
JOIN DOC_CORPO DC on DT.ID = DC.DOC_TESTA_ID
left JOIN DOC_CORPO DC2 ON (DC.DOC_TESTA_ID >= DC2.DOC_TESTA_ID)
AND
(DC.ARTICOLO_ID = DC2.ARTICOLO_ID)
WHERE
DC.ARTICOLO_ID = :ID
GROUP BY
DATA,
DOCUMENTO_ID,
NUMERO,
SERIE
ORDER BY
DATA,
DOCUMENTO_ID,
NUMERO,
SERIE
but I have an issue:
if I have a row with CARICO = 0 and SCARICO = 0, SALDO is added or
subtracted by the number present in last row with
CARICO > 0 or SCARICO > 0
if I add "(AND(DC.CARICO + DC.SCARICO) > 0) IN a LEFT JOIN I obtain
almost correct result but the rows that contain "CARICO = 0 and SCARICO
= 0" now contain SALDO = NULL, the others contains correct progressive sum.
What I'm wrong?
Thanks
--
<pre>
Luigi Siciliano
--------------------------
</pre>
--
<pre>
Luigi Siciliano
--------------------------
</pre>