7

我编写了以下查询,但在 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
4

3 回答 3

20

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

Where YourDate >= (GetDate() - 90)
于 2012-06-28T06:24:14.027 回答
0

如果您想要从“当前月前 3 个月的月初”到“当前月末”的所有内容,这是您当前实际拥有的,您可以使用DATEDIFFandDATEADD一起使用:

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

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

The date literals are carefully selected to have the relationship between them that we're seeking - e.g. 20001001 is the start of the month 3 months before 20010101. 20010201 is the start of the month 1 month ahead of 20010101.

于 2012-06-28T06:32:41.287 回答
-1
select * from mytable where mydate > date_sub(now(), interval 90 day)
于 2012-06-28T06:15:05.367 回答