SELECT DISTINCT
                        --resource
                        vmsf.MedStaffFact_id,
                        vmsf.Person_SurName AS Last_Name,
                        vmsf.Person_FirName AS First_Name,
                        nullif(rtrim(ltrim(vmsf.Person_SecName)), '') AS Middle_Name,
                        vmsf.Person_Snils AS SNILS,
                        --post
                        mpp.MedPersonalPost_Code AS Post_Id,
                        Post.Post_Code AS Inner_Id,
                        Post.Post_Name AS Inner_Name,
                        --date
                        cast(ttg.TimeTableGraf_begTime as date) "begTime"
                FROM v_PersonDisp pd  
                INNER JOIN PersonDispHist pdh   on pdh.PersonDisp_id = pd.PersonDisp_id and PersonDispHist_endDate is null
                INNER JOIN (  
                 
                SELECT MedStaffFact.* 
                FROM dbo.v_MedStaffFact MedStaffFact  
             left join lateral  (
                SELECT 
                    COUNT(*) as allow
                FROM DataStorage   
                WHERE DataStorage_Value='1' AND DataStorage_Name='allow_queue_auto'
                  AND DataStorage.Lpu_id = MedStaffFact.Lpu_id
            ) AS AllowLpu  ON true 
                 left join lateral  (
                        SELECT 
                                count(q.EvnQueue_id) as countQeue
                        FROM dbo.v_LpuSection LpuSection  
                        INNER JOIN v_EvnQueue q   on q.Lpu_id = MedStaffFact.Lpu_id AND q.LpuSectionProfile_did = LpuSection.LpuSectionProfile_id
                        INNER JOIN dbo.EvnDirection ed   on ed.Evn_id = q.EvnDirection_id  AND COALESCE(ed.evn_deleted, 1) = 1 
                          
                        WHERE LpuSection.LpuUnit_id = MedStaffFact.LpuUnit_id
                          AND ed.MedStaffFact_id is null
                          AND q.QueueFailCause_id is null
                          AND q.RecMethodType_id in (1,2)
                          AND q.EvnQueueStatus_id in (1)
                ) AS EvnQueueStatus  ON true 
                 left join lateral  (
                        SELECT 
                                count(q.EvnQueue_id) as countMed
                        FROM v_EvnQueue q  
                        INNER JOIN dbo.EvnDirection ed   on ed.Evn_id = q.EvnDirection_id AND ed.MedStaffFact_id = MedStaffFact.MedStaffFact_id  AND COALESCE(ed.evn_deleted, 1) = 1 
                          
                        WHERE  q.Lpu_id = MedStaffFact.Lpu_id
                          AND q.QueueFailCause_id is null
                          AND q.RecMethodType_id in (1, 2)
                          AND q.EvnQueueStatus_id in (1)
                ) AS EvnQueueStatusMed  ON true 
                WHERE MedStaffFact.LpuUnit_id = 426 AND COALESCE(MedStaffFactCache_isNotShown, 1) != 2
                  AND (COALESCE(AllowLpu.allow, 0) = 0 OR (EvnQueueStatus.countQeue=0 AND EvnQueueStatusMed.countMed=0 ))
          ) vmsf on vmsf.MedPersonal_id = pdh.MedPersonal_id and vmsf.LpuSection_id = pdh.LpuSection_id --vmsf.MedStaffFact_id = pd.MedStaffFact_id
                INNER JOIN (SELECT dbo.tzGetDate() curDate) cdt ON cdt.curDate between  vmsf.WorkData_begDate AND COALESCE(vmsf.WorkData_endDate, cdt.curDate)
                LEFT JOIN nsi.v_PostLink PostLink   on PostLink.Post_lid = vmsf.Post_id
                LEFT JOIN nsi.v_Post Post   on Post.Post_id = PostLink.Post_id
                LEFT JOIN persis.Post pp   on pp.id =  vmsf.Post_id
                LEFT JOIN persis.FRMPPost frp   on frp.id =  pp.frmpEntry_id
                LEFT JOIN nsi.MedPersonalPost mpp   on mpp.MedPersonalPost_id =  frp.Frmr_id 
                --бирки
                INNER JOIN dbo.v_TimeTableGraf ttg   ON ttg.MedStaffFact_id = vmsf.MedStaffFact_id 
                        AND TimeTableType_id in (1,11,16,17,18) 
                        AND ttg.Person_id IS null
                         
                                        AND CAST(TimeTableGraf_begTime AS date) > CAST(cdt.curDate AS date) 
                                 
                        AND cast(TimeTableGraf_begTime as date) >= cast( '2023-03-30' as date)
                        AND cast(TimeTableGraf_begTime as date) <= cast( '2023-04-12' as date)
                         
                                AND (CAST(TimeTableGraf_begTime AS date) !=  CAST(cdt.curDate AS date)+1  or CAST(cdt.curDate AS TIME) < '17:00')
                         
                        AND (CAST(TimeTableGraf_begTime AS date) !=  CAST(cdt.curDate AS date)+20  or CAST(cdt.curDate AS TIME) >= '21:00')
                WHERE vmsf.LpuUnit_id = 426 AND pd.PersonDisp_id = 910101000436627
