I'm experiencing a problem with a query that I've written.
Given a table with the schema:
create table somevals(division varchar(100),
subdivision varchar(100),
good float,
bad float);
I would like to find, for each unique (division, subdivision), the sum of good values, the sum of bad values, and the ratio of good to bad values. So, I wrote the query:
select division, subdivision, g, b, g / b as ratio
from (select division, subdivision, sum(good) as g, sum(bad) as b
from somevals
group by division, subdivision
) as temp
The problem is that this returns far fewer rows than I would expect. However, if I remove the division (g / b as ratio), this query returns the correct number of rows in the result (about three times as many). None of the columns contain Null values. None of the bad values are less than or equal to zero, so sum(bad) will always be non-zero.
I am at a loss for how this could be happening. Under what circumstances can adding a division expression in the select statement reduce the number of rows in the result?
bwould be 0? – cheesemacfly Mar 13 at 19:38ARITHABORT. Either way, it wouldn't simply exclude rows. ...What happens if you eliminate the outer query, and simply addSUM(good) / SUM(bad) AS ratioto the SELECT list? – Dan J Mar 13 at 19:43