Growth ๐ŸŒณ/Practice ๐Ÿ’ป

[Leet Code] 1873. Calculate Special Bonus

์ธ” 2023. 10. 24. 23:34

๐Ÿ“ข ๋ณธ ํฌ์ŠคํŒ…์— ํ™œ์šฉ๋˜๋Š” ๊ธฐ๋ณธ ๋ฌธ์ œ ๋ฐ ์ž๋ฃŒ ์ถœ์ฒ˜๋Š”

       ๋ฆฌํŠธ์ฝ”๋“œ Problems, https://leetcode.com/problemset/all/์ž„์„ ๋ฐํž™๋‹ˆ๋‹ค.


โœ” ๋ฌธ์ œ

https://leetcode.com/problems/calculate-special-bonus/description/

 

Calculate Special Bonus - LeetCode

Can you solve this real interview question? Calculate Special Bonus - Table: Employees +-------------+---------+ | Column Name | Type | +-------------+---------+ | employee_id | int | | name | varchar | | salary | int | +-------------+---------+ employee_i

leetcode.com

 

Table : Employees

 

Write a solution to calculate the bonus of each employee. The bonus of an employee is 100% of their salary if the ID of the employee is an odd number and the employee's name does not start with the character 'M'. The bonus of an employee is 0 otherwise.

Return the result table ordered by employee_id.


โœ” ํ’€์ด

  ๋ฌธ์ œ ์š”๊ตฌ์‚ฌํ•ญ  

 

๊ทผ๋กœ์ž๋ณ„ ๋ณด๋„ˆ์Šค๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ๋ฌธ์ œ์ด๋‹ค. ๋‹จ  ๋ณด๋„ˆ์Šค ์ง€๊ธ‰ ๋Œ€์ƒ์€ employee_idd๊ฐ€ ํ™€์ˆ˜์ด๊ณ  ์ด๋ฆ„์ด 'M'์œผ๋กœ ์‹œ์ž‘ํ•˜์ง€ ์•Š์•„์•ผ ํ•˜๋ฉฐ ํ•ด๋‹น๋˜์ง€ ์•Š๋Š” ๊ทผ๋กœ์ž๋Š” ๋ณด๋„ˆ์Šค๊ฐ€ ์—†๋‹ค(=0).

์กฐ๊ฑด๋ณ„ ๊ฐ’์„ ์ถœ๋ ฅํ•˜๊ธฐ ์œ„ํ•ด CASE๋ฌธ์„ ์‚ฌ์šฉํ–ˆ๋‹ค.

<์กฐ๊ฑด1> employee_id๊ฐ€ ํ™€์ˆ˜์ธ ์กฐ๊ฑด์€ ํ•จ์ˆ˜ MOD* ์‚ฌ์šฉํ•ด์„œ ๋‚˜๋จธ์ง€๊ฐ’ 1 ์ธ ๋ฐ์ดํ„ฐ๋ฅผ ํ™•์ธ    

<์กฐ๊ฑด2> name์€ ๋ฌธ์ž์—ด ํ•จ์ˆ˜ LIKE๋ฅผ ์‚ฌ์šฉํ•ด์„œ 'M'์œผ๋กœ ์‹œ์ž‘ํ•˜์ง€  ์•Š๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ํ™•์ธ

 

์กฐ๊ฑด 1, 2๋ฅผ ๋ชจ๋‘ ์ถฉ์กฑํ•ด์•ผ ํ•˜๋ฏ€๋กœ ( ์กฐ๊ฑด1 )&( ์กฐ๊ฑด2 ) ๋กœ AND ํ˜•ํƒœ๋ฅผ ๋งŒ๋“ค์–ด์ฃผ์—ˆ๋‹ค. 

SELECT employee_id,
       CASE WHEN (MOD(employee_id, 2) = 1)&(name NOT LIKE 'M%') THEN salary * 1.0 
       ELSE 0 END bonus 
 FROM Employees
 ORDER BY 1;

 

* MOD(n, m) 

n์„ m์œผ๋กœ ๋‚˜๋ˆˆ ๋‚˜๋จธ์ง€๊ฐ’์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.