sql - Count dividing by 0 -


first of all, have researched everywhere query sorted , cant find answer solved issue here is.

i have query:

select     [report date],     count(case when [total_ahr_cap] = '0' or [standing_load] = '0'then null else 1 end) [zero values],     count(case when [total_ahr_cap] / [standing_load] > '12' 1 else null end) [green zone],     count(case when [total_ahr_cap] / [standing_load] < '12' , [total_ahr_cap] / [standing_load] >= '10'           1 else null end) [yellow zone],     count(case when [total_ahr_cap] / [standing_load] < '10' 1 else null end) [red zone],     count(case when [total_ahr_cap] null or [standing_load]  null 1 end)as [null values],     count(case when [total_ahr_cap] / [standing_load] > '0' 1 else null end) [total]   [dbo].[dc_chargers$] [report date] = 'march 2016' , sla_no not ('%south%') group [report date] 

bear in mind copy , paste query results several months of year. result expecting count of how many fields fall under 'green', 'yellow', 'red' , 'null' value fields bosses require info every month whenever [standing_load] value or [total_ahr_cap] values 0 - divide 0 error.

i need have count of figures re divided 0 cannot change them null values how can around everytime divide 0 error query not count month of figures have 0 in , have change them null value giving me inaccurate data.

please see below query should 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 can see, counts other months not month has 0 values.

help please!

to avoid divide 0 exception in query, in each case divide operation first check if divider greater 0. why comparing numerical values string?

also 5th case missing else part.

see code below:

select     [report date],     count(case when [total_ahr_cap] = 0 or [standing_load] = 0 null else 1 end) [zero values],     count(case when [standing_load] > 0 , [total_ahr_cap] / [standing_load] > 12 1 else null end) [green zone],     count(case when [standing_load] > 0 , [total_ahr_cap] / [standing_load] < 12 , [total_ahr_cap] / [standing_load] >= 10           1 else null end) [yellow zone],     count(case when [standing_load] > 0 , [total_ahr_cap] / [standing_load] < 10 1 else null end) [red zone],     count(case when [standing_load] > 0 , [total_ahr_cap] null or [standing_load]  null 1 else null end)as [null values],     count(case when [standing_load] > 0 , [total_ahr_cap] / [standing_load] > 0 1 else null end) [total]   [dbo].[dc_chargers$] [report date] = 'march 2016' , sla_no not ('%south%') group [report date] 

also small advice because i'm not sure if acceptable in solution i'd change count sum , nulls 0.


Comments

Popular posts from this blog

javascript - How to get current YouTube IDs via iMacros? -

c# - Maintaining a program folder in program files out of date? -

emulation - Android map show my location didn't work -