본문 바로가기
SQL/SQL test

[HackerRank] The Blunder 풀이

by ISLA! 2023. 12. 23.

문제

 

Samantha was tasked with calculating the average monthly salaries for all employees in the EMPLOYEES table, but did not realize her keyboard's  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 (i.e.:  average monthly salaries), and round it up to the next integer.

 

👉 키보드 고장으로 '0' 이 입력되지 않은 데이터 셋의 평균과 실제 데이터셋(0이 제대로 기입된 데이터)의 평균 차를 구하기 (반올림하여 정수로)

 

Input Format

The EMPLOYEES table is described as follows:

Note: Salary is per month.

Sample Input (실제 데이터) : 즉, 여기서 0이 빠진 데이터는 따로 평균 내야 함

 


풀이

 

  • 실제 평균은 AVG(컬럼명)
  • 0이 빠진 데이터를 만들기 위해 REPLACE 사용 : REPLACE(컬럼명, '대체가 필요한 문자 또는 숫자', '대체할 문자 또는 숫자')
  • 동일하게 AVG로 해당 값의 평균을 내줌
SELECT CEIL(AVG(Salary) - AVG(REPLACE(Salary, '0', '')))
FROM EMPLOYEES;

 

 

728x90