๐ข ๋ณธ ํฌ์คํ ์ ํ์ฉ๋๋ ๊ธฐ๋ณธ ๋ฌธ์ ๋ฐ ์๋ฃ ์ถ์ฒ๋
๋ฆฌํธ์ฝ๋ 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์ผ๋ก ๋๋ ๋๋จธ์ง๊ฐ์ ๋ฐํํ๋ค.
'Growth ๐ณ > Practice ๐ป' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[ํ๋ก๊ทธ๋๋จธ์ค] ์ค๋ ๊ธฐ๊ฐ ๋ณดํธํ ๋๋ฌผ(2) : 2์ฐจ ํ์ด (0) | 2023.10.25 |
---|---|
[ํ๋ก๊ทธ๋๋จธ์ค] ์์ n๊ฐ ๋ ์ฝ๋ : 2์ฐจ ํ์ด ์ถ๊ฐ (0) | 2023.10.25 |
[Leet Code] 1633. Percentage of Users Attended a Contest (0) | 2023.10.23 |
[Leet Code] 1148. Article Views I (0) | 2023.10.21 |
[Leet Code] 1407. Top Travellers (1) | 2023.10.20 |