Subject Re: [firebird-support] How write a query with a progressive sum field
Author Luigi Siciliano
Hallo,

Il 08/03/2016 20.36, setysvar setysvar@... [firebird-support] ha
scritto:
> This will not work properly if more than one row of DC are joined to
> the same DT (but then your ORDER BY isn't 100% deterministic). If you
> change the ordering of your query, you also have to change the
> subselect (e.g. if you add DESC you have to change from > to <). If
> this doesn't fit, is too slow or too difficult to understand, I'd
> recommend EXECUTE BLOCK (as Sean already wrote). HTH,

Thank You, the query suggested works but because of the primary index of
DOC_TESTA the value calculated of SALDO is not compliant with the result
of the rows of the query.

I solved by creating a stored procedure like this:

AS
BEGIN
SALDO = 0;

FOR SELECT
DT.DATA,
DT.DOCUMENTO_ID,
DT.NUMERO,
DT.SERIE,
DC.CARICO,
DC.SCARICO,
DC.CARICO - DC.SCARICO + :SALDO,
from
DOC_TESTA DT
JOIN DOC_CORPO DC on DT.ID = DC.DOC_TESTA_ID
WHERE
DC.ARTICOLO_ID = :ID
ORDER BY
DT.DATA,
DT.DOCUMENTO_ID,
DT.NUMERO,
DT.SERIE
INTO
:DATADOCUMENTO,
:DOCUMENTO_ID,
:NUMERO,
:SERIE,
:CARICO,
:SCARICO,
:SALDO,

DO SUSPEND;
END^

Thanks to all :)
--

Luigi Siciliano
--------------------------