SQL - 错误 "Opening Recordset"

SQL - Error "Opening Recordset"

为 Volusion 进行 SQL 导出并收到导致 "Step 2 of 3 - Opening Recordset" 冻结的错误。

SELECT 
    od.ProductCode
    , SUM(od.Quantity) AS 'Total Units Sold'
    , SUM(od.TotalPrice) AS 'Total Payment Received'
    , SUM(ISNULL(od.Vendor_Price, 0) * od.Quantity) AS 'Total Cost'
    , SUM(od.TotalPrice) / SUM(od.Quantity) AS 'Average Price'
    , ((SUM(od.TotalPrice) - SUM(ISNULL(od.Vendor_Price, 0) * od.Quantity)) / SUM(od.TotalPrice)) AS 'Average Margin'
FROM OrderDetails AS od
LEFT JOIN Orders AS o
    ON o.OrderID = od.OrderID
LEFT JOIN Customers AS c 
    ON c.CustomerID = o.CustomerID
WHERE c.CustomerID >= 23
    AND c.CustomerID <> 24
    AND o.Orderstatus <> 'cancelled'
    AND c.AccessKey <> 'A'
    AND o.OrderDate BETWEEN '2/28/2015 0:00' AND '5/28/2015 23:59'
    AND o.Orderstatus NOT LIKE '%Returned'
GROUP BY 
    od.ProductCode
ORDER BY SUM(od.Quantity) DESC

完全困惑,但我已经确定了导致问​​题的行:

, ((SUM(od.TotalPrice) - SUM(ISNULL(od.Vendor_Price, 0) * od.Quantity)) / SUM(od.TotalPrice)) AS 'Average Margin'

如果删除该行,它将 return 正常。

感谢任何帮助。

谢谢,

编辑: OrderDetails 中有很多列,但有问题的三个列是:

数量,Vendor_Price,总价

您的查询没有语法错误。 正如您所说,错误出现在

, ((SUM(od.TotalPrice) - SUM(ISNULL(od.Vendor_Price, 0) * od.Quantity)) 
           / SUM(od.TotalPrice)) AS 'Average Margin'

只有两种可能

  1. 列名称拼写错误。但是上面提到的所有列也都使用了较早的列。所以很好

  2. 下一个问题可能是除以零 here.Division 可能是个问题。校验和(od.TotalPrice)总是非零..