text: SELECT 
      DISTINCT a.AP_ID,
      EXTRACT(YEAR FROM TO_DATE(:P5_DATE, 'YYYY-MM-DD')) as "Year",
      EXTRACT(MONTH FROM TO_DATE(:P5_DATE, 'YYYY-MM-DD')) as "Month", 
      a.AP_FLOOR,
      a.BUILDING_ID,
      ID_HOUSEHOLD,
      a.ENTRANCE_ID,
      TRUNC(:P5_RIO * (a.IDEAL_PART/calc.mn),2) as "RIO"
  from INCOME_LOG
  JOIN APARTMENTS a ON a.AP_ID = INCOME_LOG.AP_ID
  JOIN HOUSEHOLDS h ON h."id_household" = INCOME_LOG.id_household
  JOIN (SELECT BUILDING_ID, SUM(IDEAL_PART) as sm, min(IDEAL_PART) as mn FROM APARTMENTS
          WHERE BUILDING_ID = :P5_BUILDING_ID
          AND ENTRANCE_ID = :P5_ENTRANCE_ID
          GROUP BY BUILDING_ID) calc ON INCOME_LOG.BUILDING_ID = calc.BUILDING_ID
  JOIN ()
  WHERE a.BUILDING_ID = :P5_BUILDING_ID
  AND a.ENTRANCE_ID = :P5_ENTRANCE_ID
  AND h.ACTIVE = 1
  ORDER BY a.AP_ID