-2

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?

1
  • 4
    Because you're trying to divide by 0? Jun 9, 2012 at 21:15

2 Answers 2

4

Check for 0 before dividing:

SELECT
    ...,
    CASE WHEN SUM(NUM_PEOPLE_PARTY) = 0
         THEN NULL
         ELSE SUM(GRAND_TOTAL) / SUM(NUM_PEOPLE_PARTY) END AS CASHPERCUSTOMER,
    ... 

Or get the same result by using NULLIF:

SELECT
    ...,
    CASE SUM(GRAND_TOTAL) / NULLIF(SUM(NUM_PEOPLE_PARTY), 0) AS CASHPERCUSTOMER,
    ... 
5
  • Your first answer looks good, but your second answer is NOT the same and will not fix the problem, as it replaces a NULL denominator with a denominator of zero. Actually dividing by null is not a problem because that division would evaluate to null.
    – Mark M
    Jun 9, 2012 at 21:28
  • @MarkM: Are you confusing NULLIF with IFNULL?
    – Mark Byers
    Jun 9, 2012 at 21:30
  • This worked perfectly (the first suggestion), thank you very much for taking the time!
    – tmparisi
    Jun 9, 2012 at 21:33
  • 3
    @tmparisi so I take it you have no interest in debugging the problem and determining why you have days where a cashier had sales but served zero people? Sounds like more than just a "sweep-it-under-the-rug-using-the-query" problem. Jun 9, 2012 at 21:42
  • 1
    Thanks Aaron, but I don't have access to change database issues. Other than that, you do not understand our operation and to think you understand our business operations from a SQL script is foolish. However, so you can feel relieved, there are zeros because some businesses offer carryout service... possibly you have partaken? But thank you for your reply.
    – tmparisi
    Jun 10, 2012 at 6:33
4

You will need to run the query this way, and tell us if there are any results:

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
FROM INVOICE_TOTALS 
WHERE [DATETIME] >= '2012-06-01' 
GROUP BY CASHIER_ID, CONVERT(VARCHAR(10),[DATETIME],111) 
HAVING SUM(NUM_PEOPLE_PARTY) = 0;

If there are results, then clearly your query needs to cater for them. For example:

SELECT SERVER, [DATE], TOTAL_SALES, NUMBER_SERVED,
  CASHPERCUSTOMER = TOTAL_SALES / NULLIF(NUMBER_SERVED, 0)
FROM
(
  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
  FROM INVOICE_TOTALS 
  WHERE [DATETIME] >= '2012-06-01' 
  GROUP BY CASHIER_ID, CONVERT(VARCHAR(10),[DATETIME],111) 
);

But you should figure out why there are results with 0s since that doesn't make much sense.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.