Growth ๐ŸŒณ/Practice ๐Ÿ’ป

[Leet code] 1934. Confirmation Rate

์ธ” 2023. 10. 5. 15:04

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

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


โœ”๋ฌธ์ œ

https://leetcode.com/problems/confirmation-rate/description/

 

LeetCode - The World's Leading Online Programming Learning Platform

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

 

Table : Signups

Table : Confirmations

The confirmation rate of a user is the number of 'confirmed' messages divided by the total number of requested confirmation messages. The confirmation rate of a user that did not request any confirmation messages is 0.

Round the confirmation rate to two decimal places.

Write a solution to find the confirmation rate of each user.

Return the result table in any order.

The result format is in the following example.


โœ” ํ’€์ด

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

 

Signups ์€ ์‚ฌ์šฉ์ž์˜ ๊ฐ€์ž…(๋“ฑ๋ก)์ผ ์ •๋ณด๋ฅผ ๋‹ด๊ณ  ์žˆ๋‹ค.

Confirmations์€ ๊ฐ€์ž…ํ•œ ์‚ฌ์šฉ์ž์ค‘ ๋ฉ”์‹œ์ง€๋ฅผ ์š”์ฒญํ•œ ์‚ฌ์šฉ์ž๋“ค์˜ ์š”์ฒญ์ผ์ž์™€ ์š”์ฒญ ์ƒํƒœ(action)์„ ๋‚˜ํƒ€๋‚ธ๋‹ค.

๋ฌธ์ œ์—์„œ ์š”๊ตฌํ•˜๋Š” confirmation_rate๋Š” user_id๋ณ„๋กœ 'ํ™•์ธ๋œ' ๋ฉ”์‹œ์ง€ ์ˆ˜๋ฅผ ์š”์ฒญํ•œ ์ „์ฒด ํ™•์ธ ๋ฉ”์‹œ์ง€ ์ˆ˜๋กœ ๋‚˜๋ˆ„์–ด์„œ ๊ตฌํ•  ์ˆ˜ ์žˆ๋‹ค.

ํ™•์ธ ๋ฉ”์‹œ์ง€๋ฅผ ์š”์ฒญํ•˜์ง€ ์•Š์€ ์‚ฌ์šฉ์ž๋Š” confirmation_rate๋ฅผ 0 ์œผ๋กœ ์ถœ๋ ฅํ•˜๊ณ  ํ™•์ธ ๋น„์œจ์€ ์†Œ์ˆ˜์  ์ดํ•˜ ๋‘ ์ž๋ฆฌ์—์„œ ๋ฐ˜์˜ฌ๋ฆผํ•ด์•ผ ํ•œ๋‹ค.

๊ฐ ์‚ฌ์šฉ์ž๋ณ„ confirmation_rate๋ฅผ ๊ตฌํ•˜๊ธฐ ์œ„ํ•ด  

โ‘  ๊ฐ€์ž… ์ดํ›„ ์š”์ฒญ์ด ์—†์—ˆ๋˜ ์œ ์ €๊นŒ์ง€ ํฌํ•จํ•  ์ˆ˜ ์žˆ๋„๋ก signups ํ…Œ์ด๋ธ”์„ ๊ธฐ์ค€์œผ๋กœ left join ํ•ด์ฃผ๊ณ 

โ‘ก CASE๋ฌธ๊ณผ ์ง‘๊ณ„ํ•จ์ˆ˜SUM์„ ์‚ฌ์šฉํ•ด์„œ ์‚ฌ์šฉ์ž๋‹น ( action = 'confirmed' ์ธ ๋ฉ”์‹œ์ง€ ์ˆ˜ ) / ์ „์ฒด ๋ฉ”์‹œ์ง€์ˆ˜ ๋ฅผ ๊ตฌํ•˜๋˜

      ๊ฐ€์ž… ์ดํ›„ ์š”์ฒญ์ด ์—†์—ˆ๋˜ ์œ ์ €์˜ ๊ฒฝ์šฐ action์ด null๋กœ ์กฐ์ธ๋œ ์ƒํƒœ์ด๋ฏ€๋กœ ๋ฌธ์ œ ์š”์ฒญ์— ๋”ฐ๋ผ IFNULL๋กœ 0 ์ฒ˜๋ฆฌ.

      ์ด ์™ธ ์ถœ๋ ฅ๊ฐ’์€ ์ถœ๋ ฅ ์กฐ๊ฑด์ธ ๋ฐ˜์˜ฌ๋ฆผ ์ฒ˜๋ฆฌ ํ•ด์ค€๋‹ค.

