Skip to content
Advertisement

AVG() of the quotient of two fields giving unexpected output in SQL

I have a SQL table and I need to find the average of the quotient of two fields (rating, position) grouped by another field (query_name). I cannot make sense of why the following two queries don’t give the same result:

Query 1:

Query 2:

Here is the table:

This is the expected output:

However, Query 1 gives the following:

Advertisement

Answer

Some databases do integer division, so 1/2 is 0 rather than 0.5. If this is the case, then your numbers will be off.

It is easily fixed. I just multiply by 1.0:

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement