So, I have a query where I am able to get some values using some formulas and that kind of stuff. When the query have values it will work fine, but if I have no values, in the divided formulas it will give me the know error
Divided by zero encountered
This is the query:
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
Do you have any idea how I can solve it?