Count dividing by 0

Raul Gonzales

First of all, I have researched everywhere to get this query sorted and I cant find an answer that solved my issue so here it is.

I have this query:

SELECT
    [Report Date],
    COUNT(CASE WHEN [total_ahr_cap] = '0' OR [standing_load] = '0'THEN NULL ELSE 1 END) AS [Zero Values],
    COUNT(CASE WHEN [total_ahr_cap] / [standing_load] > '12' THEN 1 ELSE NULL END) AS [Green Zone],
    COUNT(CASE WHEN [total_ahr_cap] / [standing_load] < '12' AND [total_ahr_cap] / [standing_load] >= '10'
          THEN 1 ELSE NULL END) AS [Yellow Zone],
    COUNT(CASE WHEN [total_ahr_cap] / [standing_load] < '10' THEN 1 ELSE NULL END) AS [Red Zone],
    COUNT(CASE WHEN [total_ahr_cap] IS NULL OR [standing_load]  IS NULL THEN 1 END)AS [Null Values],
    COUNT(CASE WHEN [total_ahr_cap] / [standing_load] > '0' THEN 1 ELSE NULL END) AS [Total]


FROM [dbo].[dc_chargers$]
WHERE [Report Date] = 'March 2016' AND sla_no NOT LIKE ('%South%')
GROUP BY [Report Date]

Bear in mind that I copy and paste this query to get results for several months of the year. The result that I am expecting is the count of how many fields fall under the 'green', 'yellow', 'red' and 'null' value fields as my bosses require this info every month but whenever the [standing_load] value or the [total_ahr_cap] values are 0 - I get a divide by zero error.

I need to have the count of the figures that re divided by 0 as I cannot change them to NULL values so how can I get around this as everytime that I get the divide by zero error the query does not count the month of the figures that have the 0 in and I have to change them to NULL value but that is giving me inaccurate data.

please see below what the query should look like:

Report Date     Green Zone    Yellow Zone   Red Zone    Null Values Total
2015-12-01          276           5            5            16        286
2016-01-01          365           5            5            24        375
2016-02-01          376           4            5            25        384

as you can see, it counts all the other months but not the month that has 0 values.

help please!

gzaxx

To avoid Divide by 0 exception in you query, in each case where you do divide operation first check if divider is greater than 0. Also why comparing numerical values to string?

Also your 5th case is missing else part.

See code below:

SELECT
    [Report Date],
    COUNT(CASE WHEN [total_ahr_cap] = 0 OR [standing_load] = 0 THEN NULL ELSE 1 END) AS [Zero Values],
    COUNT(CASE WHEN [standing_load] > 0 and [total_ahr_cap] / [standing_load] > 12 THEN 1 ELSE NULL END) AS [Green Zone],
    COUNT(CASE WHEN [standing_load] > 0 and [total_ahr_cap] / [standing_load] < 12 AND [total_ahr_cap] / [standing_load] >= 10
          THEN 1 ELSE NULL END) AS [Yellow Zone],
    COUNT(CASE WHEN [standing_load] > 0 and [total_ahr_cap] / [standing_load] < 10 THEN 1 ELSE NULL END) AS [Red Zone],
    COUNT(CASE WHEN [standing_load] > 0 and [total_ahr_cap] IS NULL OR [standing_load]  IS NULL THEN 1 ELSE NULL END)AS [Null Values],
    COUNT(CASE WHEN [standing_load] > 0 and [total_ahr_cap] / [standing_load] > 0 THEN 1 ELSE NULL END) AS [Total]


FROM [dbo].[dc_chargers$]
WHERE [Report Date] = 'March 2016' AND sla_no NOT LIKE ('%South%')
GROUP BY [Report Date]

Also a small advice because I'm not sure if it is acceptable in your solution but I'd change count to sum and nulls to 0.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Dividing by a subquery by a count(*)

From Dev

Dividing one count() by another

From Dev

Dividing long by long returns 0

From Dev

Dividing 2 numbers returns 0

From Dev

Dividing a number in loop until it satisfies a condition = 0

From Dev

Netlogo How can I avoid the "dividing by 0" error during the dividing process compiling with Behavior space?

From Dev

x86 Assembly (NASM): Floating Point Exception, not dividing by 0?

From Dev

Eliminate #Error within Report Builder 3.0 when dividing by 0

From Dev

PHP dividing by 10, adding +1 when result is greater than .0

From Dev

Dividing two integers results 0, while I should get 1

From Dev

Dividing a list by another with some "0"s in the denominator list

From Dev

How to suppress the error message when dividing 0 by 0 using np.divide (alongside other floats)?

From Dev

Count number of 0 values

From Dev

mysql count with 0

From Dev

Oracle Count not showing 0

From Dev

sqlite include 0 in count

From Dev

include 0 in count sql

From Dev

How to display 0 in count?

From Dev

Streak Count Below 0

From Dev

MySQL: WHERE COUNT(*) = 0

From Dev

Why is PHAssetCollection Count 0?

From Dev

Count number of 0 values

From Dev

Undefined variable if count 0

From Dev

SSRS count if > 0 Expression

From Dev

Display a 0 in Count() query

From Dev

Spark SQL for dividing count from two different queries and store the output as Double

From Dev

Users count in every district including 0 count

From Dev

Selecting the count and a column value if the count is > 0

From Dev

Normalize matrix to values in [0,1] by dividing by row-wise sum