
            -- variables
            with curDT as (select localtimestamp as curDate)
            -- end variables
            SELECT
             count(  * ) AS cnt 
            FROM
            -- from
                (
            SELECT
                evnRecept_all.ReceptDelayType_id as "ReceptDelayType_id",
                CASE
                    WHEN
                        (evnRecept_all.EvnRecept_deleted = 2/* or evnRecept_all.ReceptDelayType_id = 4*/)
                    THEN
                        'Удалённый МО'
                    WHEN
                        evnRecept_all.ReceptDelayType_id = 3
                    THEN
                        'Отказ'
                    WHEN
                        evnRecept_all.ReceptDelayType_id = 5
                    THEN
                        'Снят с обслуживания'
                    WHEN
                        evnRecept_all.ReceptDelayType_id = 2 and evnRecept_all.EvnRecept_otpDT is null and evnRecept_all.EvnRecept_obrDT is not null
                    THEN
                        'Отсрочен'
                    WHEN
                        
            localtimestamp >= case
                when RV.ReceptValid_Code = 1 then dateadd('month', 1, evnRecept_all.EvnRecept_setDate)
                when RV.ReceptValid_Code = 2 then dateadd('month', 3, evnRecept_all.EvnRecept_setDate)
                when RV.ReceptValid_Code = 3 then dateadd('day', 14, evnRecept_all.EvnRecept_setDate)
                when RV.ReceptValid_Code = 4 then dateadd('day', 5, evnRecept_all.EvnRecept_setDate)
                when RV.ReceptValid_Code = 5 then dateadd('month', 2, evnRecept_all.EvnRecept_setDate)
                when RV.ReceptValid_Code = 7 then dateadd('day', 10, evnRecept_all.EvnRecept_setDate)
                when RV.ReceptValid_Code = 8 then dateadd('day', 60, evnRecept_all.EvnRecept_setDate)
                when RV.ReceptValid_Code = 9 then dateadd('day', 30, evnRecept_all.EvnRecept_setDate)
                when RV.ReceptValid_Code = 10 then dateadd('day', 90, evnRecept_all.EvnRecept_setDate)
                when RV.ReceptValid_Code = 11 then dateadd('day', 15, evnRecept_all.EvnRecept_setDate)
            end
            and evnRecept_all.EvnRecept_otpDT is null
        
                    THEN
                        'Просрочен'
                    WHEN
                        evnRecept_all.EvnRecept_otpDT is not null
                    THEN
                        'Отоварен'
                    WHEN
                        (evnRecept_all.EvnRecept_otpDT is null AND evnRecept_all.EvnRecept_obrDT is null)
                        OR 	(evnRecept_all.ReceptDelayType_id is not  null)
                    THEN
                        'Выписан'
                END
                    as "ReceptDelayType_Name",
                CASE WHEN (evnRecept_all.EvnRecept_IsSigned = 2 ) THEN 'ДА'
                ELSE 'НЕТ' END as "EvnRecept_IsSigned",
                CASE WHEN evnRecept_all.EvnRecept_isImport = 2 THEN 'true' ELSE 'false' END as "EvnRecept_isImport",
                RT.ReceptType_Code as "ReceptType_Code",
                CASE WHEN RT.ReceptType_Code = 3 THEN 'ЭД' ELSE RT.ReceptType_Name END as "ReceptType_Name",
                evnRecept_all.ReceptForm_id as "ReceptForm_id",
                Lpu.Lpu_Nick as "Lpu_Nick",
                evnRecept_all.EvnRecept_id as "EvnRecept_id",
                evnRecept_all.Person_id as "Person_id",
                evnRecept_all.PersonEvn_id as "PersonEvn_id",
                evnRecept_all.Server_id as "Server_id",
                evnRecept_all.OrgFarmacy_id as "OrgFarmacy_id",
                evnRecept_all.OrgFarmacy_oid as "OrgFarmacy_oid",
                CAST((WDSS.WhsDocumentSupplySpec_PriceNDS * evnRecept_all.EvnRecept_Kolvo) as numeric(19, 2)) as "EvnRecept_Suma",
                Person.Person_Surname as "Person_Surname",
                Person.Person_Firname as "Person_Firname",
                Person.Person_Secname as "Person_Secname",
                to_char(Person.Person_BirthDay, 'DD.MM.YYYY') as "Person_Birthday",

                Person.Person_Snils as "Person_Snils",
                evnRecept_all.EvnRecept_Ser as "EvnRecept_Ser",
                evnRecept_all.EvnRecept_Num as "EvnRecept_Num",
                case when evnRecept_all.Drug_id is not null then
                    ReceptFinance.ReceptFinance_Name
                    else
                    COALESCE(DF.DrugFinance_Name,ReceptFinance.ReceptFinance_Name)

                    end as "ReceptFinance_Name",
                /*ReceptFinance.ReceptFinance_Name as ReceptFinance_Name,*/
                ReceptFinance.ReceptFinance_id as "ReceptFinance_id",
                COALESCE(MedPersonal.Person_Surname, '') || ' ' || COALESCE(MedPersonal.Person_Firname, '') ||
                ' ' || COALESCE(MedPersonal.Person_Secname, '') as "MedPersonal_Fio",
                case
                    when
                        COALESCE(evnRecept_all.Drug_rlsid, evnRecept_all.DrugComplexMnn_id) IS Not null

                    then
                        coalesce(rlsActmatters.RUSNAME,rlsDrugComplexMnnName.DrugComplexMnnName_Name,'')
                    else
                        DrugMnn.DrugMnn_Name || case when evnRecept_all.EvnRecept_otpDT is not null then '' || COALESCE(DrugMnnOtp.DrugMnn_Name,'') else '' end

                end
                as "DrugMnn_Name",
                case
                    when
                        evnRecept_all.Drug_rlsid IS Not null
                    then
                        COALESCE(rlsDrug.Drug_Name,'')

                    else
                        COALESCE(Drug.Drug_Name,'') || case when evnRecept_all.EvnRecept_otpDT is not null then ' ' || COALESCE(DrugOtp.Drug_Name,'') else '' end

                end
                as "Drug_Name",
                evnRecept_all.Drug_id as "Drug_id",
                evnRecept_all.Drug_rlsid as "Drug_rlsid",
                evnRecept_all.DrugComplexMnn_id as "DrugComplexMnn_id",
                cast(ROUND(cast(evnRecept_all.EvnRecept_Kolvo as numeric), 3)as varchar) as "EvnRecept_firKolvo",
                cast(ROUND(cast(ro.otvEvnRecept_Kolvo as numeric), 3) as varchar) as "EvnRecept_secKolvo",
                cast(ROUND(cast(evnRecept_all.EvnRecept_Kolvo as numeric), 3) as varchar) || '<br/>' ||
                    case when ro.otvEvnRecept_Kolvo is not null then cast(ROUND(cast(ro.otvEvnRecept_Kolvo as numeric), 3) as varchar) else '&nbsp;' end as "EvnRecept_Kolvo",
                COALESCE(OrgFarmacy.OrgFarmacy_Name,'') || '<br/>' ||

                    COALESCE(OrgFarmacyOtp.OrgFarmacy_Name,'') as "OrgFarmacy_Name",


                to_char(evnRecept_all.EvnRecept_setDate, 'DD.MM.YYYY') as "EvnRecept_setDate",

                to_char(interval '-1 day' +case
                    when RV.ReceptValid_Code = 1 then interval '1 month' + evnRecept_all.EvnRecept_setDate
                    when RV.ReceptValid_Code = 2 then interval '3 month' + evnRecept_all.EvnRecept_setDate
                    when RV.ReceptValid_Code = 3 then interval '14 day' + evnRecept_all.EvnRecept_setDate
                    when RV.ReceptValid_Code = 4 then interval '5 day' + evnRecept_all.EvnRecept_setDate
                    when RV.ReceptValid_Code = 5 then interval '2 month' + evnRecept_all.EvnRecept_setDate
                    when RV.ReceptValid_Code = 7 then interval '10 day' + evnRecept_all.EvnRecept_setDate
                    when RV.ReceptValid_Code = 8 then interval '60 day' + evnRecept_all.EvnRecept_setDate
                    when RV.ReceptValid_Code = 9 then interval '30 day' + evnRecept_all.EvnRecept_setDate
                    when RV.ReceptValid_Code = 10 then interval '90 day' + evnRecept_all.EvnRecept_setDate
                    when RV.ReceptValid_Code = 11 then interval '15 day' + evnRecept_all.EvnRecept_setDate
                end,'dd.mm.yyyy') as "EvnRecept_Godn",
                to_char(evnRecept_all.EvnRecept_obrDT, 'DD.MM.YYYY') as "EvnRecept_obrDate",

                to_char(evnRecept_all.EvnRecept_otpDT, 'DD.MM.YYYY') as "EvnRecept_otpDate",

                datediff('day', evnRecept_all.EvnRecept_setDT, evnRecept_all.EvnRecept_obrDT) as "EvnRecept_obrDay",
                datediff('day', evnRecept_all.EvnRecept_obrDT, COALESCE(evnRecept_all.EvnRecept_otpDT,(select curDate from curDT))) as "EvnRecept_otsDay",

                datediff('day', evnRecept_all.EvnRecept_setDT, evnRecept_all.EvnRecept_otpDT) as "EvnRecept_otovDay",
                case when evnRecept_all.DrugRequestRow_id is null then 'НЕТ' else 'ДА' end as "EvnRecept_InRequest",
                COALESCE(OrgFarmacyOtp.OrgFarmacy_Name,'') as "OrgFarmacyOtp_Name",

                RecF.ReceptForm_Code as "ReceptForm_Code",
                COALESCE(wdcit.WhsDocumentCostItemType_Name,'') as "WhsDocumentCostItemType_Name"

            FROM
                v_EvnRecept_all evnRecept_all

            inner join
                v_PersonState Person  on Person.Person_id = evnRecept_all.Person_id

            left join WhsDocumentCostItemType wdcit  on wdcit.WhsDocumentCostItemType_id = evnRecept_all.WhsDocumentCostItemType_id

            left join dbo.v_ReceptValid RV on RV.ReceptValid_id = evnRecept_all.ReceptValid_id

            left join v_ReceptForm RecF  on RecF.ReceptForm_id = evnRecept_all.ReceptForm_id

            left join v_ReceptType RT  on RT.ReceptType_id = evnRecept_all.ReceptType_id

            left join WhsDocumentSupply WDS on evnRecept_all.WhsDocumentUc_id is not null
                and WDS.WhsDocumentUc_id = evnRecept_all.WhsDocumentUc_id
            left join WhsDocumentSupplySpec WDSS on WDSS.WhsDocumentSupply_id = WDS.WhsDocumentSupply_id
                and (evnRecept_all.Drug_rlsid = WDSS.Drug_id or evnRecept_all.DrugComplexMnn_id = WDSS.DrugComplexMnn_id)

            LEFT JOIN LATERAL (
                select
                    otv.EvnRecept_Kolvo as otvEvnRecept_Kolvo
                from ReceptOtov otv
                where otv.EvnRecept_otpDate is not null and
                    otv.EvnRecept_id = evnRecept_all.EvnRecept_id
                limit 1
            ) ro ON true

            LEFT JOIN LATERAL (
                    select Person_Surname, Person_Firname, Person_Secname
                    from v_MedPersonal
                    where
                        MedPersonal_id = evnRecept_all.MedPersonal_id
                         and Lpu_id = evnRecept_all.Lpu_id 
                        and MedPersonal_Code is not null
                    limit 1
            ) MedPersonal ON true

            LEFT JOIN
                v_Drug as Drug  on Drug.Drug_id = evnRecept_all.Drug_id

            LEFT JOIN
                v_Drug as DrugOtp  on DrugOtp.Drug_id = evnRecept_all.Drug_oid

            LEFT JOIN
                v_DrugMnn as DrugMnn  on DrugMnn.DrugMnn_id = Drug.DrugMnn_id

            LEFT JOIN
                v_DrugMnn as DrugMnnOtp  on DrugMnnOtp.DrugMnn_id = DrugOtp.DrugMnn_id

            LEFT JOIN
                rls.v_Drug as rlsDrug  on rlsDrug.Drug_id = evnRecept_all.Drug_rlsid

            LEFT JOIN
                rls.v_DrugComplexMnn as rlsDrugComplexMnn  on rlsDrugComplexMnn.DrugComplexMnn_id = COALESCE(rlsDrug.DrugComplexMnn_id, evnRecept_all.DrugComplexMnn_id)


            LEFT JOIN
                rls.v_DrugComplexMnnName as rlsDrugComplexMnnName  on rlsDrugComplexMnnName.DrugComplexMnnName_id = rlsDrugComplexMnn.DrugComplexMnnName_id

            LEFT JOIN
                rls.v_Actmatters as rlsActmatters  on rlsActmatters.Actmatters_id = rlsDrugComplexMnnName.Actmatters_id

            LEFT JOIN
                v_OrgFarmacy as OrgFarmacy  on OrgFarmacy.OrgFarmacy_id = evnRecept_all.OrgFarmacy_id

            LEFT JOIN
                v_OrgFarmacy as OrgFarmacyOtp  on OrgFarmacyOtp.OrgFarmacy_id = evnRecept_all.OrgFarmacy_oid

            LEFT JOIN
                v_ReceptFinance as ReceptFinance  on ReceptFinance.ReceptFinance_id = evnRecept_all.ReceptFinance_id

            LEFT JOIN
                v_DrugFinance as DF  on DF.DrugFinance_id = evnRecept_all.DrugFinance_id

            LEFT JOIN
                v_ReceptDelayType as ReceptDelayType  on ReceptDelayType.ReceptDelayType_id = evnRecept_all.ReceptDelayType_id

            LEFT JOIN
                v_Lpu as Lpu  on Lpu.Lpu_id = evnRecept_all.Lpu_id

            LEFT JOIN
                v_Diag as Diag  on Diag.Diag_id = evnRecept_all.Diag_id

             WHERE COALESCE(evnRecept_all.EvnRecept_Is7Noz, 1) = '1' and coalesce(Diag.Diag_Code, '') not in('R75','Z20.6','Z21','Z71.7','Z83.0') and coalesce(Diag.Diag_Code, '') not between 'B20.0' and 'B24'
                    UNION
                
            SELECT
                EvnRecept.ReceptDelayType_id as "ReceptDelayType_id",
                CASE
                    WHEN
                        
            localtimestamp >= case
                when RV.ReceptValid_Code = 1 then dateadd('month', 1, evnRecept_all.EvnRecept_setDate)
                when RV.ReceptValid_Code = 2 then dateadd('month', 3, evnRecept_all.EvnRecept_setDate)
                when RV.ReceptValid_Code = 3 then dateadd('day', 14, evnRecept_all.EvnRecept_setDate)
                when RV.ReceptValid_Code = 4 then dateadd('day', 5, evnRecept_all.EvnRecept_setDate)
                when RV.ReceptValid_Code = 5 then dateadd('month', 2, evnRecept_all.EvnRecept_setDate)
                when RV.ReceptValid_Code = 7 then dateadd('day', 10, evnRecept_all.EvnRecept_setDate)
                when RV.ReceptValid_Code = 8 then dateadd('day', 60, evnRecept_all.EvnRecept_setDate)
                when RV.ReceptValid_Code = 9 then dateadd('day', 30, evnRecept_all.EvnRecept_setDate)
                when RV.ReceptValid_Code = 10 then dateadd('day', 90, evnRecept_all.EvnRecept_setDate)
                when RV.ReceptValid_Code = 11 then dateadd('day', 15, evnRecept_all.EvnRecept_setDate)
            end
            and EvnRecept.EvnRecept_otpDT is null
        
                    THEN
                        'Просрочен'
                    WHEN
                        EvnRecept.ReceptDelayType_id is null
                    THEN
                        'Выписан'
                    WHEN
                        EvnRecept.EvnRecept_otpDT is not null
                    THEN
                        'Отоварен'
                    WHEN
                        EvnRecept.EvnRecept_otpDT is null and EvnRecept.EvnRecept_obrDT is not null
                    THEN
                        'Отсрочен'
                    WHEN
                        EvnRecept.ReceptDelayType_id = 3
                    THEN
                        'Отказ'
                END
                    as "ReceptDelayType_Name",
                CASE WHEN (evnRecept_all.EvnRecept_IsSigned = 2 ) THEN 'ДА' ELSE 'НЕТ' END as "EvnRecept_IsSigned",
                CASE WHEN evnRecept_all.EvnRecept_isImport = 2 THEN 'true' ELSE 'false' END as "EvnRecept_isImport",
                RT.ReceptType_Code as "ReceptType_Code",
                RT.ReceptType_Name as "ReceptType_Name",
                evnRecept_all.ReceptForm_id as "ReceptForm_id",
                Lpu.Lpu_Nick as "Lpu_Nick",
                null as "EvnRecept_id",
                EvnRecept.Person_id as "Person_id",
                evnRecept_all.OrgFarmacy_id as "OrgFarmacy_id",
                evnRecept_all.OrgFarmacy_oid as "OrgFarmacy_oid",
                null as "PersonEvn_id",
                null as "Server_id",
                cast(du.DocumentUc_SumNdsR as numeric(19,2)) as "EvnRecept_Suma",
                Person.Person_Surname as "Person_Surname",
                Person.Person_Firname as "Person_Firname",
                Person.Person_Secname as "Person_Secname",
                to_char(Person.Person_BirthDay, 'DD.MM.YYYY') as "Person_Birthday",

                EvnRecept.Person_Snils as "Person_Snils",
                EvnRecept.EvnRecept_Ser as "EvnRecept_Ser",
                EvnRecept.EvnRecept_Num as "EvnRecept_Num",
                case when evnRecept_all.Drug_id is not null then
                    ReceptFinance.ReceptFinance_Name
                    else
                    COALESCE(DF.DrugFinance_Name,ReceptFinance.ReceptFinance_Name)

                    end as "ReceptFinance_Name",
                /*ReceptFinance.ReceptFinance_Name as ReceptFinance_Name,*/
                ReceptFinance.ReceptFinance_id as "ReceptFinance_id",
                COALESCE(MedPersonal.Person_Surname, '') || ' ' || COALESCE(MedPersonal.Person_Firname, '') ||
                ' ' || COALESCE(MedPersonal.Person_Secname, '') as "MedPersonal_Fio",
                '&nbsp;<br/>'||DrugMnn.DrugMnn_Name as "DrugMnn_Name",
                '&nbsp;<br/>'||COALESCE(Drug.Drug_Name,'') as "Drug_Name",

                EvnRecept.Drug_id as "Drug_id",
                evnRecept_all.Drug_rlsid as "Drug_rlsid",
                evnRecept_all.DrugComplexMnn_id as "DrugComplexMnn_id",
                cast(ROUND(cast(EvnRecept.EvnRecept_Kolvo as numeric), 3)as varchar) as "EvnRecept_firKolvo",
                '' as "EvnRecept_secKolvo",
                cast(ROUND(cast(EvnRecept.EvnRecept_Kolvo as numeric), 3)as varchar) as "EvnRecept_Kolvo",
                '&nbsp;<br/>'||COALESCE(OrgFarmacy.OrgFarmacy_Name,'') as "OrgFarmacy_Name",

                to_char(EvnRecept.EvnRecept_setDT, 'DD.MM.YYYY') as "EvnRecept_setDate",

                to_char(interval '-1 day' +case
                    when RV.ReceptValid_Code = 1 then interval '1 month' + evnRecept_all.EvnRecept_setDate
                    when RV.ReceptValid_Code = 2 then interval '3 month' + evnRecept_all.EvnRecept_setDate
                    when RV.ReceptValid_Code = 3 then interval '14 day' + evnRecept_all.EvnRecept_setDate
                    when RV.ReceptValid_Code = 4 then interval '5 day' + evnRecept_all.EvnRecept_setDate
                    when RV.ReceptValid_Code = 5 then interval '2 month' + evnRecept_all.EvnRecept_setDate
                    when RV.ReceptValid_Code = 7 then interval '10 day' + evnRecept_all.EvnRecept_setDate
                    when RV.ReceptValid_Code = 8 then interval '60 day' + evnRecept_all.EvnRecept_setDate
                    when RV.ReceptValid_Code = 9 then interval '30 day' + evnRecept_all.EvnRecept_setDate
                    when RV.ReceptValid_Code = 10 then interval '90 day' + evnRecept_all.EvnRecept_setDate
                    when RV.ReceptValid_Code = 11 then interval '15 day' + evnRecept_all.EvnRecept_setDate
                end,'dd.mm.yyyy') as "EvnRecept_Godn",
                to_char(EvnRecept.EvnRecept_obrDT, 'DD.MM.YYYY') as "EvnRecept_obrDate",

                to_char(EvnRecept.EvnRecept_otpDT, 'DD.MM.YYYY') as "EvnRecept_otpDate",

                datediff('day', EvnRecept.EvnRecept_setDT, EvnRecept.EvnRecept_obrDT) as "EvnRecept_obrDay",
                datediff('day', EvnRecept.EvnRecept_obrDT, COALESCE(EvnRecept.EvnRecept_otpDT,(select curDate from curDT))) as "EvnRecept_otsDay",

                datediff('day', EvnRecept.EvnRecept_setDT, EvnRecept.EvnRecept_otpDT) as "EvnRecept_otovDay",
                'НЕТ' as "EvnRecept_InRequest",
                '' as "OrgFarmacyOtp_Name",
                null as "ReceptForm_Code",
                COALESCE(wdcit.WhsDocumentCostItemType_Name,'') as "WhsDocumentCostItemType_Name"

            FROM
                v_ReceptOtovUnSub EvnRecept
            inner join
                v_PersonState Person on Person.Person_id = EvnRecept.Person_id -- подняли выше, работает быстрее

            left join v_evnrecept_all evnRecept_all on evnRecept_all.EvnRecept_id=EvnRecept.EvnRecept_id
            left join WhsDocumentCostItemType wdcit on wdcit.WhsDocumentCostItemType_id = evnRecept_all.WhsDocumentCostItemType_id
            left join v_ReceptType RT on RT.ReceptType_id = evnRecept_all.ReceptType_id
            left join dbo.v_ReceptValid RV on RV.ReceptValid_id = EvnRecept.ReceptValid_id
            left join DocumentUcStr DUS on EvnRecept.evnrecept_otpdt is not null and EvnRecept.receptotov_id=DUS.ReceptOtov_id
            left join DocumentUc du  on DUS.DocumentUc_id=du.DocumentUc_id

            LEFT JOIN LATERAL (
                    select Person_Surname, Person_Firname, Person_Secname
                    from v_MedPersonal

                    where
                        MedPersonal_id = EvnRecept.MedPersonalRec_id
                         and Lpu_id = evnRecept_all.Lpu_id 
                        and MedPersonal_Code is not null
                    limit 1
            ) MedPersonal ON true

            LEFT JOIN v_Drug Drug on Drug.Drug_id=EvnRecept.Drug_id
            LEFT JOIN v_DrugMnn DrugMnn on DrugMnn.DrugMnn_id=Drug.DrugMnn_id
            LEFT JOIN rls.v_Drug as rlsDrug on rlsDrug.Drug_id = evnRecept_all.Drug_rlsid
            LEFT JOIN rls.v_DrugComplexMnn as rlsDrugComplexMnn
                on rlsDrugComplexMnn.DrugComplexMnn_id = COALESCE(rlsDrug.DrugComplexMnn_id, evnRecept_all.DrugComplexMnn_id)
            LEFT JOIN rls.v_DrugComplexMnnName as rlsDrugComplexMnnName
                on rlsDrugComplexMnnName.DrugComplexMnnName_id = rlsDrugComplexMnn.DrugComplexMnnName_id
            LEFT JOIN rls.v_Actmatters as rlsActmatters on rlsActmatters.Actmatters_id = rlsDrugComplexMnnName.Actmatters_id
            LEFT JOIN v_OrgFarmacy as OrgFarmacy on OrgFarmacy.OrgFarmacy_id=EvnRecept.OrgFarmacy_id
            LEFT JOIN ReceptFinance on ReceptFinance.ReceptFinance_id = EvnRecept.ReceptFinance_id
            LEFT JOIN v_DrugFinance as DF on DF.DrugFinance_id = EvnRecept.DrugFinance_id
            LEFT JOIN v_Lpu as Lpu on Lpu.Lpu_id = EvnRecept.Lpu_id
            LEFT JOIN v_Diag as Diag on Diag.Diag_id = EvnRecept.Diag_id

             WHERE COALESCE(EvnRecept.EvnRecept_Is7Noz, 1) = '1' and coalesce(Diag.Diag_Code, '') not in('R75','Z20.6','Z21','Z71.7','Z83.0') and coalesce(Diag.Diag_Code, '') not between 'B20.0' and 'B24'
                ) as "Recept"
            -- end from
            
