Prepare > SQL > Aggregation > The Blunder

2023. 5. 16. 20:51HackerRank-My SQL

 

 

The Blunder | HackerRank

Query the amount of error in Sam's result, rounded up to the next integer.

www.hackerrank.com

 

문제


Samantha was tasked with calculating the average monthly salaries for all employees in the EMPLOYEES table, but did not realize her keyboard's  0 key was broken until after completing the calculation. She wants your help finding the difference between her miscalculation (using salaries with any zeros removed), and the actual average salary.

Write a query calculating the amount of error(actual - miscalculated), and round it up to the next integer.

 

=> 사만다가 사용자들의 월급 평균을 구하려는데 키보드의 0 이 망가져 잘 못 계산 했다. 실제와 오류의 차이를 구해 올림하여라.

 

 

 

 

코드


SELECT CEIL(AVG(Salary) - AVG(replace(Salary, 0, ''))) FROM EMPLOYEES;

 

 

두 번째 시도

SELECT 
CEIL(AVG(SALARY - replAce(salary, 0 , '')))
FROM EMPLOYEES;

 

 

 

 

참조


 

[MSSQL] 문자열 치환 방법 3가지 (REPLACE 함수)

SQL Server에서 문자열을 치환하기 위해서는 REPLACE 함수를 자주 사용한다. 그러나 문자열의 특정 영역을 치환 또는 마스킹 킹 처리를 할 때는 STUFF 함수를 사용하면 조금 더 편리하게 할 수 있다. 또

gent.tistory.com