[LeetCode Hard] 262. Trips and Users

2024. 2. 28. 12:46·SQL 문제 풀이

 

테이블 정보

 

 

문제

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 solution 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.

The result format is in the following example.

 

결과 예시

 

 

정답 코드 (MySQL)

select
    request_at as 'Day',
    Round(sum(status <> 'completed') / count(status), 2) as 'Cancellation Rate'
from Trips
where request_at between '2013-10-01' and '2013-10-03' and
      client_id NOT IN (select users_id 
                        from Users 
                        where role = 'client' and banned = 'Yes') and
      driver_id NOT IN (select users_id 
                        from Users 
                        where role = 'driver' and banned = 'Yes')
group by request_at;

 

 

문제 풀이

취소율은 금지되지 않은 사용자가 있는 요청의 수를

해당 날짜의 금지되지 않은 사용자가 있는 요청의 총 수로 나누어 계산한다.

여기서 금지되지 않은 사용자는 Users 테이블에서 banned 컬럼 값이 'No'에 해당하는 클라이언트, 드라이버들을 말한다.

 

그러므로 banned된 사용자는 집계에서 제외하기 위해 다중행 서브 쿼리를 사용하고,

"2013-10-01"에서 "2013-10-03" 사이에서

각각 계산한 취소율을 Round 함수를 사용하여 소수점 두 자리까지 나타내어 계산해주면 된다.

 

 

 

 

Trips and Users - LeetCode

Can you solve this real interview question? Trips and Users - 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

 

'SQL 문제 풀이' 카테고리의 다른 글

[프로그래머스 Level 4] 자동차 대여 기록 별 대여 금액 구하기  (0) 2024.02.14
[프로그래머스 Level 4] 우유와 요거트가 담긴 장바구니  (0) 2024.01.25
[LeetCode Hard] 601. Human Traffic of Stadium  (0) 2024.01.16
[LeetCode Medium] 1341. Movie Rating  (0) 2024.01.12
[LeetCode Hard] 185. Department Top Three Salaries  (2) 2024.01.07
'SQL 문제 풀이' 카테고리의 다른 글
  • [프로그래머스 Level 4] 자동차 대여 기록 별 대여 금액 구하기
  • [프로그래머스 Level 4] 우유와 요거트가 담긴 장바구니
  • [LeetCode Hard] 601. Human Traffic of Stadium
  • [LeetCode Medium] 1341. Movie Rating
개발이조아용
개발이조아용
IT 개발에서 배운 성장의 기록을 작성합니다.
  • 개발이조아용
    계속 하다 보면?!
    개발이조아용
  • 전체
    오늘
    어제
    • 분류 전체보기 (68)
      • Tibero DB (Tmax AI Bigdata .. (7)
      • Git (2)
      • CI CD (2)
      • Redis (3)
      • SpringBoot (16)
      • SQL 문제 풀이 (8)
      • Apache Kafka (8)
        • 오류 해결 (3)
        • 개념 정리 (4)
        • 보안 (1)
      • Nginx (3)
      • SW마에스트로 (3)
      • Kubernetes (4)
      • AWS (5)
      • gRPC (3)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    DynamoDB 연동
    Redis 개념
    Kafka 오류
    sql 문제
    Kafka SASL
    Git
    SASL 인증
    SpringBoot
    redis script
    KAFKA
    MSA
    Kafka 개념
    Tibero
    nginx
    leetcode
    grpc
    소프트웨어 마에스트로
    K8S
    SQL
    redis
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.3
개발이조아용
[LeetCode Hard] 262. Trips and Users
상단으로

티스토리툴바