Here is my code:
SELECT CASHIER_ID AS SERVER, CONVERT(VARCHAR(10),[DATETIME],111) AS DATE,
SUM(GRAND_TOTAL) AS TOTAL_SALES,
SUM(NUM_PEOPLE_PARTY) AS NUMBER_SERVED,
SUM(GRAND_TOTAL) / SUM(NUM_PEOPLE_PARTY) AS CASHPERCUSTOMER
FROM INVOICE_TOTALS
WHERE [DATETIME] >= '2012-06-01'
GROUP BY CASHIER_ID, CONVERT(VARCHAR(10),[DATETIME],111)
Everything works perfect with the code except for the:
SUM(GRAND_TOTAL) / SUM(NUM_PEOPLE_PARTY) AS CASHPERCUSTOMER
portion. If I take this line out then everything works perfect, If I put it in I receive the following error:
Message: Could not retrieve datatable.
SELECT CASHIER_ID AS SERVER, …rest of query above…Stack Trace:
…irrelevant stack trace…Message: Divide by zero error encountered.
Stack Trace:
…irrelevant stack trace…
This is SQL inside a restaurant point-of-sale (POS) system that uses SQL Server. Any ideas why this may be occuring?
Check for 0 before dividing:
Or get the same result by using
NULLIF: