row_number over partition at Version < Firebird 3
Hard to tell what you want (a precise question gets a precise answer, a vague question at best gets a lucky guess). In Firebird 2.5 an answer may be something like:

EXECUTE BLOCK RETURNS (ID INTEGER, ID2 INTEGER, FROM DATE, TO DATE, 
                        COSTS DECIMAL(9,2), MySUM(DECIMAL(9,2))
AS
DECLARE VARIABLE FROMDATE DATE;
DECLARE VARIABLE TODATE DATE;
BEGIN
  FROMDATE = NULL;
  TODATE = NULL;
  MySUM = 0;
  FOR SELECT ID, ID2, FROM, TO, COSTS
      FROM MyTable
      INTO :ID, :ID2, :FROM, :TO, :COSTS DO
  BEGIN
    IF (FROMDATE &lt;= FROM and TODATE &gt;= TO) THEN
    BEGIN
      MySUM = MySUM + COSTS;
      SUSPEND;
    END
    ELSE
    BEGIN
      MySUM = COSTS;
      FROMDATE = FROM;
      TODATE = TO;
    END
    SUSPEND;
  END
END


By the way, I don't think the above would quite compile since 'FROM' and 'TO' are not names I would recommend as names of columns.

Still, HTH,
Set