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
+ 1 comment MySQL
SELECT CEILING(AVG(SALARY) - AVG(REPLACE(SALARY,'0',''))) FROM EMPLOYEES
+ 1 comment SQL Server The problem here is that
AVG()
returns an integer when used on integers. So the result will be wrong. You have to convert the values to float before usingAVG()
. This worked for me:select cast(ceiling(avg(cast(salary as float)) - avg(cast(replace(convert(varchar(5), salary), "0", "") as float))) as int) from employees
+ 0 comments SELECT CEILING ((AVG (salary)) - AVG (REPLACE (salary,0,""))) AS avg_sal FROM EMPLOYEES
+ 0 comments SELECT CEILING ((AVG (salary)) - AVG (REPLACE (salary,0,""))) AS avg_sal FROM EMPLOYEES
+ 0 comments MS SQL SEVER: - Salary : INT => AVG Salary: INT > FLOAT - REPLACE (salary,0,""): VARCHAR > FLOAT - CEILING: FLOAT => NUMERIC OR INT
SELECT CAST (CEILING (AVG (CAST (salary AS FLOAT)) - AVG (CAST(REPLACE (salary,0,"") AS FLOAT))) AS NUMERIC) FROM EMPLOYEES
Load more conversations
Sort 1151 Discussions, By:
Please Login in order to post a comment