
                
                
                SELECT 
                ppr.PersonPrivilegeReq_id as "PersonPrivilegeReq_id",
                ppr.PersonPrivilegeReqType_id as "PersonPrivilegeReqType_id",
                ppra.PersonPrivilegeReqAns_id as "PersonPrivilegeReqAns_id",
                ppr.PersonPrivilegeReqType_id as "PersonPrivilegeReqType_id",
                ppr.PersonPrivilege_id as "PersonPrivilege_id",
                pprt.PersonPrivilegeReqType_Name as "PersonPrivilegeReqType_Name",
                ppra.PersonPrivilegeReqStatus_id as "PersonPrivilegeReqStatus_id",
                to_char(ppr.PersonPrivilegeReq_setDT, 'dd.mm.yyyy HH24:MI') as "PersonPrivilegeReq_setDT",
                (
                    coalesce (rtrim(ps.Person_SurName)||' ','')||
                    coalesce (rtrim(ps.Person_FirName)||' ','')||
                    coalesce (rtrim(ps.Person_SecName)||' ','')||
                    coalesce (to_char(ps.Person_Birthday, 'dd.mm.yyyy'), '')
                ) as "Person_FullName",
                pt.PrivilegeType_Name as "PrivilegeType_Name",
                '' as "MedStaffFact_FullName",
                pprs.PersonPrivilegeReqStatus_Name as "PersonPrivilegeReqStatus_Name",
                (case
                    when
                        ppra.PersonPrivilegeReqStatus_id = 3 and in_reg.YesNo_Code = 0
                    then
                        (
                            'Отказано' || coalesce (': '||ppra.PersonPrivilegeReqAns_DeclCause, '')||' '||
                            to_char(ppra.PersonPrivilegeReqAns_updDT, 'dd.mm.yyyy HH24:MI')
                        )
                    when
                        ppra.PersonPrivilegeReqStatus_id = 3 and in_reg.YesNo_Code = 1
                    then
                        (
                            'Включен ' || to_char(ppra.PersonPrivilegeReqAns_updDT, 'dd.mm.yyyy HH24:MI')
                        )
                    else
                        ''
                end) as "Result_Data",
                (
                    coalesce (l.Lpu_Nick ||', ', '') ||
                    coalesce (pm.PostMed_Name || ', ', '') ||
                    coalesce (msf.Person_Fio, '')
                ) as "MedStaffFact_FullName",
                (
                    case when ppr.PersonPrivilegeReq_IsSigned = 2 or (ppr.pmUser_signID is not null and ppr.PersonPrivilegeReq_signDT is not null)
                        then 2 else
                        case when ppr.PersonPrivilegeReq_IsSigned = 2 AND (ppr.pmUser_signID is not null and ppr.PersonPrivilegeReq_signDT >= ppr.PersonPrivilegeReq_setDT)
                            then 1
                            else null
                        end
                    end
                ) as "PersonPrivilegeReq_IsSigned",
                to_char(ppr.PersonPrivilegeReq_signDT, 'dd.mm.yyyy') as "PersonPrivilegeReq_signDT",
                puc.pmUser_Name as "PPRSignPmUser_Name",
                (
                    case when ppra.PersonPrivilegeReqAns_IsSigned = 2 or (ppra.pmUser_signID is not null and ppra.PersonPrivilegeReqAns_signDT is not null)
                        then 2 else
                        case when ppra.PersonPrivilegeReqAns_IsSigned = 2 AND (ppra.pmUser_signID is not null and ppra.PersonPrivilegeReqAns_signDT >= ppra.PersonPrivilegeReqAns_updDT)
                            then 1
                            else null
                        end
                    end
                ) as "PersonPrivilegeReqAns_IsSigned",
                to_char(ppra.PersonPrivilegeReqAns_signDT, 'dd.mm.yyyy') as "PersonPrivilegeReqAnssignDT",
                puca.pmUser_Name as "PPRASignPmUser_Name",
                pprt.PersonPrivilegeReqType_Name as "PersonPrivilegeReqType_Name"

                
                FROM
                
                v_PersonPrivilegeReq ppr
                inner join v_PersonState ps on ps.Person_id = ppr.Person_id
                left join v_MedStaffFact msf on msf.MedStaffFact_id = ppr.MedStaffFact_id
                left join v_Lpu l on l.Lpu_id = msf.Lpu_id
                left join v_PersonPrivilegeReqAns ppra on ppra.PersonPrivilegeReq_id = ppr.PersonPrivilegeReq_id
                left join v_PersonPrivilegeReqStatus pprs on pprs.PersonPrivilegeReqStatus_id = ppra.PersonPrivilegeReqStatus_id
                left join v_PrivilegeType pt on pt.PrivilegeType_id = ppr.PrivilegeType_id
                left join v_YesNo in_reg on in_reg.YesNo_id = ppra.PersonPrivilegeReqAns_IsInReg
                left join v_PostMed pm on pm.PostMed_id = msf.Post_id
                left join v_pmUserCache puc on puc.pmUser_id = ppr.pmUser_signID
                left join v_PersonPrivilegeReqType pprt on pprt.PersonPrivilegeReqType_id = ppr.PersonPrivilegeReqType_id
                left join v_pmUserCache puca on puca.pmUser_id = ppra.pmUser_signID
                left join lateral (
                    select
                        cast(ppr.PersonPrivilegeReq_setDT as date) as val
                ) set_dt on true
                 WHERE 
                    set_dt.val >= CAST('2019-11-29' as date) and set_dt.val <= CAST('2022-05-04' as date) and ppra.PersonPrivilegeReqStatus_id = '2' and pprs.PersonPrivilegeReqStatus_id <> 1
                    
                
                        ORDER BY
                            
                ppr.PersonPrivilegeReq_id
                
                OFFSET 0 ROWS
                FETCH NEXT 50 ROWS ONLY
                
