Notice
														
												
											
												
												
													Recent Posts
													
											
												
												
													Recent Comments
													
											
												
												
													Link
													
											
									| 일 | 월 | 화 | 수 | 목 | 금 | 토 | 
|---|---|---|---|---|---|---|
| 1 | 2 | 3 | 4 | |||
| 5 | 6 | 7 | 8 | 9 | 10 | 11 | 
| 12 | 13 | 14 | 15 | 16 | 17 | 18 | 
| 19 | 20 | 21 | 22 | 23 | 24 | 25 | 
| 26 | 27 | 28 | 29 | 30 | 31 | 
													Tags
													
											
												
												- TensorFlowGPU
- ProfileReport
- 데이터분석
- eda
- 프로그래머스
- tensorflow
- 신경쓰기의 기술
- Product Demand
- 피그마인디언
- HackerRank
- kaggle
- Gaimification
- MS SQL Server
- ModelCheckPoint
- 웨어하우스 보관 최적화
- MySQL
- ABC Analysis
- leetcode
- SKU Consolidation
- Labor Management System
- pandas profiling
- 파이썬
- forecast
- 딥러닝
- 코딩테스트
- 당신의 인생이 왜 힘들지 않아야 한다고 생각하십니까
- SQL
- oracle
- Inventory Optimization
- 코딩테스트연습
													Archives
													
											
												
												- Today
- Total
오늘도 배운다
262. Trips and Users / LeetCode, SQL, MySQL 본문
문제
source: LeetCode
The cancellation rate is computed by dividing the number of canceled (by client or driver) requests with unbanned users by the total number of requests with unbanned users on that day.
Write a SQL query to find the cancellation rate of requests with unbanned users (both client and driver must not be banned) each day between "2013-10-01" and "2013-10-03". Round Cancellation Rate to two decimal points.
Return the result table in any order.

제출답안(MySQL)
WITH users_ubnd AS (
    SELECT users_id
    FROM users
    WHERE banned = 'No' 
),
oct_1_thru_3 AS (
    SELECT 
        request_at, 
        CASE WHEN status REGEXP'^cancelled' THEN 1
        ELSE 0 END AS cancellation
    FROM 
        trips
    WHERE 
        client_id IN (SELECT users_id FROM users_ubnd)
        AND
        driver_id IN (SELECT users_id FROM users_ubnd)
        AND
        request_at BETWEEN '2013-10-01' AND '2013-10-03'
)
SELECT 
    request_at AS Day,
    ROUND(
        AVG(cancellation), 2
        ) AS 'Cancellation Rate'
FROM oct_1_thru_3
GROUP BY request_at
풀이(MySQL)
더보기
전략
>> CTE에서 다음 데이터를 모은다
a. ban 되지 않은 유저 id 목록
b. ban 되지 않은 client & driver의 2013-10-01 ~ 2013-10-3의 trip 데이터
- b 항목은 확률을 계산하기 쉽게 id 와 cancel 여부를 이진법화 (cancel의 경우 1, 아닐 경우 0)
WITH users_ubnd AS (
    SELECT users_id
    FROM users
    WHERE banned = 'No' 
),
oct_1_thru_3 AS (
    SELECT 
        request_at, 
        CASE WHEN status REGEXP'^cancelled' THEN 1
        ELSE 0 END AS cancellation
    FROM 
        trips
    WHERE 
        client_id IN (SELECT users_id FROM users_ubnd)
        AND
        driver_id IN (SELECT users_id FROM users_ubnd)
        AND
        request_at BETWEEN '2013-10-01' AND '2013-10-03'
)
oct_1_thru_3에서 날짜별로 그룹화 후 cancel 여부 평균계산
SELECT 
    request_at AS Day,
    ROUND(
        AVG(cancellation), 2
        ) AS 'Cancellation Rate'
FROM oct_1_thru_3
GROUP BY request_at
문제 바로가기(MySQL)
728x90
    
    
  '코딩테스트연습(SQL)' 카테고리의 다른 글
| 185. Department Top Three Salaries / LeetCode, SQL, MS SQL Server (0) | 2023.03.19 | 
|---|---|
| 601. Human Traffic of Stadium / LeetCode, SQL, MS SQL (0) | 2023.03.18 | 
| 607. Sales Person / LeetCode, SQL, MySQL (0) | 2023.03.13 | 
| 1527. Patients With a Condition / LeetCode, SQL, MySQL (0) | 2023.03.11 | 
| 1393. Capital Gain/Loss / Leet Code, SQL, MySQL (0) | 2023.03.10 | 
			  Comments
			
		
	
               
           
					
					
					
					
					
					
				 
								 
								 
								