简体   繁体   English

对于没有数据的查询,除以零错误返回

[英]Divide by zero error returns for queries with no data

I'm in the process of trying to figure out a way to solve this divide by zero error. 我正在试图找出一种解决零误差的方法。 The current challenge I'm facing comes from this question I asked not too long ago. 我面临的当前挑战来自于我不久前提出的这个问题。

Here's the Code I'm using that I'm plugging into reports stretching out into the end of the year: 这是我正在使用的代码,我正在插入延伸到年底的报告:

SELECT CAST(ROUND((.01 * 2541) / Count(date_record_entered), 1) AS decimal(5,1)) 
FROM dbo.tablename
WHERE date_record_entered IS NOT NULL 
and date_record_entered >= 20131201 
AND date_record_entered <= 20131231

When it comes to current or previous months reports, there aren't any issues with the data populating. 对于当前或上个月的报告,填充数据没有任何问题。 However, when the reports past the current month are accessed, I get the following error in SQL (and a similar error in C#/ASP.NET): 但是,当访问当前月份的报告时,我在SQL中遇到以下错误(以及C#/ ASP.NET中的类似错误):

Msg 8134, Level 16, State 1, Line 1
Divide by zero error encountered

Here's what I've tried with no success. 这是我尝试过没有成功的事情。

Sample #1 样品#1

SELECT CASE WHEN date_record_entered = 0 then null
ELSE CAST(ROUND(.01 * 2541 / COUNT(date_record_entered), 
AS deciamal (5,1)
FROM dbo.tablename 
WHERE date_record_entered IS NOT NULL 
AND date_record_entered >= 20140201 
and date_record_entered <= 20140228

Sample #2 样品#2

SELECT CAST(ROUND((.01 * 2541) / NULLIF(Count(date_record_entered), 1) 
AS decimal(5,1))) 
FROM dbo.tablename 
WHERE date_record_entered IS NOT NULL 
and date_record_entered >= 20140201 
AND date_record_entered <= 20140228

Sample #3 样品#3

SET ARITHABORT OFF
SET ANSI_WARNINGS OFF

SELECT CAST(ROUND(.01 * 2541 / COUNT(date_record_entered ), 1) AS decimal(5,1)) 
FROM dbo.tablename  
WHERE date_record_entered IS NOT NULL 
AND date_record_entered >= 20140201 
and date_record_entered  <= 20140228

Now this third one works in SQL but I haven't found a way to do this with a gridview/c#. 现在这第三个在SQL中工作,但我还没有找到一种方法来使用gridview / c#。

I know this may be something really simple but I'm just spinning my wheels at this point. 我知道这可能是非常简单的事情,但我现在只是在转动我的车轮。

Shouldn't the second solution attempt be ... 第二种解决方案尝试不应该......

SELECT CASE WHEN COUNT(date_record_entered) = 0 then null ELSE CAST(ROUND(.01 * 2541 / COUNT(date_record_entered), AS decimal (5,1)) END FROM dbo.tablename WHERE date_record_entered IS NOT NULL AND date_record_entered >= 140201 and date_record_entered <= 140228

With a COUNT() in the CASE WHEN 在CASE WHEN中使用COUNT()

Sample 2 has: NULLIF(Count(date_record_entered), 1) 示例2具有:NULLIF(Count(date_record_entered),1)

Instead, try: NULLIF(Count(date_record_entered), 0) 相反,尝试:NULLIF(Count(date_record_entered),0)

NULLIF will return the value in the first argument unless it is equal to the value in the second argument. NULLIF将返回第一个参数中的值,除非它等于第二个参数中的值。 If they are equal, NULLIF returns NULL. 如果它们相等,则NULLIF返回NULL。

SELECT CAST(ROUND((.01 * 2541) / NULLIF(Count(date_record_entered), 1), 0) AS decimal(5,1))
FROM dbo.tablename 
WHERE date_record_entered IS NOT NULL 
and date_record_entered >= 20140201 
AND date_record_entered <= 20140228

Using NULLIF() works because dividing by NULL returns NULL : 使用NULLIF()有效的,因为除以NULL返回NULL

SELECT CAST((.01 * 2541) / NULLIF(COUNT(date_record_entered), 0) AS DECIMAL(5,1)) 
FROM dbo.tablename
WHERE date_record_entered >= 20131201 
  AND date_record_entered <= 20131231

Using CASE : 使用CASE

SELECT CASE WHEN COUNT(date_record_entered) = 0 THEN NULL
            ELSE CAST((.01 * 2541) / COUNT(date_record_entered) AS DECIMAL(5,1))
       END
FROM dbo.tablename 
WHERE date_record_entered >= 20140201 
  AND date_record_entered <= 20140228

Suppressing warnings can usually be avoided. 通常可以避免抑制警告。 date_record_entered can never be both NULL AND be between two dates, so I removed that part of the WHERE criteria. date_record_entered永远不能同时为NULL并且在两个日期之间,所以我删除了WHERE条件的那一部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM