我正在尝试从12 小时前OrderDate
( ) 所在的 SQL Server 数据库中提取记录。datetime
理想情况下,我想做整个小时……例如……
如果我现在运行查询,在 21.38(我的时间),查询将提取具有datetime
9.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))