
            select
                rr.RegistryRecept_id as "RegistryRecept_id",
                rr.ReceptStatusFLKMEK_id as "ReceptStatusFLKMEK_id"
            from r33.registryrecept rr
            left join lateral (
                select
                    count(*) as cnt
                from
                    ReceptOtov ro
                        inner join lateral (
                            select *
                            from ReceptUploadLog rul
                                 inner join r33.registryrecept rr1 on rr1.ReceptUploadLog_id = rul.ReceptUploadLog_id
                            where rul.receptuploadstatus_id in(4,5)
                                and rr1.receptotov_id = ro.receptotov_id
                                and rr1.ReceptUploadLog_id <> rr.ReceptUploadLog_id
                            limit 1
                        ) rul1 on true
                where
                    ro.EvnRecept_Ser = rr.RegistryRecept_ReceprSer
                    and replace(ltrim(replace(ro.EvnRecept_Num, '0', ' ')), ' ', '0') = cast(rr.RegistryRecept_ReceprNum as varchar)
                    and cast(ro.EvnRecept_setDT as date) = cast(rr.RegistryRecept_setDT as date)
                    and ro.ReceptStatusType_id in(2,3)
            ) ro on true
            where rr.receptuploadlog_id = '330101000000216' and ro.cnt > 0
        
