所以,我有一个查询,我可以使用一些公式和那种东西获得一些值。当查询具有值时,它将正常工作,但如果我没有值,则在分割的公式中它将给出我知道的错误
除以零
这是查询:
WITH TrashFile AS(
SELECT FileType AS[Extension],
COUNT(*) AS [Nº of files],
CAST(((COUNT(FileSize) * 100.0) / (SELECT COUNT(*) FROM InfoFile))
AS DECIMAL(10, 2)) AS[Percentage(%)],
CAST((SUM(FileSize) / 1024.0) AS DECIMAL(10,1)) AS [Total(KB)],
NULL AS [Converted to MB],
NULL AS [Converted to GB],
MIN(COUNT(*)) OVER() * 100.0 / (SUM(COUNT(*)) OVER()) AS[Min.Percentage(%)],
MAX(COUNT(*)) OVER() * 100.0 / SUM(COUNT(*)) OVER() AS[Max.Percentage(%)]
FROM InfoFile
GROUP BY FileType)
SELECT[Extension],
[Nº of files],
[Percentage(%)],
[Total(KB)],
[Converted to MB],
[Converted to GB],
NULL AS[Min.Percentage(%)],
NULL AS[Max.Percentage(%)]
FROM TrashFile
UNION ALL
SELECT '-----------------------------',
COUNT('Nº de extensions'),
((COUNT(FileType) * 100) / (SELECT COUNT(FileType) FROM InfoFile)),
CAST((SUM(FileType) / 1024.0) AS DECIMAL(10,1)),
CAST((SUM(FileType) / 1024.0 / 1024.0) AS DECIMAL(10,5)),
CAST((SUM(FileType) / 1024.0 / 1024.0 / 1024.0) AS DECIMAL(10,9)),
CAST(((SELECT MAX([Min.Percentage(%)]) FROM TrashFile))
AS DECIMAL(10,2)) AS[Min.Percentage(%)] ,
CAST((SELECT MAX([Max.Percentage(%)]) FROM TrashFile)
AS DECIMAL(10,2)) AS[Max.Percentage(%)]
FROM InfoFile
你知道我怎么解决它吗?
答案 0 :(得分:1)
在处理类似情况时,我将所有分歧插入案例陈述中:
[分母]>的情况0然后值/分母其他0结束。
答案 1 :(得分:0)
当您的SELECT COUNT(*)
子选项之一返回0条记录时,您的查询将会失败。
有两种方法可以解决这个问题:
COUNT()
ISNULL
和NULLIF
来划分和处理除以0: ISNULL(MAX(COUNT(*)) OVER() * 100.0 / NULLIF(SUM(COUNT(*)) OVER(),0) ,0)
这是有效的,因为将任意数字除以NULL
会返回NULL
,这意味着您刚刚避免了Divide by Zero
错误。然后,您可以将此NULL
替换为您要显示的内容。在上面的示例中,它将返回0
。如果您想替换其他内容以便稍后处理,请将其替换为下面的<value>
:
ISNULL(MAX(COUNT(*)) OVER() * 100.0 / NULLIF(SUM(COUNT(*)) OVER(),0) ,<value>)
答案 2 :(得分:0)
您可以使用CASE WHEN语句以避免除以零。
SELECT FileType AS[Extension],
COUNT(*) AS [Nº of files],
CASE WHEN (SELECT COUNT(*) FROM InfoFile) > 0
THEN
CAST(((COUNT(FileSize) * 100.0) / (SELECT COUNT(*) FROM InfoFile))
ELSE
NULL
END AS Alias,
... FROM Table
答案 3 :(得分:0)
本声明中有许多地方除以COUNT(*)或类似值的结果。这些COUNT值可能为零,这将导致发生DIVIDE BY ZERO错误。解决此问题的一种方法是使用NULLIF函数,该函数将使用NULL替换零值。这将导致计算返回NULL而不是DIVIDE BY ZERO错误 - 如果NULL值不是您想要的,您可以用ISNULL包围计算并用您喜欢的任何值替换NULL。
WITH TrashFile AS(SELECT FileType AS [Extension],
COUNT(*) AS [Nº of files],
CAST(((COUNT(FileSize) * 100.0) / (SELECT NULLIF(COUNT(*), 0)
FROM InfoFile))
AS DECIMAL(10, 2)) AS [Percentage(%)],
CAST((SUM(FileSize) / 1024.0) AS DECIMAL(10,1)) AS [Total(KB)],
NULL AS [Converted to MB],
NULL AS [Converted to GB],
MIN(COUNT(*)) OVER() * 100.0 / NULLIF(SUM(COUNT(*)), 0) OVER()) AS [Min.Percentage(%)],
MAX(COUNT(*)) OVER() * 100.0 / NULLIF(SUM(COUNT(*)), 0) OVER()) AS [Max.Percentage(%)]
FROM InfoFile
GROUP BY FileType)
SELECT [Extension],
[Nº of files],
[Percentage(%)],
[Total(KB)],
[Converted to MB],
[Converted to GB],
NULL AS [Min.Percentage(%)],
NULL AS [Max.Percentage(%)]
FROM TrashFile
UNION ALL
SELECT '-----------------------------',
COUNT('Nº de extensions'),
((COUNT(FileType) * 100) / (SELECT NULLIF(COUNT(FileType), 0) FROM InfoFile)),
CAST((SUM(FileType) / 1024.0) AS DECIMAL(10,1)),
CAST((SUM(FileType) / 1024.0 / 1024.0) AS DECIMAL(10,5)),
CAST((SUM(FileType) / 1024.0 / 1024.0 / 1024.0) AS DECIMAL(10,9)),
CAST(((SELECT MAX([Min.Percentage(%)]) FROM TrashFile))
AS DECIMAL(10,2)) AS[Min.Percentage(%)] ,
CAST((SELECT MAX([Max.Percentage(%)]) FROM TrashFile)
AS DECIMAL(10,2)) AS[Max.Percentage(%)]
FROM InfoFile