
            with currentDate as (select cast(localtimestamp as date) as curdate)
            SELECT
                LSW.LpuSectionWard_id as "LpuSectionWard_id",
                LSW.LpuSection_id as "LpuSection_id",
                LSW.Sex_id as "Sex_id",
                LS.Lpu_id as "Lpu_id",
                COALESCE(LSW.LpuSectionWard_BedCount, 0) as "LpuSectionWard_BedCount",
                COALESCE(LSW.LpuSectionWard_BedRepair, 0) as "LpuSectionWard_BedRepair",
                RTRIM(COALESCE(LSW.LpuSectionWard_Name, '')) as "LpuSectionWard_Name",
                to_char(LSW.LpuSectionWard_disDate, 'DD.MM.YYYY') as "LpuSectionWard_disDate",
                to_char(LSW.LpuSectionWard_setDate, 'DD.MM.YYYY') as "LpuSectionWard_setDate"
                
            FROM v_LpuSectionWard LSW
                inner join v_LpuSection LS on LS.LpuSection_id = LSW.LpuSection_id
                inner join v_WardProperties wp on wp.LpuSectionWard_id = LSW.LpuSectionWard_id
                LEFT JOIN LATERAL (
                        select
                            count(EvnSection.EvnSection_id) as busy
                        from
                            v_EvnSection EvnSection
                        where
                            EvnSection.LpuSection_id = LSW.LpuSection_id
                            and EvnSection.LpuSectionWard_id = LSW.LpuSectionWard_id
                            and cast(EvnSection_setDate as DATE) <= cast('2023-07-22' as DATE)
                            and EvnSection_disDate is null
                    ) busyCountProfil on true
                    
                WHERE LS.Lpu_id in (13100979) and LS.LpuSection_id = '99560025731' and LSW.LpuSectionWard_id = '3417' and (3 = '2' or LSW.Sex_id = '2' or LSW.Sex_id is null) and ((select curdate from currentDate) between cast(LSW.LpuSectionWard_setDate as date) and cast(COALESCE(LSW.LpuSectionWard_disDate, (select curdate from currentDate)) as date)) and wp.WardProperties_endDate is null and 
                        LSW.LpuSectionWard_id = '3417' or (
                        LSW.LpuSection_id = '99560025731'
                        and (3 = '2' or LSW.Sex_id = '2' or LSW.Sex_id is null)
                        and ((select curdate from currentDate) between cast(LSW.LpuSectionWard_setDate as date) and cast(COALESCE(LSW.LpuSectionWard_disDate,(select curdate from currentDate)) as date))
                        and (LSW.LpuSectionWard_BedCount - LSW.LpuSectionWard_BedRepair - busyCountProfil.busy) > 0
                    )
        
