DROP MATERIALIZED VIEW IF EXISTS IES.V_MTR_SCHET_C_OKATO1;

CREATE MATERIALIZED VIEW IES.V_MTR_SCHET_C_OKATO1
AS
    select s.SchetID, f.TF_KOD TFOMS_IN_C_OKATO1, COUNT(*) TfomsCount
    from IES.T_SCHET s
        join IES.T_F001_TFOMS f on f.TF_OKATO = s.C_OKATO1
    where s.C_OKATO1 is not null and f.D_END is null
    group by s.SchetID, f.TF_KOD;

CREATE UNIQUE INDEX V_MTR_SCHET_C_OKATO1_PK ON IES.V_MTR_SCHET_C_OKATO1 (SchetID ASC);
