Hi there,
I'm trying to do some reporting with subqueries, where some of the columns are operations between two subqueries.
The problem lies in that there is a possibilty that one of the denominators could be zero. I have tried checking for this with a CASE or ISNULL, but I still get a divide by zero error.
This is an example of what I'm trying to do. Any help would be appreciated, either if I have a mistake, or a better way:
ISNULL:
SELECT *, (SELECT COUNT(*) FROM [table1]) /
ISNULL((SELECT COUNT(*) FROM [table2]),1)
FROM [table3]
CASE:
SELECT *, CASE
WHEN (SELECT COUNT(*) FROM [table2]) IS NULL
THEN 0
ELSE (SELECT COUNT(*) FROM [table1]) /
(SELECT COUNT(*) FROM [table2])
FROM [table3]
That's the basic gist of it.
Thank you for your future help!
I'm trying to do some reporting with subqueries, where some of the columns are operations between two subqueries.
The problem lies in that there is a possibilty that one of the denominators could be zero. I have tried checking for this with a CASE or ISNULL, but I still get a divide by zero error.
This is an example of what I'm trying to do. Any help would be appreciated, either if I have a mistake, or a better way:
ISNULL:
SELECT *, (SELECT COUNT(*) FROM [table1]) /
ISNULL((SELECT COUNT(*) FROM [table2]),1)
FROM [table3]
CASE:
SELECT *, CASE
WHEN (SELECT COUNT(*) FROM [table2]) IS NULL
THEN 0
ELSE (SELECT COUNT(*) FROM [table1]) /
(SELECT COUNT(*) FROM [table2])
FROM [table3]
That's the basic gist of it.
Thank you for your future help!