我正在尝试从OrderDate
(datetime
)12小时前的SQL Server数据库中提取记录。理想情况下,我想整整一个小时......例如..
如果我现在运行查询,在21.38(我的时间),查询将拉出datetime
为9.00 - 10.00的记录我不会对实际日期感到困扰,只需12小时的订单以前..这个查询将每小时运行一次,所以下次运行它将运行在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))
答案 0 :(得分:5)
使用此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)
答案 1 :(得分: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)