1

Making an SQL export for Volusion and getting an error that causes a freeze on "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

Completely perplexed, but I have identified the line causing the issue:

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

If that line is removed it will return fine.

Any help is appreciated.

Thanks,

Edit: There are a lot of columns in OrderDetails, but the three in question would be:

Quantity, Vendor_Price, TotalPrice

4

1 回答 1

0

您的查询中没有语法错误。正如你所说,错误即将到来

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

只有两种可能

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

  2. 下一个问题可能是除法发生在这里。除以零可能是一个问题。检查总和(od.TotalPrice)总是非零..

于 2015-05-28T19:33:42.673 回答