
            with curDT as (select dbo.tzGetDate() as curDate)
            select
                tt.TimeTableMedService_id as "TimeTableMedService_id"
            from
                v_TimeTableMedService_lite tt

                inner join v_MedService MS  on MS.MedService_id = tt.MedService_id

            where
                tt.TimetableType_id not in (2,3,4)
                and MS.MedService_id = '890101000000797'
                and (
                    MS.RecordQueue_id = 1 -- Запретить всегда
                    or ( -- Запретить при наличии свободных бирок
                        tt.TimeTableMedService_begTime between (select curDate from curDT) and '2022-04-27'
                        and tt.Person_id is null
                        and MS.RecordQueue_id = 2
                    )
                )
        
