LeetCode

LeetCode

  • Problems
  • GitHub

›Problems

Problems

  • Two Sum
  • Add Two Numbers
  • Longest Substring Without Repeating Characters
  • Median of Two Sorted Arrays
  • Longest Palindromic Substring
  • ZigZag Conversion
  • Reverse Integer
  • String to Integer (atoi)
  • Palindrome Number
  • Regular Expression Matching
  • Container With Most Water
  • Integer to Roman
  • Roman to Integer
  • Longest Common Prefix
  • 3Sum
  • 3Sum Closest
  • Letter Combinations of a Phone Number
  • 4Sum
  • Remove Nth Node From End of List
  • Valid Parentheses
  • Merge Two Sorted Lists
  • Generate Parentheses
  • Merge k Sorted Lists
  • Swap Nodes in Pairs
  • Reverse Nodes in k-Group
  • Remove Duplicates from Sorted Array
  • Remove Element
  • Implement strStr()
  • Divide Two Integers
  • Substring with Concatenation of All Words
  • Next Permutation
  • Longest Valid Parentheses
  • Search in Rotated Sorted Array
  • Find First and Last Position of Element in Sorted Array
  • Search Insert Position
  • Valid Sudoku
  • Sudoku Solver
  • Count and Say
  • Combination Sum
  • Combination Sum II
  • First Missing Positive
  • Trapping Rain Water
  • Multiply Strings
  • Wildcard Matching
  • Jump Game II
  • Permutations
  • Permutations II
  • Rotate Image
  • Group Anagrams
  • Pow(x, n)
  • Binary Tree Inorder Traversal
  • Triangle
  • Number of Islands
  • Random Pick Index
  • Coin Change 2
  • Maximum Length of Pair Chain
  • Repeated String Match
  • Minimum ASCII Delete Sum for Two Strings
  • Remove Comments
  • Split Linked List in Parts
  • Design HashSet
  • RLE Iterator
  • Number of Recent Calls
  • Rotting Oranges
  • Minimum Number of K Consecutive Bit Flips
  • Remove All Adjacent Duplicates In String
  • Unique Number of Occurrences
  • Count Servers that Communicate
  • Subtract the Product and Sum of Digits of an Integer
  • Find the Smallest Divisor Given a Threshold
  • Find N Unique Integers Sum up to Zero
  • Minimum Flips to Make a OR b Equal to c
  • Combine Two Tables
  • Second Highest Salary
  • Nth Highest Salary
  • Rank Scores
  • Consecutive Numbers
  • Employees Earning More Than Their Managers
  • Duplicate Emails
  • Customers Who Never Order
  • Department Highest Salary
  • Department Top Three Salaries
  • Delete Duplicate Emails
  • Rising Temperature
  • Trips and Users
  • Big Countries
  • Classes More Than 5 Students
  • Valid Phone Numbers
  • Tenth Line
  • Print in Order
  • Maximum Subarray

Trips and Users

Description

The Trips table holds all taxi trips. Each trip has a unique Id, while Client_Id and Driver_Id are both foreign keys to the Users_Id at the Users table. Status is an ENUM type of (‘completed’, ‘cancelled_by_driver’, ‘cancelled_by_client’).

+----+-----------+-----------+---------+--------------------+----------+
| Id | Client_Id | Driver_Id | City_Id |        Status      |Request_at|
+----+-----------+-----------+---------+--------------------+----------+
| 1  |     1     |    10     |    1    |     completed      |2013-10-01|
| 2  |     2     |    11     |    1    | cancelled_by_driver|2013-10-01|
| 3  |     3     |    12     |    6    |     completed      |2013-10-01|
| 4  |     4     |    13     |    6    | cancelled_by_client|2013-10-01|
| 5  |     1     |    10     |    1    |     completed      |2013-10-02|
| 6  |     2     |    11     |    6    |     completed      |2013-10-02|
| 7  |     3     |    12     |    6    |     completed      |2013-10-02|
| 8  |     2     |    12     |    12   |     completed      |2013-10-03|
| 9  |     3     |    10     |    12   |     completed      |2013-10-03| 
| 10 |     4     |    13     |    12   | cancelled_by_driver|2013-10-03|
+----+-----------+-----------+---------+--------------------+----------+

The Users table holds all users. Each user has an unique Users_Id, and Role is an ENUM type of (‘client’, ‘driver’, ‘partner’).

+----------+--------+--------+
| Users_Id | Banned |  Role  |
+----------+--------+--------+
|    1     |   No   | client |
|    2     |   Yes  | client |
|    3     |   No   | client |
|    4     |   No   | client |
|    10    |   No   | driver |
|    11    |   No   | driver |
|    12    |   No   | driver |
|    13    |   No   | driver |
+----------+--------+--------+

Write a SQL query to find the cancellation rate of requests made by unbanned users (both client and driver must be unbanned) between Oct 1, 2013 and Oct 3, 2013. The cancellation rate is computed by dividing the number of canceled (by client or driver) requests made by unbanned users by the total number of requests made by unbanned users.

For the above tables, your SQL query should return the following rows with the cancellation rate being rounded to two decimal places.

+------------+-------------------+
|     Day    | Cancellation Rate |
+------------+-------------------+
| 2013-10-01 |       0.33        |
| 2013-10-02 |       0.00        |
| 2013-10-03 |       0.50        |
+------------+-------------------+

Credits:
Special thanks to @cak1erlizhou for contributing this question, writing the problem description and adding part of the test cases.

Solution(mysql)

# Write your MySQL query statement below
select 
    Request_at as Day,
    round((count(IF(Trips.status != 'completed',TRUE,null))/count(*)),2) as 'Cancellation Rate'
from Trips
where Trips.Client_Id in (Select Users_Id from Users where Banned='No') 
and Trips.Driver_Id in (Select Users_Id from Users where Banned='No')
and Trips.Request_at between '2013-10-01' and '2013-10-03'
group by Request_at;
← Rising TemperatureBig Countries →
  • Description
  • Solution(mysql)
Powered By LeetCode Site Generator