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
|
2120 Discussions
|
Please Login in order to post a comment
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;
For MySQL Platform
-- MS SQL Server --
SELECT CAST(CEILING(ABS( AVG(CAST(salary AS FLOAT)) - AVG(CAST( CASE WHEN REPLACE(CAST(salary AS VARCHAR(20)), '0', '') = '' THEN '0' ELSE REPLACE(CAST(salary AS VARCHAR(20)), '0', '') END AS FLOAT) ) )) AS INT) AS Error FROM employees;
For MySQL Platform