2

我正在尝试编写如下例所示的 sql 查询,但是,我需要它始终在当天的上午 8:00 日期和下午 4:00 的当天日期之间选择 DateEntered 字段。不知道该怎么做。有人可以帮忙吗?

SELECT     OrderNumber
    , OrderRelease
    , HeatNumber
    , HeatSuffix
    , Operation
    , COUNT(Operation) AS [Pieces Out of Tolerance]
FROM         Alerts
WHERE     (Mill = 3) 
    AND (DateEntered BETWEEN GetDate '08:00' AND GetDate '16:00')
GROUP BY OrderNumber, OrderRelease, HeatNumber, HeatSuffix, Operation
4

5 回答 5

3
DECLARE @TodayODBC varchar(30), @DateFrom datetime, @DateTo datetime
SET @TodayODBC = CONVERT(varchar(10), GETDATE(), 120)
SET @DateFrom = CONVERT(datetime, @TodayODBC + ' 08:00:00', 120)
SET @DateTo   = CONVERT(datetime, @TodayODBC + ' 16:00:00', 120)

SELECT OrderNumber, OrderRelease, HeatNumber, HeatSuffix, Operation,
    COUNT(Operation) AS [Pieces Out of Tolerance]
FROM Alerts
WHERE (Mill = 3) 
AND (DateEntered BETWEEN @DateFrom AND @DateTo)
GROUP BY OrderNumber, OrderRelease, HeatNumber, HeatSuffix, Operation

指定 ODBC 日期时间格式,无论语言环境如何,该, 120格式都是恒定的。

于 2012-06-21T15:15:57.243 回答
3
SELECT     OrderNumber
    , OrderRelease
    , HeatNumber
    , HeatSuffix
    , Operation
    , COUNT(Operation) AS [Pieces Out of Tolerance]
FROM         Alerts
WHERE     (Mill = 3) 
    AND (datediff(dd,DateEntered,getdate()) = 0 AND Datepart(hh,DateEntered) BETWEEN '08' AND '16')
GROUP BY OrderNumber, OrderRelease, HeatNumber, HeatSuffix, Operation
于 2012-06-21T15:16:48.350 回答
0

由于您使用的是 SQL Server 2008,因此您可以 CAST 为日期或时间。要在 08:00 获取当前日期,您将使用:

(CAST(CAST(getdate() AS DATE) AS DATETIME) + CAST('08:00' AS TIME)) 

要获取 16:00 的当前日期:

(CAST(CAST(getdate() AS DATE) AS DATETIME) + CAST('16:00' AS TIME)))

将其放入您的查询中:

SELECT OrderNumber
    , OrderRelease
    , HeatNumber
    , HeatSuffix
    , Operation
    , COUNT(Operation) AS [Pieces Out of Tolerance]
FROM         Alerts
WHERE     (Mill = 3) 
    AND (DateEntered 
        BETWEEN (CAST(CAST(getdate() AS DATE) AS DATETIME) + CAST('08:00' AS TIME)) 
        AND (CAST(CAST(getdate() AS DATE) AS DATETIME) + CAST('16:00' AS TIME)))
GROUP BY OrderNumber, OrderRelease, HeatNumber, HeatSuffix, Operation
于 2012-06-21T15:16:10.673 回答
0
SELECT     OrderNumber
    , OrderRelease
    , HeatNumber
    , HeatSuffix
    , Operation
    , COUNT(Operation) AS [Pieces Out of Tolerance]
FROM         Alerts
WHERE     (Mill = 3) 
    AND (DateEntered BETWEEN DATEADD(hour, 8, CONVERT(datetime,CONVERT(date,getdate()))) AND DATEADD(hour, 18, CONVERT(datetime,CONVERT(date,getdate()))))
GROUP BY OrderNumber, OrderRelease, HeatNumber, HeatSuffix, Operation
于 2012-06-21T15:16:19.463 回答
0

这可能对你有用

 DateEntered BETWEEN dateadd(hour, 8, cast(cast(getdate() as date) as datetime)) AND 
                     dateadd(hour, 16, cast(cast(getdate() as date) as datetime))

但这将(正如您的查询所做的那样)包括时间所在的行,16:00:00.000'但不包括时间所在的行16:00:00.003'

如果您打算在 17:00 之前包含所有行,请改用它。

 DateEntered >= dateadd(hour, 8, cast(cast(getdate() as date) as datetime)) AND
 DateEntered <  dateadd(hour, 17, cast(cast(getdate() as date) as datetime))
于 2012-06-21T15:27:43.290 回答