
				
				
				SELECT 
				*
			
				FROM  
				
			(select
				RES.RegistryES_id as "RegistryES_id",
				RegistryES_Num as "RegistryES_Num",
				REST.RegistryESType_Name as "RegistryESType_Name",
				RES.RegistryES_Version as "RegistryES_Version",
				to_char(RES.RegistryES_begDate, 'DD.MM.YYYY') as "RegistryES_begDate",
				to_char(RES.RegistryES_endDate, 'DD.MM.YYYY') as "RegistryES_endDate",

				to_char(RES.RegistryES_insDT, 'DD.MM.YYYY') as "RegistryES_insDT",

				to_char(RES.RegistryES_updDT, 'DD.MM.YYYY') as "RegistryES_updDT",

				RES.RegistryES_RecordCount as "RegistryES_RecordCount",			-- Всего ЛВН в реестре
				RESD.RegistryES_SuccessCount as "RegistryES_SuccessCount",		-- Принятые ЛВН в ФСС
				RES.RegistryES_ErrorCount as "RegistryES_ErrorCount",			-- Не принятые ЛВН в ФСС
				RES.Lpu_id as "Lpu_id",
				RES.Lpu_Nick as "Lpu_Nick",
				RES.Lpu_FSSRegNum as "Lpu_FSSRegNum",
				RES.Lpu_INN as "Lpu_INN",
				RES.Lpu_OGRN as "Lpu_OGRN",
				RES.RegistryES_UserFIO as "RegistryES_UserFIO",
				RES.RegistryES_UserPhone as "RegistryES_UserPhone",
				RES.RegistryES_UserEmail as "RegistryES_UserEmail",
				RES.RegistryES_Export as "RegistryES_Export",
				RES.RegistryESType_id as "RegistryESType_id",
				LB.LpuBuilding_Name as "LpuBuilding_Name",
				LS.LpuSection_Name as "LpuSection_Name",
				RESS.RegistryESStatus_id as "RegistryESStatus_id",
				RESS.RegistryESStatus_Code as "RegistryESStatus_Code",
				RESS.RegistryESStatus_Name as "RegistryESStatus_Name",
				RES.RegistryES_SendError as "RegistryES_SendError",
				case when (
					RESS.RegistryESStatus_Code = 3
					OR (
						RESS.RegistryESStatus_Code = 2
						and not exists(
							select 
								resr.Evn_id
							from
								v_RegistryESError resr 

								left join v_RegistryESErrorType ret  on ret.RegistryESErrorType_id = resr.RegistryESErrorType_id

							where
								resr.RegistryES_id = RES.RegistryES_id
								and COALESCE(ret.RegistryESErrorType_Code::text, '0') <> '005' -- разрешить только при наличии ошибок ФЛК

						)
					)
				) then 1 else 0 end as "RegistryESStatus_EnableManualActions"
			from
				v_RegistryES RES 

				left join v_RegistryESType REST  on REST.RegistryESType_id = RES.RegistryESType_id

				left join v_RegistryESStatus RESS  on RESS.RegistryESStatus_id = RES.RegistryESStatus_id

				left join v_LpuBuilding LB  on LB.LpuBuilding_id = RES.LpuBuilding_id

				left join v_LpuSection LS  on LS.LpuSection_id = RES.LpuSection_id

				LEFT JOIN LATERAL(

					select count(t.Evn_id) as RegistryES_SuccessCount
					from v_RegistryESData t 

					left join v_RegistryESDataStatus RESDS  on RESDS.RegistryESDataStatus_id = t.RegistryESDataStatus_id

					where t.RegistryES_id = RES.RegistryES_id and RESDS.RegistryESDataStatus_Code = 2
                    limit 1
				) RESD ON true
			where
				not exists (
					select 
						RESQ.RegistryESQueue_id
					from
						v_RegistryESQueue RESQ 

					where
						RESQ.RegistryES_id = RES.RegistryES_id
				)
				 and RES.Lpu_id = '13101851' and RES.RegistryES_endDate between '2021-12-16' and '2022-01-15'
			
			union all
				
			select
				RES.RegistryESQueue_id as "RegistryES_id",
				RegistryES_Num as "RegistryES_Num",
				REST.RegistryESType_Name as "RegistryESType_Name",
				null as "RegistryES_Version",
				to_char(RES.RegistryES_begDate, 'DD.MM.YYYY') as "RegistryES_begDate",
				to_char(RES.RegistryES_endDate, 'DD.MM.YYYY') as "RegistryES_endDate",

				to_char(RES.RegistryESQueue_insDT, 'DD.MM.YYYY') as "RegistryES_insDT",

				to_char(RES.RegistryESQueue_updDT, 'DD.MM.YYYY') as "RegistryES_updDT",

				null as "RegistryES_RecordCount",			-- Всего ЛВН в реестре
				RESD.RegistryES_SuccessCount as "RegistryES_SuccessCount",		-- Принятые ЛВН в ФСС
				RES.RegistryES_ErrorCount as "RegistryES_ErrorCount",			-- Не принятые ЛВН в ФСС
				RES.Lpu_id as "Lpu_id",
				RES.Lpu_Nick as "Lpu_Nick",
				RES.Lpu_FSSRegNum as "Lpu_FSSRegNum",
				RES.Lpu_INN as "Lpu_INN",
				RES.Lpu_OGRN as "Lpu_OGRN",
				RES.RegistryES_UserFIO as "RegistryES_UserFIO",
				RES.RegistryES_UserPhone as "RegistryES_UserPhone",
				RES.RegistryES_UserEmail as "RegistryES_UserEmail",
				RES.RegistryES_Export as "RegistryES_Export",
				RES.RegistryESType_id as "RegistryESType_id",
				LB.LpuBuilding_Name as "LpuBuilding_Name",
				LS.LpuSection_Name as "LpuSection_Name",
				RESS.RegistryESStatus_id as "RegistryESStatus_id",
				RESS.RegistryESStatus_Code as "RegistryESStatus_Code",
				RESS.RegistryESStatus_Name as "RegistryESStatus_Name",
				'' as "RegistryES_SendError",
				case when (
					RESS.RegistryESStatus_Code = 3
					OR (
						RESS.RegistryESStatus_Code = 2
						and not exists(
							select 
								resr.Evn_id
							from
								v_RegistryESError resr 

								left join v_RegistryESErrorType ret  on ret.RegistryESErrorType_id = resr.RegistryESErrorType_id

							where
								resr.RegistryES_id = RES.RegistryES_id
								and COALESCE(ret.RegistryESErrorType_Code::text, '0') <> '005' -- разрешить только при наличии ошибок ФЛК

						)
					)
				) then 1 else 0 end as "RegistryESStatus_EnableManualActions"
			from
				v_RegistryESQueue RES 

				left join v_RegistryESType REST  on REST.RegistryESType_id = RES.RegistryESType_id

				left join v_RegistryESStatus RESS  on RESS.RegistryESStatus_id = RES.RegistryESStatus_id

				left join v_LpuBuilding LB  on LB.LpuBuilding_id = RES.LpuBuilding_id

				left join v_LpuSection LS  on LS.LpuSection_id = RES.LpuSection_id

				LEFT JOIN LATERAL(

					select count(t.Evn_id) as RegistryES_SuccessCount
					from v_RegistryESData t 

					left join v_RegistryESDataStatus RESDS  on RESDS.RegistryESDataStatus_id = t.RegistryESDataStatus_id

					where t.RegistryES_id = RES.RegistryES_id and RESDS.RegistryESDataStatus_Code = 2
                    limit 1
				) RESD ON true
			where
				(1=1)
				 and RES.Lpu_id = '13101851' and RES.RegistryES_endDate between '2021-12-16' and '2022-01-15'
		) as "allregistryesdata"
			 WHERE  
			(1=1)
			
				
						ORDER BY 
							
			allregistryesdata
			
				OFFSET 0 ROWS		
				FETCH NEXT 100 ROWS ONLY 
				
