DROP MATERIALIZED VIEW IF EXISTS IES.V_MTR_SCHET_OKATO_OMS;

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

CREATE UNIQUE INDEX V_MTR_SCHET_OKATO_OMS_PK ON IES.V_MTR_SCHET_OKATO_OMS (SchetID ASC);