SELECT T1.user_id, 
        ROUND(IFNULL(
                    SUM(CASE WHEN T2.action = 'confirmed' THEN 1 ELSE 0 END) / COUNT(T2.action)
                    , 0)
                , 2) AS confirmation_rate
FROM Signups T1
LEFT JOIN Confirmations T2
ON T1.user_id = T2.user_id 
GROUP BY 1

 


โœ ๋ฌธ์ œํ’€์ด ํšŒ๊ณ 

์•„๋ž˜ ์ฟผ๋ฆฌ๊ฐ€ ๊ธฐ์กด์— ๋ฌธ์ œ ํ’€์ด๋Œ€๋กœ ์ฝ”๋“œ ์งœ๋ฉด์„œ ๊ณ ๋ฏผํ•œ ๊ณผ์ •์ธ๋ฐ

FROM์ ˆ์— ์„œ๋ธŒ์ฟผ๋ฆฌ๋กœ ์ž‘์„ฑํ•˜๊ณ  ๋ณด๋‹ˆ ๊ทธ๋ฃนํ•‘์„ ๋ฏธ๋ฆฌ ํ•ด์ฃผ๋ฉด ์„œ๋ธŒ์ฟผ๋ฆฌ๋ฅผ ํ•  ํ•„์š”๊ฐ€ ์—†์—ˆ๋‹ค.

์กฐ์ธํ•˜๊ณ  group by ๋ฅผ ๋งŽ์ด ์‚ฌ์šฉํ•ด๋ณด์ง€ ์•Š์•„์„œ ์ƒ๊ฐํ•˜์ง€ ๋ชปํ•œ ๋ถ€๋ถ„์ด์—ˆ๋Š”๋ฐ ์ด๋ฒˆ ๋ฌธ์ œ ํ’€์ด๋กœ ํ™œ์šฉํ•ด๋ณผ ์ˆ˜ ์žˆ์—ˆ๋‹ค.

SELECT user_id, ROUND(IFNULL(
                            SUM(CASE WHEN T3.action = 'confirmed' THEN 1 ELSE 0 END) / COUNT(T3.action)
                            , 0), 2) AS confirmation_rate
 FROM (
        SELECT T1.user_id, T2.action
        FROM Signups T1
        LEFT JOIN Confirmations T2
        ON T1.user_id = T2.user_id) T3 
 GROUP BY 1

๐Ÿ™‹‍โ™€๏ธ ๋ฌธ์ œ ํ’€์ด์™€ mysql ์ฟผ๋ฆฌ ํšจ์œจ์„ฑ ๋†’์ด๊ธฐ ๋“ฑ ํฌ์ŠคํŒ…์— ๋Œ€ํ•œ ์˜๊ฒฌ ๋ฐ ๊ธฐ์ˆ  ์กฐ์–ธ ๋Œ“๊ธ€  ๋ชจ๋‘ ํ™˜์˜ํ•ฉ๋‹ˆ๋‹ค!

'Growth ๐ŸŒณ > Practice ๐Ÿ’ป' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[Leet Code] 1068. Product Sales Analysis I  (1) 2023.10.07
[Leet code] 1075. Project Employees I  (1) 2023.10.06
[Leet code] 1393. Capital Gain/Loss  (1) 2023.10.04
[Leet code] 1204. Last Person to Fit in the Bus  (0) 2023.09.26
1907. Count Salary Categories  (0) 2023.09.24