
            with mv as (
                select
                localtimestamp as dt
            )
            SELECT RTRIM(PersonSurname_Surname) as "Person_Surname"
                ,RTRIM(PersonFirname_Firname) as "Person_Firname"
                ,RTRIM(PersonSecname_Secname) as "Person_Secname"
                ,to_char(PS.PersonBirthDay_BirthDay, 'DD.MM.YYYY') as "Person_Birthday"
                ,dbo.Age2(PS.PersonBirthDay_BirthDay, (select dt from mv)) as "Person_Age"
                ,to_char(person.Person_deadDT, 'DD.MM.YYYY') as "Person_deadDT",
                PP.Lpu_id as "Lpu_id",
                PS.Server_id as "Server_id",
                PS.Lpu_id as "Lpu_did",
                PS.PersonEvn_id as "PersonEvn_id",
                PS.Person_id as "Person_id",
                PP.PersonPrivilege_id as "PersonPrivilege_id",
                RTRIM(coalesce(PT.PrivilegeType_VCode, PT.PrivilegeType_Code::varchar)) as "PrivilegeType_VCode",
                to_char(PP.PersonPrivilege_begDate, 'DD.MM.YYYY') as "Privilege_begDate",
                to_char(PP.PersonPrivilege_endDate, 'DD.MM.YYYY') as "Privilege_endDate",
                PT.PrivilegeType_id as "PrivilegeType_id",
                PT.PrivilegeType_Code as "PrivilegeType_Code",
                RF.ReceptFinance_id as "ReceptFinance_id",
                RF.ReceptFinance_Code as "ReceptFinance_Code",
                PT_WDCIT.WhsDocumentCostItemType_Nick as "WhsDocumentCostItemType_Nick",
                CASE WHEN personrefuse.personrefuse_isrefuse = 2 and PT_WDCIT.WhsDocumentCostItemType_Nick = 'fl' THEN 'true' ELSE 'false' END as "Person_IsRefuse",

                CASE WHEN (
                    EXISTS (
                        SELECT personprivilege.person_id
                        FROM personprivilege
                        JOIN privilegetype ON (personprivilege.privilegetype_id = privilegetype.privilegetype_id)
                        WHERE (personprivilege.person_id = PS.person_id)
                            AND personprivilege.personprivilege_begdate <= now()
                            AND (personprivilege.personprivilege_enddate IS NULL
                                OR personprivilege.personprivilege_enddate >= now()::date)
                            AND privilegetype.receptfinance_id = 1
                        LIMIT 1
                    )
                ) THEN 'true'
                ELSE 'false'
                END AS "Person_IsFedLgot",

                CASE WHEN (
                    EXISTS (
                        SELECT personprivilege.person_id
                        FROM personprivilege
                            JOIN privilegetype ON personprivilege.privilegetype_id = privilegetype.privilegetype_id
                        WHERE (personprivilege.person_id = PS.person_id)
                            AND (personprivilege.personprivilege_begdate <= now())
                            AND (personprivilege.personprivilege_enddate IS NULL
                                OR personprivilege.personprivilege_enddate >= now()::date)
                            AND privilegetype.receptfinance_id = 2
                        LIMIT 1
                    )
                ) THEN 'true'
                ELSE 'false'
                END AS "Person_IsRegLgot",

                CASE
                    WHEN person_is7noz1.id IS NOT NULL THEN 'true'
                    WHEN person_is7noz2.id IS NOT NULL THEN 'true'
                    ELSE 'false'
                END AS "Person_Is7Noz",

                CASE
                    WHEN pls.Polis_endDate IS NOT NULL AND pls.Polis_endDate <= (SELECT dt FROM mv)
                        THEN 'orange'
                    ELSE CASE WHEN (person.PersonCloseCause_id = 2) AND (person.Person_closeDT IS NOT NULL)
                        THEN 'red'
                    ELSE CASE WHEN person.server_id = 0
                        THEN 'true'
                        ELSE 'false'
                        END
                    END
                END AS "Person_IsBDZ",

                coalesce(PUAdd.Address_Nick, PUAdd.Address_Address) as "Person_Address",
                to_char(PP.PersonPrivilege_delDT, 'DD.MM.YYYY HH24:MI:SS')||rtrim(UserDel.PMUser_Login)||', '||rtrim(UserDel.PMUser_Name) as "PersonPrivilege_deletedInfo",
                coalesce(PrivCT.PrivilegeCloseType_Name, '') as "PrivilegeCloseType_Name",
                coalesce(DocPriv.DocumentPrivilege_Data, '') as "DocumentPrivilege_Data",
                PersonRefuse2.IDs as "PersonRefuse_IDs",
                PersonRefuse2.Years as "PersonRefuse_Years",
                PersonRefuse2.IsRefuses as "PersonRefuse_IsRefuses",
                0 as "cntPC",
                RTRIM(PT.PrivilegeType_Name) || coalesce('Диагноз: ' || Diag.Diag_Code || ' ' || Diag.Diag_Name, '') ||
                case
                    when PT.PrivilegeType_endDate is not null then ' (закрыта)'
                    else ''
                end as "PrivilegeType_Name",

                COALESCE(FRLLOA.FRLLO_RegNum, fejl.FRLLOPersonPrivilege_RegNum) as "FRLLOPersonPrivilege_RegNum",
                Count(PP.PersonPrivilege_id) OVER() AS "totalCount" 
            FROM PersonPrivilege PP
                INNER JOIN PrivilegeType PT on PT.PrivilegeType_id = PP.PrivilegeType_id
                INNER JOIN PersonState PS ON (PS.person_id = pp.person_id)
                
                INNER JOIN person ON
                    (ps.person_id = person.person_id) AND
                    (person.person_deleted = 1 OR person.person_deleted IS NULL)

                LEFT JOIN v_ReceptFinance rf on RF.ReceptFinance_id = PT.ReceptFinance_id
                LEFT JOIN v_WhsDocumentCostItemType PT_WDCIT on  (PT_WDCIT.WhsDocumentCostItemType_id = PT.WhsDocumentCostItemType_id)

                LEFT JOIN LATERAL (
                    SELECT
                        PersonRefuse_IsRefuse
                    FROM
                        v_PersonRefuse
                    WHERE
                        Person_id = ps.Person_id
                        AND PersonRefuse_Year = (select YEAR(dt) from mv)
                    ORDER BY
                        PersonRefuse_upddt desc
                    LIMIT 1
                ) AS personrefuse ON TRUE

                LEFT JOIN LATERAL (
                    SELECT * FROM v_Polis pls WHERE (pls.Polis_id = ps.Polis_id) LIMIT 1
                    ) pls ON TRUE

                LEFT JOIN LATERAL (
                    SELECT *
                    FROM v_Address PUAdd
                    WHERE PUAdd.Address_id = coalesce(PS.PAddress_id, PS.UAddress_id)
                    LIMIT 1
                    ) PUAdd ON TRUE

                LEFT JOIN LATERAL (
                    SELECT *
                    FROM v_PMUserCache UserDel
                    WHERE UserDel.pmUser_id = PP.pmUser_delID
                    LIMIT 1
                    ) UserDel ON TRUE

                LEFT JOIN LATERAL (
                    SELECT *
                    FROM v_PrivilegeCloseType PrivCT
                    WHERE (PrivCT.PrivilegeCloseType_id = PP.PrivilegeCloseType_id)
                    LIMIT 1
                    ) PrivCT ON TRUE

                LEFT JOIN LATERAL(
                    SELECT
                        (
                            i_DocPriv.DocumentPrivilege_Ser || ' ' ||
                            i_DocPriv.DocumentPrivilege_Num || ' ' ||
                            to_char(i_DocPriv.DocumentPrivilege_begDate, 'DD.MM.YYYY') || ' ' ||
                            coalesce(i_Org.Org_Nick, i_DocPriv.DocumentPrivilege_Org, '')
                        ) as DocumentPrivilege_Data
                    FROM
                        v_DocumentPrivilege i_DocPriv
                        LEFT JOIN LATERAL (
                            SELECT *
                            FROM v_Org i_Org
                            WHERE i_Org.Org_id = i_DocPriv.Org_id
                            LIMIT 1
                        ) i_Org ON TRUE
                    WHERE
                        (i_DocPriv.PersonPrivilege_id = PP.PersonPrivilege_id)
                    ORDER BY
                        i_DocPriv.DocumentPrivilege_id DESC
                    LIMIT 1
                ) AS DocPriv ON TRUE

                LEFT JOIN LATERAL (
                    SELECT Diag_Code, Diag_Name FROM v_Diag WHERE Diag_id = PP.Diag_id LIMIT 1
                ) Diag ON TRUE

                LEFT JOIN LATERAL (
                    SELECT
                        string_agg(CAST(_vPP.PersonRefuse_id as varchar), ',') AS IDs,
                        string_agg(CAST(_vPP.PersonRefuse_Year as varchar), ',') AS Years,
                        string_agg(CAST(YN.YesNo_Code as varchar), ',') AS IsRefuses
                    FROM
                        v_PersonRefuse AS _vPP,
                        v_YesNo AS YN
                    WHERE
                        _vPP.Person_id = PP.Person_id
                        AND _vPP.PersonRefuse_IsRefuse = YN.YesNo_id
                        AND(
                            _vPP.PersonRefuse_Year = (select YEAR(dt) from mv)
                            or _vPP.PersonRefuse_Year = (select YEAR(dt)+1 from mv)
                        )
                ) PersonRefuse2 ON TRUE

                LEFT JOIN LATERAL (
                    SELECT v_persondisp.persondisp_id AS id
                    FROM v_personregistertype personregistertype
                        JOIN v_personregisterdiag ON (v_personregisterdiag.personregistertype_id = personregistertype.personregistertype_id)
                        JOIN v_persondisp ON
                            (v_persondisp.diag_id = v_personregisterdiag.diag_id)
                            AND (v_persondisp.person_id = PS.person_id)
                    WHERE personregistertype.personregistertype_sysnick::text = 'nolos'::text
                    LIMIT 1
                ) person_is7noz1 ON TRUE

                LEFT JOIN LATERAL (
                    SELECT v_personregister.personregister_id AS id
                    FROM v_personregistertype personregistertype
                    JOIN v_personregister ON (v_personregister.personregistertype_id = personregistertype.personregistertype_id)
                        AND (v_personregister.person_id = PS.person_id)
                        AND (v_personregister.personregister_disdate IS NULL)
                        AND (v_personregister.personregisteroutcause_id IS NULL)
                    WHERE personregistertype.personregistertype_sysnick::text = 'nolos'::text
                    LIMIT 1
                ) person_is7noz2 ON TRUE
                LEFT JOIN LATERAL (
                     	SELECT
                            fr.FRLLO_RegNum AS FRLLO_RegNum
                        FROM  v_FRLLOPersonPrivilegeLink vFPPL
                              LEFT JOIN dbo.v_FRLLO AS fr ON fr.frllo_id = vFPPL.frllo_id
                        WHERE vFPPL.personprivilege_id = PP.PersonPrivilege_id AND
                              fr.FRLLO_insDt = (
                                    SELECT MAX(FRLLO_insDt)
                                    FROM v_FRLLO b
                                    WHERE b.Person_id = PS.Person_id AND b.FRLLO_RegNum IS NOT NULL and
                                        b.FRLLO_insDt <= (select dt from mv))
                        LIMIT 1
                     ) AS FRLLOA ON TRUE
                LEFT JOIN LATERAL (
                    SELECT
                        fejl.frlloexportjournallog_id,
                        fejl.FRLLOPersonPrivilege_RegNum
                    FROM
                        v_FRLLOExportJournalLog fejl
                    WHERE
                        fejl.PersonPrivilege_id = PP.PersonPrivilege_id AND
                        fejl.FRLLOPersonPrivilege_RegNum IS NOT NULL AND
                        fejl.frlloexportjournallog_insdt = (
                        SELECT MAX(frlloexportjournallog_insdt)
                        FROM v_FRLLOExportJournalLog b
                        WHERE b.PersonPrivilege_id = PP.PersonPrivilege_id and b.frllopersonprivilege_regnum is not null and
                                b.frlloexportjournallog_insdt <= (select dt from mv))
                    LIMIT 1
                ) as fejl ON TRUE
            WHERE (1 = 1)  and (Coalesce(PP.PersonPrivilege_deleted, 1) = 1)
            --and PT.PrivilegeType_id not in('314','2746','2260','324','306','12','2325','332','3408','3415','3303','509')
            --and PPPersonCard.personcard_id is not null
            and PT.PrivilegeType_id not in('330','306','328','332','40','2746','314')
            ORDER BY
                PersonSurname_Surname,
                PersonFirname_Firname,
                PersonSecname_Secname,
                PP.PersonPrivilege_id
            OFFSET 0 ROWS
                FETCH NEXT 50 ROWS ONLY
