We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
- Prepare
- SQL
- Aggregation
- The Blunder
- Discussions
The Blunder
The Blunder
Sort by
recency
|
2124 Discussions
|
Please Login in order to post a comment
SELECT CEILING( AVG(salary) - AVG(CAST(REPLACE(salary, '0', '') AS UNSIGNED)) ) AS error FROM employees;
SELECT CAST( CEILING(AVG(CAST(salary AS FLOAT)) - AVG(CAST(REPLACE(CAST(salary AS VARCHAR(21)), '0', '') AS FLOAT))) AS INT) FROM Employees;
In SQL Server: Question is to round up to the "next integer". Hence, we used CEILING( ) function.
SELECT CAST(CEILING(AVG(CAST(salary AS FLOAT)) - AVG(CAST(REPLACE(CAST(salary AS VARCHAR(21)), '0', '') AS FLOAT))) AS INT) FROM Employees;
Like me, you may keep getting 2252 as the wrong answer (in DB2)
The solution is to use DECIMAL to perform the calculation using floats and then use INT on the total. You will then get the correct answer 2253.
SELECT INT(CEIL( AVG(DECIMAL(salary)) - AVG(DECIMAL(replace(char(salary), '0', ''))) )) FROM employees;