Summary
이 문제는 키보드가 고장나 Salary 에 0을 입력하지 못했을 경우에 대해서 오차를 구하는 문제였다.
기존의 Salary 의 평균 값을 가져오고 또 0 을 제외한 Salary 의 연봉을 가져와 뺀 값을 구하면 된다.
기존의 Salary 의 평균은 다음과 같이 쉽게 가져올 수 있다.
avg(salary)
다음으로 Salary 에서 0 이 없을 경우에 대한 평균을 구해야 한다.
그럼, Salary 에서 0을 제외하기 위해서 REPLACE 문법을 사용해서 0을 제거해줄 수 있었다.
replace(salary, '0', '')
그렇게 0을 제외한 Salary 의 평균을 구해주면 된다.
avg(replace(salary, '0', ''))
그리고 문제를 보면 반올림을 하라는 문구가 보여 각 평균에 대해 반올림을 해준 후에 차이 값을 구해주었다.
round(avg(salary)) - round(avg(replace(salary, '0', '')))
ROUND 문법으로 반올림 값을 구할 수 있고 CEIL 문법으로 올림 값을 구할 수 있다.
Problem
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.
Input Format
The EMPLOYEES table is described as follows:
Note: Salary is per month.
Constraints
1000 < salary < 10^5
Sample Input
Sample Output
2061
Explanation
The table below shows the salaries without zeros as they were entered by Samantha:
Samantha computes an average salary of 98.00. The actual average salary is 2159.00.
The resulting error between the two calculations is 2159.00 - 98.00. Since it is equal to the integer 2016, it does not get rounded up.
Submit Code
select round(avg(salary)) - round(avg(replace(salary, '0', '')))
from employees
Result
'알고리즘 > HackerRank' 카테고리의 다른 글
Basic Data Types - Nested Lists (Python) (0) | 2023.01.11 |
---|---|
[Python] Basic Data Types - Lists (0) | 2023.01.10 |
Basic Data Types - List Comprehensions (Python) (0) | 2023.01.08 |
Advanced Select - The PADS (SQL) (0) | 2022.12.06 |
Advanced Select - Type of Triangle (SQL) (0) | 2022.12.05 |