select a.valoare,
       a.valoare_ramasa,
       a.dns_ramas,
       a.id_tip_operatie,
       a.id_operatie_mf,
       a.data_operatie,
       a.id_operatie_prec,
       a.denumire as numeoperatie,
       Add_months(a.data_operatie, a.DNS_RAMAS) as data_expirare,
       a.explicatia as explicatiaoperatie,
       b.id_mf,
       b.denumire,
       b.data_achizitie,
       b.data_pif,
       b.nract,
       b.nr_pif,
       b.id_tip_imobilizare,
       b.EXPLICATIA,
       b.tip_amortizare,
       b.tip_imobilizare,
       c.dataora,
       c.utilizator,
       c.dns_luni,
       c.id_lista_mf,
       c.id_lucrare,
       c.id_gestiune,
       c.nr_inventar,
       c.cod_mf,
       c.cont,
       c.acont,
       c.id_sectie,
       c.id_responsabil,
       c.cgest,
       c.gestiune,
       c.responsabil,
       NVL(k.rata, 0) as rata,
       k.luna,
       k.an,
       k.luna_exp,
       k.an_exp,
       NVL(k.cota_an, 0) as cota_an,
       NVL(k.cota_luna, 0) as cota_luna,
       k.ID_CALCUL_RATE,
       k.amortizare_prec as amort_prec,
       k.uzura_prec,
       /*k.data_ultima_rata as data_expirare,*/
       i.data_operatie as ultima_operatie,
       c.id_sucursala,
       a.utilizator as utilizatorop,
       a.DATAORA as dataoraop,
       a.STERS as stersop,
       a.DATAORA_STERS as dataoraop_sters,
       a.ID_UTIL_STERS as id_utilop_ster,
       a.UTILIZATORS as utilizatorop_sters
  from imob_voperatii_mf a
  left join imob_vlista_mf c on a.ID_MF = c.ID_MF
                            and to_date(to_char(extract(month from
                                                        a.DATA_OPERATIE)) || '/' ||
                                        to_char(extract(year from
                                                        a.DATA_OPERATIE)),
                                        'mm/yyyy') <=
                                to_date(c.LUNA_EXP || '/' || c.an_exp,
                                        'MM/YYYY')
                            and to_date(to_char(extract(month from
                                                        a.DATA_OPERATIE)) || '/' ||
                                        to_char(extract(year from
                                                        a.DATA_OPERATIE)),
                                        'mm/yyyy') >=
                                to_date(to_char(c.LUNA) || '/' ||
                                        to_char(c.AN),
                                        'mm/yyyy')
  left join imob_vnom_mf b on a.id_mf = b.id_mf
  left join imob_vcalcul_rate k on a.id_operatie_mf = k.ID_OPERATIE_MF
  left join (select id_operatie_mf, id_mf, id_operatie_prec, data_operatie
  from imob_voperatii_mf
 where id_operatie_mf in
       (select distinct first_value(id_operatie_mf) over(partition by id_mf order by data_operatie desc)
          from imob_voperatii_mf
         where data_operatie <= to_date('31102021','ddmmyyyy')
           and sters = 0
           and id_operatie_mf in (select distinct id_operatie_mf
                                    from imob_vcalcul_rate
                                   where sters = 0))) i on a.id_mf = i.id_mf
                                   and a.id_operatie_mf = i.ID_OPERATIE_MF
 where months_between(a.data_operatie, to_date('31102021','ddmmyyyy')) <= 0
 order by b.denumire, b.nract, a.id_operatie_mf asc;
