1

我正在尝试从12 小时前OrderDate( ) 所在的 SQL Server 数据库中提取记录。datetime理想情况下,我想做整个小时……例如……

如果我现在运行查询,在 21.38(我的时间),查询将提取具有datetime9.00 - 10.00的记录小时,因此下一次运行时,它将在 22.38 运行,并将在 10.00 - 11.00 之间拉订单。

这是我尝试过的:

SELECT TOP (100) PERCENT 
    dbo.[Order].OrderID, 
    dbo.Customer.Forename, 
    dbo.Customer.Surname, 
    dbo.Customer.Email, 
    dbo.[Order].OrderDate, 
    dbo.[Order].OrderStatusID, 
    dbo.[Order].WebsiteID, 
    dbo.Addresses.CountryID
FROM dbo.[Order] 
INNER JOIN dbo.Customer 
    ON dbo.[Order].CustomerID = dbo.Customer.CustomerID
INNER JOIN dbo.Addresses 
    ON dbo.[Order].BillingAddressID = dbo.Addresses.AddressID 
    AND dbo.[Order].DeliveryAddressID = dbo.Addresses.AddressID
    AND dbo.Customer.CustomerID = dbo.Addresses.CustomerID
WHERE (dbo.[Order].WebsiteID IN (1, 2, 8, 12)) 
  AND (dbo.[Order].OrderStatusID = 1) 
  AND (dbo.[Order].OrderDate >= DATEADD(hour, - 12, GETDATE())) 
  AND (dbo.[Order].OrderDate <= DATEADD(hour, - 11, GETDATE()))

更新:

这是我目前正在尝试的正确查询

SELECT     TOP (100) PERCENT dbo.[Order].OrderID, dbo.Customer.Forename,     dbo.Customer.Surname, dbo.Customer.Email, dbo.[Order].OrderDate,     dbo.[Order].OrderStatusID, dbo.[Order].WebsiteID, 
                       dbo.Addresses.CountryID
FROM         dbo.[Order] INNER JOIN
                      dbo.Customer ON dbo.[Order].CustomerID = dbo.Customer.CustomerID     LEFT OUTER JOIN
                      dbo.Addresses ON dbo.[Order].DeliveryAddressID =     dbo.Addresses.AddressID AND dbo.Customer.CustomerID = dbo.Addresses.CustomerID
WHERE     (dbo.[Order].WebsiteID IN (1, 2, 8, 12)) AND (dbo.[Order].OrderStatusID = 1)     AND (dbo.[Order].OrderDate >= DATEADD(Hour, DATEDIFF(Hour, 0, GETDATE()) - 12, 0))     AND 
                      (dbo.[Order].OrderDate < DATEADD(Hour, DATEDIFF(Hour, 0,     GETDATE()) - 11, 0))
4

2 回答 2

7

改用这个 where 子句

WHERE   dbo.[Order].WebsiteID IN (1, 2, 8, 12) 
        AND dbo.[Order].OrderStatusID = 1 
        AND OrderDate >=  DateAdd(Hour, DateDiff(Hour, 0, GetDate())-12, 0)
        AND OrderDate <   DateAdd(Hour, DateDiff(Hour, 0, GetDate())-11, 0)
于 2013-05-17T20:48:25.793 回答
0

这应该有效(我清理了你的空白):

declare @topofhour datetime
SELECT @topofhour = dateadd(hour,-11,dateadd(hour,datediff(hour,0,getdate()),0))

--checking against the provided scenario, since i've buggered this up twice
--
--declare @topofhour  datetime 
--select @topofhour  = '05/17/2013 21:38:00'
--select @topofhour  = dateadd(hour,-11,dateadd(hour,datediff(hour,0,@topofhour ),0))
--select dateadd(hour,-1,@topofhour), @topofhour
--
-- -- results are `2013-05-17 09:00:00.000,2013-05-17 10:00:00.000`

SELECT     TOP (100) PERCENT dbo.[Order].OrderID
            , dbo.Customer.Forename
            , dbo.Customer.Surname
            , dbo.Customer.Email
            , dbo.[Order].OrderDate
            , dbo.[Order].OrderStatusID
            , dbo.[Order].WebsiteID
            , dbo.Addresses.CountryID 
FROM         dbo.[Order] 
INNER JOIN  dbo.Customer 
ON          dbo.[Order].CustomerID = dbo.Customer.CustomerID 
INNER JOIN  dbo.Addresses
AND         dbo.[Order].DeliveryAddressID = deliveryAddress.AddressID 
AND         dbo.Customer.CustomerID = deliveryAddress.CustomerID
WHERE       (dbo.[Order].WebsiteID IN (1, 2, 8, 12)) 
AND         (dbo.[Order].OrderStatusID = 1) 
AND         (dbo.[Order].OrderDate between dateadd(hour,-1,@topofhour) and @topofhour)

更新: 我重做了这个。请尝试运行它。如果它返回您所期望的,则添加其他 WHERE 行。

DECLARE     @topDate datetime, @bottomDate datetime

SELECT      @topDate        = DATEADD(Hour, DATEDIFF(Hour, 0, GETDATE()) - 12, 0)
            ,@bottomDate    = DATEADD(Hour, DATEDIFF(Hour, 0, GETDATE()) - 11, 0)

SELECT     TOP (100) PERCENT o.OrderID
            , c.Forename
            , c.Surname
            , c.Email
            , o.OrderDate
            , o.OrderStatusID
            , o.WebsiteID
            , a.CountryID
FROM        dbo.[Order] as o
left join   dbo.Customer as c
ON          o.CustomerID = c.CustomerID     
left join   dbo.Addresses as a
ON          o.DeliveryAddressID = a.AddressID 
AND         c.CustomerID = a.CustomerID
WHERE       (o.OrderDate BETWEEN @bottomDate AND @topDate)
--AND         (o.WebsiteID IN (1, 2, 8, 12)) 
--AND         (o.OrderStatusID = 1)     
于 2013-05-17T20:49:38.063 回答