SQLTeam.com | Weblogs | Forums

Computing division in a select statement


I built the query below. Its a simple query. In the SELECT clause, the first statement is doing a count distinct of total visits in the data, second statement in the SELECT clause is doing a count distinct of visits while using a condition in the IIF function, which gives me the number of visits that "bounced". The third statement in the SELECT clause is supposed to give me the bounced visits over total visits, which is what I'm trying to do. For example, for the first record, I'm trying to calculate 1540455/3866818. But in the results I'm getting zeros down the column. Could somebody please tell me what I'm doing wrong?

Your results look correct as 1540455/3866818 = 0
(ie An integer divided by an integer equals an integer.)

If you want decimal places you need to convert to a data type which allows them. The easiest way to do this is to multiply by 1.0:

SELECT 1540455 * 1.0 / 3866818

1 Like