WHERE子句中的日期范围从90天前到今天的日期

时间:2012-06-28 06:13:59

标签: sql sql-server tsql

我编写了以下查询,但在WHERE子句中,我使用了日期范围的特定日期。我希望自动计算日期范围 - 也就是说,直到今天为止的90天。我该怎么办?

SELECT Orders.Day, 
Orders.Month,
Orders.Year, 
Orders.Orders_Count,Orders.PaymentAmount, 
Orders.Total_Payment_Authorized,
Orders.ShipCity,   
Orders.Total_Payment_Received, 
Orders.SalesTax1, 
Orders.SalesTax2, 
Orders.SalesTax3, 
Orders.TotalShippingCost, 
Orders.OrderDetails_Count,   
Orders.Quantity,
Orders.COGS, 
Orders.Profit,
Orders.ProfitMargin  
FROM (SELECT Orders.Day,Orders.Month,Orders.Year,Orders.Orders_Count,  Orders.PaymentAmount,Orders.Total_Payment_Authorized,Orders.ShipCity,Orders.Total_Payment_Received,Orders.SalesTax1,Orders.SalesTax2,Orders.SalesTax3,  Orders.TotalShippingCost,OrderDetails.OrderDetails_Count, OrderDetails.Quantity,OrderDetails.COGS,OrderDetails.Profit,OrderDetails.ProfitMargin   
FROM (SELECT DATEPART(DD, Orders.OrderDate) AS Day, DATEPART(MM, Orders.OrderDate) AS Month,DATEPART(YY, Orders.OrderDate) AS Year,    Max(Orders.OrderDate) As OrderDate ,Max(Orders.ShipDate) As ShipDate, Count(Orders.OrderID) As Orders_Count, Sum(Orders.PaymentAmount) AS PaymentAmount, Sum(Orders.Total_Payment_Authorized) AS Total_Payment_Authorized,Orders.ShipCity AS ShipCity,   
Sum(Orders.Total_Payment_Received) AS Total_Payment_Received, Sum(Orders.SalesTax1) AS SalesTax1,Sum(Orders.SalesTax2) AS SalesTax2 ,  Sum(Orders.SalesTax3) AS SalesTax3, Sum(Orders.TotalShippingCost) AS TotalShippingCost 
FROM (((Orders WITH(NOLOCK) LEFT JOIN PaymentMethods    WITH(NOLOCK) ON Orders.PaymentMethodID = PaymentMethods.PaymentMethodID) 
LEFT JOIN ShippingMethods WITH(NOLOCK) ON    Orders.ShippingMethodID = ShippingMethods.ShippingMethodID) 
LEFT JOIN Customers WITH(NOLOCK) ON Orders.CustomerID = Customers.CustomerID)     
WHERE Orders.OrderStatus <> 'Cancelled' GROUP BY Orders.ShipCity, DATEPART(DD, Orders.OrderDate),DATEPART(MM, Orders.OrderDate),DATEPART(YY, Orders.OrderDate))  Orders   
INNER JOIN (SELECT DATEPART(DD, Orders.OrderDate) AS Day,DATEPART(MM, Orders.OrderDate) AS Month,DATEPART(YY, Orders.OrderDate) AS Year,  Count(OrderDetails.OrderDetailID) As OrderDetails_Count,
Sum(OrderDetails.Quantity) AS Quantity,  Sum(OrderDetails.Vendor_Price * OrderDetails.Quantity) AS COGS,
Case When SUM(Vendor_Price) IS NULL Then null Else Sum((Case When OrderDetails.ProductCode like 'DSC-%' Then 
OrderDetails.ProductPrice - ISNULL(OrderDetails.Vendor_Price,0)  
Else OrderDetails.ProductPrice - OrderDetails.Vendor_Price End) * OrderDetails.Quantity) End AS Profit,     
CASE SUM(OrderDetails.ProductPrice * OrderDetails.Quantity) WHEN 0 THEN 0      
ELSE ROUND(((SUM(OrderDetails.ProductPrice * OrderDetails.Quantity) - SUM(OrderDetails.Vendor_Price * OrderDetails.Quantity)) / SUM(OrderDetails.ProductPrice * OrderDetails.Quantity)) * 100, 1)   END AS ProfitMargin       
FROM (((Orders WITH(NOLOCK) LEFT JOIN PaymentMethods WITH(NOLOCK) ON Orders.PaymentMethodID = PaymentMethods.PaymentMethodID)       
LEFT JOIN ShippingMethods WITH(NOLOCK) ON Orders.ShippingMethodID = ShippingMethods.ShippingMethodID) LEFT JOIN Customers       
WITH(NOLOCK)  ON Orders.CustomerID = Customers.CustomerID) LEFT JOIN OrderDetails WITH(NOLOCK) ON Orders.OrderID = OrderDetails.OrderID     
WHERE Orders.OrderStatus <> 'Cancelled' AND DATEDIFF(dd, Orders.OrderDate, '03/01/2012') <= 0 AND DATEDIFF(dd, Orders.OrderDate, '06/30/2012') >= 0 AND Orders.ShipPostalCode IN ('96704','96710','96718','96719','96720','96721','96725','96726','96727','96728','96737','96738','96739','96740','96743','96745','96749','96750','96755','96760','96764','96771','96772','96773','96774','96776','96777','96778','96780','96781','96783','96785') 
GROUP BY Orders.ShipCity, DATEPART(DD, Orders.OrderDate),DATEPART(MM, Orders.OrderDate), DATEPART(YY, Orders.OrderDate)) OrderDetails ON    Orders.Day = OrderDetails.Day AND Orders.Month = OrderDetails.Month AND  Orders.Year = OrderDetails.Year) Orders 
ORDER BY Orders.Day DESC,   Orders.Month DESC,Orders.Year DESC

3 个答案:

答案 0 :(得分:12)

如果是Microsoft SQL 2000或更高版本(05,08,10),那么只需使用:

Where YourDate >= (GetDate() - 90)

答案 1 :(得分:0)

如果您想要从“当前月份前3个月”到“当月结束”(这是您目前实际拥有的那一天)的所有内容,您可以使用DATEDIFF和{{ 3}}在一起:

WHERE
    Orders.OrderDate >= DATEADD(month,DATEDIFF(month,20010101,CURRENT_TIMESTAMP),'20001001') and
    Orders.OrderDate < DATEADD(month,DATEDIFF(month,20010101,CURRENT_TIMESTAMP),'20010201')

虽然我不确定将来OrderDate意味着什么。

仔细选择日期文字以获得我们正在寻找的关系 - 例如20001001是20010101之前3个月的月初.20010201是20010101之前1个月的月初。

答案 2 :(得分:-1)

select * from mytable where mydate > date_sub(now(), interval 90 day)