Leetcode) No.262 [Trips and Users]
본문 바로가기
Data Base/Leetcode

Leetcode) No.262 [Trips and Users]

by 조훈이 2022. 4. 4.

Leetcode) No.262 [Trips and Users]


  문제 

(2) Trips and Users - LeetCode

 

Trips and Users - LeetCode

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


  설명 

 <사용된 함수> 
 * GROUP BY
 * HAVING
 * WHERE
 * ROUND
 * IF
 * COUNT

  Request_at 날짜를 기준으로 그룹화를 한다. 이 때, 그 그룹에는 Banned 되지 않은 Client와 Driver 만 들어갈 수 있으며, 2013년 10월 1일 부터 2013년 10월 3일 사이의 날짜에 해당되는 경우만 그룹화 한다.

  이렇게 구성된 그룹 내에서, Status가 'completed' 가 아닌 경우의 Data들의 수 / 그룹 내 Data 들의 수 로 값을 계산한다. MySQL 에선 정수/정수 를 진행하면 실수가 나오므로, ROUND 함수를 통해 그 실수를 소수 둘 째 자리까지 반올림 한다.


  Code 

더보기
SELECT T.request_at AS 'Day', 
       ROUND(COUNT(IF(T.status != 'completed', 1, NULL))/COUNT(*), 2) 
            AS 'Cancellation Rate'
FROM Trips AS T
WHERE T.client_id NOT IN (
                          SELECT users_id FROM Users
                          WHERE banned = 'Yes'
                         )
                          AND
      T.driver_id NOT IN (
                          SELECT users_id FROM Users
                          WHERE banned = 'Yes'
                         )   
GROUP BY request_at
HAVING T.request_at BETWEEN '2013-10-01' AND '2013-10-03'
728x90

'Data Base > Leetcode' 카테고리의 다른 글

Leetcode) No.626 [Exchange Seats]  (0) 2021.09.04
Leetcode) No.180 [Consecutive Numbers]  (0) 2021.09.03
Leet) No.175 [Combine Two Tables]  (0) 2021.08.31
Leet) No.1179 [Reformat Department Table]  (0) 2021.08.30

댓글