
            select
                RESS.RegistryESStorage_id as "RegistryESStorage_id",
                RESS.RegistryESStorage_NumQuery as "RegistryESStorage_NumQuery",
                case
                    when RegistryESStorage_NumQuery is null then 'stickReq'
                    else right('0000000000000' || RESS.RegistryESStorage_NumQuery, 14)
                end as "groupField",
                RESC.RegistryESStorage_Count as "RegistryESStorage_Count",
                RESS.EvnStickBase_Num as "EvnStickBase_Num",
                RESS.RegistryESStorage_isActual as "RegistryESStorage_isActual",
                to_char (RESS.RegistryESStorage_updDT, 'dd.mm.yyyy') as "RegistryESStorage_updDT",
                case when RESS.EvnStickBase_id is not null then 2 else 1 end as "RegistryESStorage_IsUsed",
                COALESCE(puc.pmUser_Name, '') || ' (' || rtrim(puc.pmUser_Login) || ')' as "pmUser_Name"
            from
                v_RegistryESStorage RESS
                left join v_pmUserCache puc on puc.pmUser_id = RESS.pmUser_insID
                inner join lateral (
                    select
                        count(RegistryESStorage_id) as RegistryESStorage_Count
                    from
                        v_RegistryESStorage st
                    where
                        RESS.RegistryESStorage_NumQuery is not null
                        and st.RegistryESStorage_NumQuery = RESS.RegistryESStorage_NumQuery
                        and st.EvnStickBase_id is null
                        and st.RegistryESStorage_isActual = 2

                ) as RESC on true
            where
                (1=1) and RESS.Lpu_id = '2722' and cast(RESS.RegistryESStorage_insDT as date) between '2022-01-18' and '2022-02-17' and RESS.RegistryESStorage_isActual = 2
                and RESS.EvnStickBase_Num is not null
            order by
                RESS.EvnStickBase_Num asc
        
