[TIL] MySQL TIL Day 2

์ตœ๋Œ€ 1 ๋ถ„ ์†Œ์š”

๐ŸŸข [HackerRank] The Blunder

https://www.hackerrank.com/challenges/the-blunder/problem?isFullScreen=true

Solution

์ง‘๊ณ„ํ•จ์ˆ˜(Aggregation)์„ ์ด์šฉํ•˜์—ฌ ํ•ด๊ฒฐํ•˜๋Š” ๋ฌธ์ œ์ด๋‹ค. ๊ณ„์‚ฐ๊ธฐ์˜ 0์ด ๊ณ ์žฅ๋‚˜๊ธฐ ์ „, ํ›„์˜ ํ‰๊ท  ์ฐจ์ด๋ฅผ ๊ณ„์‚ฐํ•œ ํ›„ ์˜ฌ๋ฆผ์„ ํ•ด์ค˜์•ผ ํ•œ๋‹ค.

  1. 0์„ ์ œ๊ฑฐํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” REPLACE๋ฅผ ์ด์šฉํ•˜๋ฉด ๋œ๋‹ค. ๊ทธ๋Ÿฌ๋ฉด 0์„ ์ œ๊ฑฐํ•œ ์ˆซ์ž๋งŒ ๋‚จ๊ฒŒ ๋œ๋‹ค. (ex: 909 -> 99)
  2. ์˜ฌ๋ฆผ์€ CEIL ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•œ๋‹ค.

REPLACE(๋ฌธ์ž/์ˆซ์ž, ์›๋ž˜ ๋ฌธ์ž, ๋ฐ”๊ฟ€ ๋ฌธ์ž)

SELECT CEIL(AVG(salary) - AVG(REPLACE(salary, 0, '')))
FROM employees

๋Œ“๊ธ€๋‚จ๊ธฐ๊ธฐ