
            select
                R.Registry_id as "Registry_id",
                R.RegistryType_id as "RegistryType_id",
                RT.RegistryType_SysNick as "RegistryType_SysNick",
                RS.RegistryStatus_id as "RegistryStatus_id",
                RS.RegistryStatus_SysNick as "RegistryStatus_SysNick",
                RS.RegistryStatus_Name as "RegistryStatus_Name",
                RS.RegistryStatus_Name as "RegistryStatusText",
                RT.RegistryType_SysNick as "RegistryType_SysNick",
                RT.RegistryType_Name as "RegistryType_Name",
                R.Registry_IsActive as "Registry_IsActive",
                coalesce(R.Registry_IsNeedReform, 1) as "Registry_IsNeedReform",
                RTrim(R.Registry_Num) as "Registry_Num",
                to_char(R.Registry_insDT, 'dd.mm.yyyy') as "Registry_insDT",
                to_char(R.Registry_accDate, 'dd.mm.yyyy') as "Registry_accDate",
                to_char(R.Registry_begDate, 'dd.mm.yyyy') as "Registry_begDate",
                to_char(R.Registry_endDate, 'dd.mm.yyyy') as "Registry_endDate",
                R.Lpu_id as "Lpu_id",
                R.Registry_xmlExportPath as "Registry_xmlExportPath",
                DC.DispClass_id as "DispClass_id",
                DC.DispClass_Name as "DispClass_Name",
                R.PayType_id as "PayType_id",
                PT.PayType_Name as "PayType_Name",
                PT.PayType_SysNick as "PayType_SysNick",
                R.KatNasel_id as "KatNasel_id",
                KN.KatNasel_Name as "KatNasel_Name",
                KN.KatNasel_SysNick as "KatNasel_SysNick",
                R.OrgRSchet_id as "OrgRSchet_id",
                R.OrgSMO_id as "OrgSMO_id",
                R.Org_mid as "Org_mid",
                R.Registry_IsRepeated as "Registry_IsRepeated",
                R.Registry_IsLocked as "Registry_IsLocked",
                case when R.Registry_IsRepeated = 2 then 'Исправительный' else 'Основной' end as "RegistryUnionText",
                case when R.Org_mid is not null then O.Org_Nick else case when KN.KatNasel_Code = 1 then OS.OrgSMO_Name else 'ТФОМС Нижегородской области' end end as "PayText",
                case when R.Registry_IsZNO = 2 then 'true' else 'false' end as "Registry_IsZNO",
                case when R.Registry_IsFAP = 2 then 'true' else 'false' end as "Registry_IsFAP",
                LB.LpuBuilding_Name as "LpuBuilding_Name",
                coalesce(R.Registry_RecordCount, 0) as "Registry_Count",
                coalesce(R.Registry_RecordCount, 0) as "Registry_RecordCount",
                coalesce(R.Registry_ErrorCount, 0) as "Registry_ErrorCount",
                RegistryErrorCom.RegistryErrorCom_IsData as "RegistryErrorCom_IsData",
                RegistryError.RegistryError_IsData as "RegistryError_IsData",
                RegistryNoPolis.RegistryNoPolis_IsData as "RegistryNoPolis_IsData",
                RegistryErrorTFOMS.RegistryErrorTFOMS_IsData as "RegistryErrorTFOMS_IsData",
                RegistryNoPay.RegistryNoPay_IsData as "RegistryNoPay_IsData",
                RegistryPerson.RegistryPerson_IsData as "RegistryPerson_IsData",
                coalesce(R.Registry_Sum, 0.00) as "Registry_Sum",
                R.Registry_SumPaid as "Registry_SumPaid",
                case when RQ.RegistryQueue_id is not null then 1 else 0 end as "Registry_IsProgress",
                '' as "Registry_lastExpDate",
                to_char(R.Registry_updDT, 'dd.mm.yyyy hh24:mi:ss') as "Registry_updDate",
                to_char(RQH.RegistryQueueHistory_endDT, 'dd.mm.yyyy hh24:mi:ss') as "ReformTime",
                RGT.RegistryGroupType_Name as "RegistryGroupType_Name",
                RGT.RegistryGroupType_Code as "RegistryGroupType_Code",
                RCS.RegistryCheckStatus_id as "RegistryCheckStatus_id",
                RCS.RegistryCheckStatus_Code as "RegistryCheckStatus_Code",
                RCS.RegistryCheckStatus_Name as "RegistryCheckStatus_Name",
                RCS.RegistryCheckStatus_SysNick as "RegistryCheckStatus_SysNick",
                case when RS.RegistryStatus_SysNick = 'paid' then '2' else '1' end as "Registry_IsPaid",
                OS.OrgSMO_Nick as "OrgSMO_Nick",
                concat(trim(PUC.pmuser_login),' - ',trim(PUC.pmuser_name)) as "Registry_PmUserName",
                '' as "Error_Message"
                ,R.Registry_SumNoErr as "Registry_SumNoErr"
            from r52.v_Registry R
                left join v_RegistryType RT on RT.RegistryType_id = R.RegistryType_id
                left join v_RegistryGroupType RGT on RGT.RegistryGroupType_id = R.RegistryGroupType_id
                left join v_RegistryCheckStatus RCS on RCS.RegistryCheckStatus_id = R.RegistryCheckStatus_id
                left join v_DispClass DC on DC.DispClass_id = R.DispClass_id
                left join v_PayType PT on PT.PayType_id = R.PayType_id
                left join v_KatNasel KN on KN.KatNasel_id = R.KatNasel_id
                left join v_LpuBuilding LB  on LB.LpuBuilding_id = R.LpuBuilding_id
                left join v_OrgSMO OS on OS.OrgSMO_id = R.OrgSMO_id
                left join v_Org O on O.Org_id = R.Org_mid
                left join v_PmUserCache PUC on PUC.pmuser_id = coalesce(R.pmuser_updid, R.pmuser_insid)
                left join v_RegistryStatus RS on RS.RegistryStatus_id = R.RegistryStatus_id
                left join lateral(
                    select
                        RegistryQueue_id
                    from r52.v_RegistryQueue
                    where Registry_id = R.Registry_id
                    limit 1
                ) RQ on true
                left join lateral(
                    select
                        RegistryQueueHistory_endDT
                    from r52.RegistryQueueHistory
                    where Registry_id = R.Registry_id
                        and RegistryQueueHistory_endDT is not null
                    order by RegistryQueueHistory_id desc
                    limit 1
                ) RQH on true
                left join lateral(
                    select case when RE.Registry_id is not null then 1 else 0 end as RegistryErrorCom_IsData
                    from r52.v_RegistryErrorCom RE
                    where RE.Registry_id = R.Registry_id
                    limit 1
                ) RegistryErrorCom on true
                left join lateral(
                    select case when RE.Registry_id is not null then 1 else 0 end as RegistryError_IsData
                    from r52.v_RegistryError RE
                    where RE.Registry_id = R.Registry_id 
                    limit 1
                ) RegistryError on true
                left join lateral(
                    select case when RE.Registry_id is not null then 1 else 0 end as RegistryNoPolis_IsData
                    from r52.v_RegistryNoPolis RE
                    where RE.Registry_id = R.Registry_id 
                    limit 1
                ) RegistryNoPolis on true
                left join lateral(
                    select case when RE.Registry_id is not null then 1 else 0 end as RegistryPerson_IsData
                    from r52.v_RegistryPerson RE
                    where RE.Registry_id = R.Registry_id
                    limit 1
                ) RegistryPerson on true
                left join lateral(
                    select case when RE.Registry_id is not null then 1 else 0 end as RegistryErrorTFOMS_IsData
                    from r52.v_RegistryErrorTFOMS RE
                    where RE.Registry_id = R.Registry_id 
                    limit 1
                ) RegistryErrorTFOMS on true
                left join lateral(
                    select case when RD.Registry_id is not null then 1 else 0 end as RegistryNoPay_IsData
                    from r52.v_RegistryData RD
                    where RD.Registry_id = R.Registry_id and RD.Paid_id = 1 
                    limit 1
                ) RegistryNoPay on true
                inner join lateral(
                (select PSTL.PayType_id
                from v_PaySchemeTypeLink PSTL
                inner join v_PaySchemeType PST on PST.PaySchemeType_id = PSTL.PaySchemeType_id
                where PSTL.PayType_id = coalesce(R.PayType_id, PSTL.PayType_id) and PST.PaySchemeType_Code = '1'
                limit 1)
            ) filterPaySchemeType on true
            where
                (1=1) and R.Lpu_id = '2861' and R.RegistryType_id = '45' and RS.RegistryStatus_SysNick = 'work'
            order by
                R.Registry_endDate DESC,
                RQH.RegistryQueueHistory_endDT DESC
        
