if exists(select 1 from sys.views V inner join sys.[schemas] S on  v.schema_id = s.schema_id where s.name='IES' and v.name = 'V_MTR_SCHET_C_OKATO1' and v.type = 'V')
    DROP VIEW [IES].[V_MTR_SCHET_C_OKATO1]
GO

CREATE VIEW [IES].[V_MTR_SCHET_C_OKATO1] WITH SCHEMABINDING 
AS
    select s.SchetID, f.TF_KOD TFOMS_IN_C_OKATO1, COUNT_BIG(*) 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
GO

CREATE UNIQUE CLUSTERED INDEX [V_MTR_SCHET_C_OKATO1_PK] ON [IES].[V_MTR_SCHET_C_OKATO1] ([SchetID] ASC)
GO
