1

我设置了我的程序以允许用户选择一个电台,然后在 2 个日期之间进行选择,然后他们选择他们想要的班次。他们可以选择两者、第一或第二。第一个是早上 6:00 - 下午 4:00,第二个是下午 4:00 - 凌晨 3:00。我总共有4个参数。我认为我的程序没有返回第二班次的值,因为并非所有介于 16 和 3 之间的值都大于开始,小于结束。例如,下午 6:00 不小于 3 的结束时间,因此它不包含在输出中。我如何实现这一目标?这是我的代码:

DECLARE @StartHour INT
DECLARE @EndHour INT
SET @StartHour = CASE @TimeRange 

WHEN 0 THEN 0 --Start of Overall--
WHEN 1 THEN 6 --1st Shif Start--
WHEN 2 THEN 16 --2nd Shift Start--
END

SET @EndHour = CASE @TimeRange   
WHEN 0 THEN 24 --End of Overall--
WHEN 1 THEN 16 --End of First Shift--
WHEN 2 THEN 3 --End of Second Shift--
END

SELECT
Testerline1_CycleTimes.Station,
Testerline1_CycleTimes.StationEntry

WHERE
Testerline1_CycleTimes.Station LIKE @Station
AND Testerline1_CycleTimes.StationEntry Between @Start And @End
AND DATEPART( hh , Testerline1_CycleTimes.StationEntry ) >= @StartHour
AND DATEPART( hh , Testerline1_CycleTimes.StationEntry ) < @EndHour
4

1 回答 1

1

错过了最后一天的回答,但如果你还没有解决问题。我建议尝试将 union 用于您的第二班数据。随着在时间范围之间选择的日期变化

/*******************************************************
   --Commented Code
    DECLARE @StartHour INT
    DECLARE @EndHour INT
    SET @StartHour = CASE @TimeRange 

    WHEN 0 THEN 0 --Start of Overall--
    WHEN 1 THEN 6 --1st Shif Start--
    WHEN 2 THEN 16 --2nd Shift Start--
    END

    SET @EndHour = CASE @TimeRange   
    WHEN 0 THEN 24 --End of Overall--
    WHEN 1 THEN 16 --End of First Shift--
    WHEN 2 THEN 3 --End of Second Shift--
   END 
  *******************************************/

 If(@TimeRange=1)
 BEGIN
 SELECT
    Testerline1_CycleTimes.Station,
    Testerline1_CycleTimes.StationEntry
 WHERE
    sterline1_CycleTimes.Station LIKE @Station
    AND Testerline_CycleTimes.StationEntry Between @Start And @End
    AND DATEPART( hh , Testerline1_CycleTimes.StationEntry ) >= 6
    AND DATEPART( hh , Testerline1_CycleTimes.StationEntry ) < 16
END

ELSE IF(@TimeRange=2)
BEGIN
SELECT
   Testerline1_CycleTimes.Station,
   Testerline1_CycleTimes.StationEntry  
 WHERE
    Testerline1_CycleTimes.Station LIKE @Station
    AND Testerline1_CycleTimes.StationEntry Between @Start And @End
    AND DATEPART( hh , Testerline1_CycleTimes.StationEntry ) >= 16
UNION ALL
SELECT
     Testerline1_CycleTimes.Station,
     Testerline1_CycleTimes.StationEntry
WHERE
    Testerline1_CycleTimes.Station LIKE @Station
    AND Testerline1_CycleTimes.StationEntry Between @Start And @End
    AND DATEPART( hh , Testerline1_CycleTimes.StationEntry ) < 3
END

 ELSE
 BEGIN
 SELECT
      Testerline1_CycleTimes.Station,
      Testerline1_CycleTimes.StationEntry
 WHERE
     Testerline1_CycleTimes.Station LIKE @Station
     AND Testerline1_CycleTimes.StationEntry Between @Start And @End
 END
于 2013-03-13T05:45:47.777 回答