Exercise #53 (tips and solutions)

Here is a solution where an attempt at rounding is made.

SELECT CAST(ROUND(AVG(numguns), 2) AS DECIMAL(10,2)) Avg_numGuns
FROM Classes
WHERE type = 'bb';
mssql
🚫
[[ error ]]
[[ column ]]
[[ value ]]

The attempt is unsuccessful, since rounding as well as further casting to DECIMAL type (which is redundant in the given context, though) is carried out for a value which is already integer. In consequence, we manage to get not more than two zeroes after decimal point.

The problem lies in the fact that AVG(numguns) is applied to an integer argument, therefore the result is cast to integer as well, fractional part being discarded rather than rounded, which is a peculiarity of SQL Server rather than the general rule.

To return to discussion of exercise #53

To solve a problem on SQL-EX.RU