 SELECT o.id, o.name,
    (select sum(o.coef1) from RENT_P_GETOBJECT(o.id, 0, 0) g, rent_object o
      where o.id = g.objectkey and o.isgroup = 0) as SMain,
  
    (select sum(o.coef1) from RENT_P_GETOBJECT(o.id, 0, 0) g, rent_object o
      where o.id = g.objectkey and o.useful = 1) as SRenta,

    (select sum(ris.costncu)
    from rent_invoice i, rent_contractservice cs, rent_invoiceservice ris,
      RENT_P_GETOBJECT(o.id, 0, 0) g, rent_service s
    where i.id = ris.invoicekey and ris.contractservicekey = cs.id and
      cs.objectkey = g.objectkey and cs.servicekey = s.id and s.externalkey = 1
      and i.invoicedate >= :begindate and i.invoicedate <= :enddate)
    as SumInvoice,

    (select sum(ris.costncu)
    from rent_paybill i, rent_contractservice cs, rent_paybillrelat ris,
      RENT_P_GETOBJECT(o.id, 0, 0) g, rent_service s
    where i.id = ris.paybillkey and ris.contractservicekey = cs.id and
      cs.objectkey = g.objectkey and cs.servicekey = s.id and s.externalkey = 1
      and i.paybilldate >= :begindate and i.paybilldate <= :enddate)
    as SumPayBill
  FROM rent_object o
  WHERE parent IS NULL
  