
            SELECT
                PP.PersonPrivilege_id as "PersonPrivilege_id",
                PP.Lpu_id as "Lpu_id",
                PS.Lpu_id as "Lpu_did",
                case
                    when PP.Server_id = 3 then 'ПФР'
                    when PP.Server_id = 7 then 'Минздрав'
                    else coalesce(LPU.Lpu_Nick,'')
                end as "Lpu_Nick",
                PP.Server_id as "Server_id",
                PS.Person_id as "Person_id",
                PS.Person_Snils as "Person_Snils",
                PS.Polis_Ser as "Polis_Ser",
                COALESCE(RTRIM(case when Polis.PolisType_id = 4 and PS.Person_EdNum is not null then PS.Person_EdNum else Polis.Polis_Num end), '') as "Polis_Num",
                uaddr.Address_Address as "Person_UAddress",
                PS.PersonEvn_id as "PersonEvn_id",
                PT.PrivilegeType_Code as "PrivilegeType_Code",
                coalesce(PT.PrivilegeType_VCode, cast(PT.PrivilegeType_Code as varchar)) as "PrivilegeType_VCode",
                PT.PrivilegeType_id as "PrivilegeType_id",
                RTRIM(PS.Person_Surname) as "Person_Surname",
                RTRIM(PS.Person_Firname) as "Person_Firname",
                RTRIM(PS.Person_Secname) as "Person_Secname",
                to_char(PS.Person_Birthday, 'DD.MM.YYYY') as "Person_Birthday",
                to_char(PP.PersonPrivilege_begDate, 'DD.MM.YYYY') as "Privilege_begDate",
                to_char(PP.PersonPrivilege_endDate, 'DD.MM.YYYY') as "Privilege_endDate",
                RF.ReceptFinance_id as "ReceptFinance_id",
                RF.ReceptFinance_Code as "ReceptFinance_Code",
                coalesce (PCT.PrivilegeCloseType_Name, '') as "PrivilegeCloseType_Name",
                coalesce (DP.DocumentPrivilege_Data, '') as "DocumentPrivilege_Data",
                coalesce (D.Diag_Code || ' ' || D.Diag_Name, '') as "Diag_Name"
            FROM
                v_PersonPrivilege PP
                inner join v_PersonState PS on PS.Person_id = PP.Person_id
                INNER JOIN v_PrivilegeType PT ON (PT.PrivilegeType_id = PP.PrivilegeType_id)
                left join v_Lpu LPU on LPU.Lpu_id = PP.Lpu_id
                left join v_ReceptFinance RF on RF.ReceptFinance_id = PT.ReceptFinance_id
                left join v_PrivilegeCloseType PCT on PCT.PrivilegeCloseType_id = PP.PrivilegeCloseType_id
                left join v_Diag D on D.Diag_id = PP.Diag_id
                left join Polis on Polis.Polis_id = PS.Polis_id
                left join lateral (
                    select
                        uaddr.Address_Address
                    from
                        v_Address uaddr
                    where
                        uaddr.Address_id = PS.UAddress_id
                    limit 1
                ) uaddr on true
                left join lateral(
                    select
                        (
                            i_DP.DocumentPrivilege_Ser ||' '||
                            i_DP.DocumentPrivilege_Num ||' '||
                            to_char( i_DP.DocumentPrivilege_begDate, 'dd.mm.yyyy')||' '||
                            coalesce(i_O.Org_Nick, i_DP.DocumentPrivilege_Org, '')
                        ) as DocumentPrivilege_Data
                    from
                     	v_DocumentPrivilege i_DP
                        left join v_Org i_O on i_O.Org_id = i_DP.Org_id
                    where
                        i_DP.PersonPrivilege_id = PP.PersonPrivilege_id
                    order by
                        i_DP.DocumentPrivilege_id
                    limit 1
                ) DP on true
            WHERE
                PP.PrivilegeType_id = '3397'
                and PT.ReceptFinance_id = 2
                 and PP.PrivilegeType_id not in('330','306','328','332','40','2746','314')
                AND EXISTS (
                    SELECT 1
                    FROM v_PersonCard PC
                    WHERE (PC.Person_id = PS.Person_id)
                    AND (PC.Lpu_id = '13101138')
                    LIMIT 1)
            
            ORDER BY
                PS.Person_Surname,
                PS.Person_Firname,
                PS.Person_Secname
        
