我已经编写了以下查询,但它给了我以下错误。你能告诉我查询中的问题在哪里吗?
错误:
SQL 错误:在“LI”附近的预期条件的上下文中指定的非布尔类型的表达式。
查询:
SELECT Orders.Day,
Orders.Month,
Orders.Year,
Orders.Orders_Count,
Orders.PaymentAmount,
Orders.Total_Payment_Authorized,
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.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,
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 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 Orders.ShipPostalCode LIKE '96704'
AND Orders.ShipPostalCode LIKE '96710'
AND Orders.ShipPostalCode LIKE '96718'
AND Orders.ShipPostalCode LIKE '96719'
AND Orders.ShipPostalCode LIKE '96720'
AND Orders.ShipPostalCode LIKE '96721'
AND Orders.ShipPostalCode LIKE '96725'
AND Orders.ShipPostalCode LIKE '96726'
AND Orders.ShipPostalCode LIKE '96727'
AND Orders.ShipPostalCode LIKE '96728'
AND Orders.ShipPostalCode LIKE '96737'
AND Orders.ShipPostalCode LIKE '96738'
AND Orders.ShipPostalCode LIKE '96739'
AND Orders.ShipPostalCode LIKE '96740'
AND Orders.ShipPostalCode LIKE '96743'
AND Orders.ShipPostalCode LIKE '96745'
AND Orders.ShipPostalCode LIKE '96749'
AND Orders.ShipPostalCode LIKE '96750'
AND Orders.ShipPostalCode LIKE '96755'
AND Orders.ShipPostalCode LIKE '96760'
AND Orders.ShipPostalCode LIKE '96764'
AND Orders.ShipPostalCode LIKE '96771'
AND Orders.ShipPostalCode LIKE '96772'
AND Orders.ShipPostalCode LIKE '96773'
AND Orders.ShipPostalCode LIKE '96774'
AND Orders.ShipPostalCode LIKE '96776'
AND Orders.ShipPostalCode LIKE '96777'
AND Orders.ShipPostalCode LIKE '96778'
AND Orders.ShipPostalCode LIKE '96780'
AND Orders.ShipPostalCode LIKE '96781'
AND Orders.ShipPostalCode LIKE '96783'
AND Orders.ShipPostalCode LIKE '96785'
GROUP BY 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