
                with cte as (
                    select distinct
                        EvnAll.Evn_id
                    from
                        dbo.v_Evn Evn
                        inner join v_Evn EvnAll on EvnAll.Evn_rid = Evn.Evn_rid
                    where
                        Evn.Evn_id = 890101028653624
                )
            
                    (select
                        0 as "marker",
                        EvnXml.EvnXml_id as "EvnXml_id",
                        cast (unnest(xpath('data/diagnos/text()', EvnXml.EvnXml_Data)) as text) as "value"
                    from cte
                        inner join v_EvnXml EvnXml on EvnXml.Evn_id = cte.Evn_id
                        left join v_EvnXml CurrEvnXml on CurrEvnXml.EvnXml_id = 0
                    where EvnXml.XmlType_id = 9
                        and (CurrEvnXml.EvnXml_id is null or EvnXml.EvnXml_insDT < CurrEvnXml.EvnXml_insDT)
                         and COALESCE(cast(xpath('data/diagnos', EvnXml.EvnXml_Data) as text), '') != '' 
                         order by EvnXml.EvnXml_Index  DESC 
                    limit 1)
                union all
                    (select
                        1 as "marker",
                        EvnXml.EvnXml_id as "EvnXml_id",
                        cast (unnest(xpath('data/anamnesfront/text()', EvnXml.EvnXml_Data)) as text) as "value"
                    from cte
                        inner join v_EvnXml EvnXml on EvnXml.Evn_id = cte.Evn_id
                        left join v_EvnXml CurrEvnXml on CurrEvnXml.EvnXml_id = 0
                    where EvnXml.XmlType_id = 9
                        and (CurrEvnXml.EvnXml_id is null or EvnXml.EvnXml_insDT < CurrEvnXml.EvnXml_insDT)
                         and COALESCE(cast(xpath('data/anamnesfront', EvnXml.EvnXml_Data) as text), '') != '' 
                         order by EvnXml.EvnXml_Index  DESC 
                    limit 1)
                union all
                    (select
                        2 as "marker",
                        EvnXml.EvnXml_id as "EvnXml_id",
                        cast (unnest(xpath('data/comorbidities/text()', EvnXml.EvnXml_Data)) as text) as "value"
                    from cte
                        inner join v_EvnXml EvnXml on EvnXml.Evn_id = cte.Evn_id
                        left join v_EvnXml CurrEvnXml on CurrEvnXml.EvnXml_id = 0
                    where EvnXml.XmlType_id = 9
                        and (CurrEvnXml.EvnXml_id is null or EvnXml.EvnXml_insDT < CurrEvnXml.EvnXml_insDT)
                         and COALESCE(cast(xpath('data/comorbidities', EvnXml.EvnXml_Data) as text), '') != '' 
                         order by EvnXml.EvnXml_Index  DESC 
                    limit 1)
                union all
                    (select
                        3 as "marker",
                        EvnXml.EvnXml_id as "EvnXml_id",
                        cast (unnest(xpath('data/edification/text()', EvnXml.EvnXml_Data)) as text) as "value"
                    from cte
                        inner join v_EvnXml EvnXml on EvnXml.Evn_id = cte.Evn_id
                        left join v_EvnXml CurrEvnXml on CurrEvnXml.EvnXml_id = 0
                    where EvnXml.XmlType_id = 9
                        and (CurrEvnXml.EvnXml_id is null or EvnXml.EvnXml_insDT < CurrEvnXml.EvnXml_insDT)
                         and COALESCE(cast(xpath('data/edification', EvnXml.EvnXml_Data) as text), '') != '' 
                         order by EvnXml.EvnXml_Index  DESC 
                    limit 1)
                union all
                    (select
                        4 as "marker",
                        EvnXml.EvnXml_id as "EvnXml_id",
                        cast (unnest(xpath('data/complaint/text()', EvnXml.EvnXml_Data)) as text) as "value"
                    from cte
                        inner join v_EvnXml EvnXml on EvnXml.Evn_id = cte.Evn_id
                        left join v_EvnXml CurrEvnXml on CurrEvnXml.EvnXml_id = 0
                    where EvnXml.XmlType_id = 8
                        and (CurrEvnXml.EvnXml_id is null or EvnXml.EvnXml_insDT < CurrEvnXml.EvnXml_insDT)
                         and COALESCE(cast(xpath('data/complaint', EvnXml.EvnXml_Data) as text), '') != '' 
                         order by EvnXml.EvnXml_Index  ASC 
                    limit 1)
                union all
                    (select
                        5 as "marker",
                        EvnXml.EvnXml_id as "EvnXml_id",
                        cast (unnest(xpath('data/anamnesmorbi/text()', EvnXml.EvnXml_Data)) as text) as "value"
                    from cte
                        inner join v_EvnXml EvnXml on EvnXml.Evn_id = cte.Evn_id
                        left join v_EvnXml CurrEvnXml on CurrEvnXml.EvnXml_id = 0
                    where EvnXml.XmlType_id = 9
                        and (CurrEvnXml.EvnXml_id is null or EvnXml.EvnXml_insDT < CurrEvnXml.EvnXml_insDT)
                         and COALESCE(cast(xpath('data/anamnesmorbi', EvnXml.EvnXml_Data) as text), '') != '' 
                         order by EvnXml.EvnXml_Index  DESC 
                    limit 1)
                union all
                    (select
                        6 as "marker",
                        EvnXml.EvnXml_id as "EvnXml_id",
                        cast (unnest(xpath('data/anamnesvitae/text()', EvnXml.EvnXml_Data)) as text) as "value"
                    from cte
                        inner join v_EvnXml EvnXml on EvnXml.Evn_id = cte.Evn_id
                        left join v_EvnXml CurrEvnXml on CurrEvnXml.EvnXml_id = 0
                    where EvnXml.XmlType_id = 9
                        and (CurrEvnXml.EvnXml_id is null or EvnXml.EvnXml_insDT < CurrEvnXml.EvnXml_insDT)
                         and COALESCE(cast(xpath('data/anamnesvitae', EvnXml.EvnXml_Data) as text), '') != '' 
                         order by EvnXml.EvnXml_Index  DESC 
                    limit 1)
                union all
                    (select
                        7 as "marker",
                        EvnXml.EvnXml_id as "EvnXml_id",
                        cast (unnest(xpath('data/comorbidities/text()', EvnXml.EvnXml_Data)) as text) as "value"
                    from cte
                        inner join v_EvnXml EvnXml on EvnXml.Evn_id = cte.Evn_id
                        left join v_EvnXml CurrEvnXml on CurrEvnXml.EvnXml_id = 0
                    where EvnXml.XmlType_id = 8
                        and (CurrEvnXml.EvnXml_id is null or EvnXml.EvnXml_insDT < CurrEvnXml.EvnXml_insDT)
                         and COALESCE(cast(xpath('data/comorbidities', EvnXml.EvnXml_Data) as text), '') != '' 
                         order by EvnXml.EvnXml_Index  DESC 
                    limit 1)
                union all
                    (select
                        8 as "marker",
                        EvnXml.EvnXml_id as "EvnXml_id",
                        cast (unnest(xpath('data/anamnesallerg/text()', EvnXml.EvnXml_Data)) as text) as "value"
                    from cte
                        inner join v_EvnXml EvnXml on EvnXml.Evn_id = cte.Evn_id
                        left join v_EvnXml CurrEvnXml on CurrEvnXml.EvnXml_id = 0
                    where EvnXml.XmlType_id = 8
                        and (CurrEvnXml.EvnXml_id is null or EvnXml.EvnXml_insDT < CurrEvnXml.EvnXml_insDT)
                         and COALESCE(cast(xpath('data/anamnesallerg', EvnXml.EvnXml_Data) as text), '') != '' 
                         order by EvnXml.EvnXml_Index  DESC 
                    limit 1)
                union all
                    (select
                        9 as "marker",
                        EvnXml.EvnXml_id as "EvnXml_id",
                        cast (unnest(xpath('data/objectivestatus/text()', EvnXml.EvnXml_Data)) as text) as "value"
                    from cte
                        inner join v_EvnXml EvnXml on EvnXml.Evn_id = cte.Evn_id
                        left join v_EvnXml CurrEvnXml on CurrEvnXml.EvnXml_id = 0
                    where EvnXml.XmlType_id = 8
                        and (CurrEvnXml.EvnXml_id is null or EvnXml.EvnXml_insDT < CurrEvnXml.EvnXml_insDT)
                         and COALESCE(cast(xpath('data/objectivestatus', EvnXml.EvnXml_Data) as text), '') != '' 
                         order by EvnXml.EvnXml_Index  ASC 
                    limit 1)
                union all
                    (select
                        10 as "marker",
                        EvnXml.EvnXml_id as "EvnXml_id",
                        cast (unnest(xpath('data/localstatus/text()', EvnXml.EvnXml_Data)) as text) as "value"
                    from cte
                        inner join v_EvnXml EvnXml on EvnXml.Evn_id = cte.Evn_id
                        left join v_EvnXml CurrEvnXml on CurrEvnXml.EvnXml_id = 0
                    where EvnXml.XmlType_id = 8
                        and (CurrEvnXml.EvnXml_id is null or EvnXml.EvnXml_insDT < CurrEvnXml.EvnXml_insDT)
                         and COALESCE(cast(xpath('data/localstatus', EvnXml.EvnXml_Data) as text), '') != '' 
                         order by EvnXml.EvnXml_Index  ASC 
                    limit 1)
                union all
                    (select
                        11 as "marker",
                        EvnXml.EvnXml_id as "EvnXml_id",
                        cast (unnest(xpath('data/Surveys/text()', EvnXml.EvnXml_Data)) as text) as "value"
                    from cte
                        inner join v_EvnXml EvnXml on EvnXml.Evn_id = cte.Evn_id
                        left join v_EvnXml CurrEvnXml on CurrEvnXml.EvnXml_id = 0
                    where EvnXml.XmlType_id = 9
                        and (CurrEvnXml.EvnXml_id is null or EvnXml.EvnXml_insDT < CurrEvnXml.EvnXml_insDT)
                         and COALESCE(cast(xpath('data/Surveys', EvnXml.EvnXml_Data) as text), '') != '' 
                         order by EvnXml.EvnXml_Index  DESC 
                    limit 1)
                union all
                    (select
                        12 as "marker",
                        EvnXml.EvnXml_id as "EvnXml_id",
                        cast (unnest(xpath('data/research/text()', EvnXml.EvnXml_Data)) as text) as "value"
                    from cte
                        inner join v_EvnXml EvnXml on EvnXml.Evn_id = cte.Evn_id
                        left join v_EvnXml CurrEvnXml on CurrEvnXml.EvnXml_id = 0
                    where EvnXml.XmlType_id = 9
                        and (CurrEvnXml.EvnXml_id is null or EvnXml.EvnXml_insDT < CurrEvnXml.EvnXml_insDT)
                         and COALESCE(cast(xpath('data/research', EvnXml.EvnXml_Data) as text), '') != '' 
                         order by EvnXml.EvnXml_Index  DESC 
                    limit 1)
                union all
                    (select
                        13 as "marker",
                        EvnXml.EvnXml_id as "EvnXml_id",
                        cast (unnest(xpath('data/diagnos/text()', EvnXml.EvnXml_Data)) as text) as "value"
                    from cte
                        inner join v_EvnXml EvnXml on EvnXml.Evn_id = cte.Evn_id
                        left join v_EvnXml CurrEvnXml on CurrEvnXml.EvnXml_id = 0
                    where EvnXml.XmlType_id = 8
                        and (CurrEvnXml.EvnXml_id is null or EvnXml.EvnXml_insDT < CurrEvnXml.EvnXml_insDT)
                         and COALESCE(cast(xpath('data/diagnos', EvnXml.EvnXml_Data) as text), '') != '' 
                         order by EvnXml.EvnXml_Index  ASC 
                    limit 1)
                union all
                    (select
                        14 as "marker",
                        EvnXml.EvnXml_id as "EvnXml_id",
                        cast (unnest(xpath('data/comorbidities/text()', EvnXml.EvnXml_Data)) as text) as "value"
                    from cte
                        inner join v_EvnXml EvnXml on EvnXml.Evn_id = cte.Evn_id
                        left join v_EvnXml CurrEvnXml on CurrEvnXml.EvnXml_id = 0
                    where EvnXml.XmlType_id = 8
                        and (CurrEvnXml.EvnXml_id is null or EvnXml.EvnXml_insDT < CurrEvnXml.EvnXml_insDT)
                         and COALESCE(cast(xpath('data/comorbidities', EvnXml.EvnXml_Data) as text), '') != '' 
                         order by EvnXml.EvnXml_Index  ASC 
                    limit 1)
                union all
                    (select
                        15 as "marker",
                        EvnXml.EvnXml_id as "EvnXml_id",
                        cast (unnest(xpath('data/treatmentprocess/text()', EvnXml.EvnXml_Data)) as text) as "value"
                    from cte
                        inner join v_EvnXml EvnXml on EvnXml.Evn_id = cte.Evn_id
                        left join v_EvnXml CurrEvnXml on CurrEvnXml.EvnXml_id = 0
                    where EvnXml.XmlType_id = 9
                        and (CurrEvnXml.EvnXml_id is null or EvnXml.EvnXml_insDT < CurrEvnXml.EvnXml_insDT)
                         and COALESCE(cast(xpath('data/treatmentprocess', EvnXml.EvnXml_Data) as text), '') != '' 
                         order by EvnXml.EvnXml_Index  DESC 
                    limit 1)
                union all
                    (select
                        16 as "marker",
                        EvnXml.EvnXml_id as "EvnXml_id",
                        cast (unnest(xpath('data/complaint/text()', EvnXml.EvnXml_Data)) as text) as "value"
                    from cte
                        inner join v_EvnXml EvnXml on EvnXml.Evn_id = cte.Evn_id
                        left join v_EvnXml CurrEvnXml on CurrEvnXml.EvnXml_id = 0
                    where EvnXml.XmlType_id = 9
                        and (CurrEvnXml.EvnXml_id is null or EvnXml.EvnXml_insDT < CurrEvnXml.EvnXml_insDT)
                         and COALESCE(cast(xpath('data/complaint', EvnXml.EvnXml_Data) as text), '') != '' 
                         order by EvnXml.EvnXml_Index  DESC 
                    limit 1)
                union all
                    (select
                        17 as "marker",
                        EvnXml.EvnXml_id as "EvnXml_id",
                        cast (unnest(xpath('data/objectivestatus/text()', EvnXml.EvnXml_Data)) as text) as "value"
                    from cte
                        inner join v_EvnXml EvnXml on EvnXml.Evn_id = cte.Evn_id
                        left join v_EvnXml CurrEvnXml on CurrEvnXml.EvnXml_id = 0
                    where EvnXml.XmlType_id = 9
                        and (CurrEvnXml.EvnXml_id is null or EvnXml.EvnXml_insDT < CurrEvnXml.EvnXml_insDT)
                         and COALESCE(cast(xpath('data/objectivestatus', EvnXml.EvnXml_Data) as text), '') != '' 
                         order by EvnXml.EvnXml_Index  DESC 
                    limit 1)
                union all
                    (select
                        18 as "marker",
                        EvnXml.EvnXml_id as "EvnXml_id",
                        cast (unnest(xpath('data/localstatus/text()', EvnXml.EvnXml_Data)) as text) as "value"
                    from cte
                        inner join v_EvnXml EvnXml on EvnXml.Evn_id = cte.Evn_id
                        left join v_EvnXml CurrEvnXml on CurrEvnXml.EvnXml_id = 0
                    where EvnXml.XmlType_id = 9
                        and (CurrEvnXml.EvnXml_id is null or EvnXml.EvnXml_insDT < CurrEvnXml.EvnXml_insDT)
                         and COALESCE(cast(xpath('data/localstatus', EvnXml.EvnXml_Data) as text), '') != '' 
                         order by EvnXml.EvnXml_Index  DESC 
                    limit 1)
                union all
                    (select
                        19 as "marker",
                        EvnXml.EvnXml_id as "EvnXml_id",
                        cast (unnest(xpath('data/recommendations/text()', EvnXml.EvnXml_Data)) as text) as "value"
                    from cte
                        inner join v_EvnXml EvnXml on EvnXml.Evn_id = cte.Evn_id
                        left join v_EvnXml CurrEvnXml on CurrEvnXml.EvnXml_id = 0
                    where EvnXml.XmlType_id = 9
                        and (CurrEvnXml.EvnXml_id is null or EvnXml.EvnXml_insDT < CurrEvnXml.EvnXml_insDT)
                         and COALESCE(cast(xpath('data/recommendations', EvnXml.EvnXml_Data) as text), '') != '' 
                         order by EvnXml.EvnXml_Index  DESC 
                    limit 1)
                
