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.
select cast(ceil(avg(cast(salary as decimal)) - avg(cast(replace(cast(salary as varchar(20)),'0','') as decimal))) as int) from employees;
This will work for sure in DB2. Had to cry a lot over it.
Till the end only because of rounding off trouble.
Here, we typecast the integer salary to string in order to remove zeroes. Later we convert it into decimal for average substraction for clear rounding off. Finally we type cast into integer.
Hope this helps.
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
The Blunder
You are viewing a single comment's thread. Return to all comments →
select cast(ceil(avg(cast(salary as decimal)) - avg(cast(replace(cast(salary as varchar(20)),'0','') as decimal))) as int) from employees; This will work for sure in DB2. Had to cry a lot over it. Till the end only because of rounding off trouble.
Here, we typecast the integer salary to string in order to remove zeroes. Later we convert it into decimal for average substraction for clear rounding off. Finally we type cast into integer. Hope this helps.