根据条件选择班次

时间:2018-07-24 10:25:52

标签: c# sql asp.net sql-server

我有三个表,例如employee,Time slot和Scheduling。 例如

 var instance = $("#road-scroll")
  .overlayScrollbars({
  callbacks: {
   onScrollStop: function(e) {
    scrollLeft = e.target.scrollLeft;
    var scrollInfo = instance.scroll();
    let max = scrollInfo.x.max;

    if (scrollLeft === 0) {
      btnL.removeClass("btn-active");
    }

    if (scrollLeft > 0) {
      btnL.addClass("btn-active");
    }

    if (scrollLeft >= max) {
      btnR.removeClass("btn-active");
    }

    if (scrollLeft < max) {
      btnR.addClass("btn-active");
    }

   }
  }
 })
.overlayScrollbars();

我需要在Gridview的时隙中显示空闲雇员的列表,即由于EMP3是在(08:00:00 16:00:00)中分配的,因此不应在之间的任何时隙中显示它08:00:00 16:00:00,但可以显示在08:00:00之前和16:00:00之后的任何时间段中,对于所有预定的员工而言都类似。 如果没有在任何时间段安排任何员工,则该员工应该在每个时间段都可用。也就是说,EMP5应该在所有时隙中都可用。

Time Slots table : 
 id     time
  1     08:00:00    10:00:00
  2     10:00:00    12:00:00
  3     16:00:00    18:00:00
  4     08:00:00    16:00:00 
  5     14:00:00    18:00:00



Employee Table:

 EMP1
 EMP2
 EMP3
 EMP4


Scheduling table :

 EMP    TIMESLOTS ID 
 EMP1     1
 EMP2     2
 EMP3     4

任何帮助都会真的有用。谢谢!

3 个答案:

答案 0 :(得分:2)

使用cross join来生成员工和时间段的所有组合。然后使用left join(或not innot exists)过滤掉存在的内容:

select e.emp, ts.*
from employee e cross join
     timeslots ts left join
     scheduling s
     on s.emp = e.emp and s.timeslot_id = ts.timeslot_id
where s.emp is null;

答案 1 :(得分:0)

SELECT e.*, t.* 
            FROM employee e 
            CROSS JOIN 
            TimeSlot t
                WHERE NOT EXISTs (
                                    SELECT 0 FROM Scheduling s2 
                                                JOIN TimeSlot t2 
                                                        ON s2.empid = e.empid 
                                                            AND t2.endTime > t.StartTime 
                                                            AND t2.startTime < t.EndTime 

                                )  --where there is not some other overlapping timeslot allocated

答案 2 :(得分:0)

尝试一下

Declare @TimeSlots table (id int,stime time,etime time)
insert into @TimeSlots VALUES
 (1 ,'08:00:00','10:00:00')
,(2 ,'10:00:00','12:00:00')
,(3 ,'16:00:00','18:00:00')
,(4 ,'08:00:00','16:00:00')
,(5 ,'14:00:00','18:00:00')

Declare @Employee Table (Emp varchar(40))
insert into @Employee VALUES
('EMP1'),('EMP2')
,('EMP3'),('EMP4')

Declare @Scheduling table (EMP varchar(40), TIMESLOTSID int)
insert into @Scheduling VALUES
('EMP1',1),('EMP2',2),('EMP3',4)),('EMP3',3)


    ;WITH CTE
AS (
    SELECT e.Emp
        ,s.TIMESLOTSID
        ,ts.stime
        ,ts.etime
    FROM @Employee E
    LEFT JOIN @Scheduling S ON e.Emp = s.EMP
    LEFT JOIN @TimeSlots TS ON s.TIMESLOTSID = ts.id
    )
,CTE1 AS(
    SELECT c.emp
        ,c.stime
        ,c.etime
        ,oa.*
        ,CASE 
            WHEN oa.FreeStartTime >= c.stime
                AND FreeStartTime < c.etime
                THEN 0
            ELSE CASE 
                    WHEN stime >= FreeStartTime
                        AND stime < FreeEndTime
                        THEN 0
                    ELSE 1
                    END
            END RequireSlot
    FROM cte c
    OUTER APPLY (
        SELECT TS.stime FreeStartTime
            ,ts.etime FreeEndTime
        FROM @TimeSlots TS
        ) oa
)

SELECT c.emp
    ,c.FreeStartTime
    ,c.FreeEndTime
FROM CTE1 c
WHERE RequireSlot = 1
    AND NOT EXISTS (
        SELECT 1
        FROM cte1 c1
        WHERE c.emp = c1.emp
            AND c.FreeStartTime = c1.FreeStartTime
            AND c.FreeEndTime = c1.FreeEndTime
            AND c1.RequireSlot = 0
        )

如果它不能与其他样本数据一起使用,请扔掉该样本。

Alternatively,如果您有很多这样的数据要显示,则可以在应用程序级别非常容易进行处理。

您只需要2个小的结果集即可。

Select * from CTE

select * from @TimeSlots 

应用程序代码中的其余逻辑。