
            with cte as (select cast(dbo.tzGetDate() as date) as curDate)

            select
                EP.EvnPrescr_id as "EvnPrescr_id",
                EPT.EvnPrescrTreat_id as "EvnPrescrTreat_id",
                EPT.EvnPrescrTreat_IsExec as "EvnPrescr_IsExec",
                EDr.EvnDrug_id as "EvnDrug_id",
                EPTD.EvnPrescrTreatDrug_FactCount as "EvnPrescr_FactCount",
                case when EDr.EvnDrug_id is null then 1 else 2 end as "EvnPrescr_IsHasEvn",
                to_char(EPT.EvnPrescrTreat_setDT, 'DD.MM.YYYY') as "date",
                case when EPT.EvnPrescrTreat_IsExec = 2 then cast(EPT.EvnPrescrTreat_PrescrCount as varchar) else cast(EPTD.EvnPrescrTreatDrug_FactCount as varchar) end || ' / ' || cast(EPT.EvnPrescrTreat_PrescrCount as varchar) as text,
                case when EPT.EvnPrescrTreat_IsExec = 2 then cast(EPT.EvnPrescrTreat_PrescrCount as varchar) else cast(EPTD.EvnPrescrTreatDrug_FactCount as varchar) end as "countCompletedDay",
                case
                    when EPT.EvnPrescrTreat_IsExec = 2 then 'green-cell'
                    when EPT.EvnPrescrTreat_setDT < (select curDate from cte) then 'red-cell'
                    else 'blue-cell'
                end as "class"
            from
                v_EvnPrescrTreat EPT
                left join v_EvnPrescrTreatDrug EPTD on EPTD.EvnPrescrTreat_id = EPT.EvnPrescrTreat_id
                left join lateral (
                    select
                        EDr.EvnDrug_id
                    from
                        v_EvnDrug EDr
                    where
                        EPT.EvnPrescrTreat_IsExec IN (1, 2)
                        and EDr.EvnPrescr_id = EPT.EvnPrescrTreat_id
                    limit 1
                ) EDr on true
                left join lateral (
                    select
                        EP.EvnPrescr_id
                    from
                        v_EvnCourseTreat ECT
                        inner join v_EvnPrescr EP on ECT.EvnCourseTreat_id = EP.EvnCourse_id
                    where
                        ECT.EvnCourseTreat_id = EPT.EvnCourse_id
                    order by
                        EPT.EvnCourse_id,
                        EP.EvnPrescr_setDT
                    limit 1
                ) EP on true
            where
                EvnPrescrTreat_pid = '140101035399922'
        
