262. Trips and Users (Hard)

Source: https://leetcode.com/problems/trips-and-users/#/description
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 clients between Oct 1, 2013 and Oct 3, 2013. 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        |
+------------+-------------------+

Solution:

select a.Request_at as Day, round((
    select count(*) from Trips as b left join Users as c on b.Client_Id = c.Users_Id
    where (b.Status = ‘cancelled_by_client‘ or b.Status =‘cancelled_by_driver‘)
        and c.Banned = ‘No‘
        and b.Request_at = a.Request_at)/count(a.Status),2) as "Cancellation Rate"
from Trips as a left join Users as d
on a.Client_Id = d.Users_Id
where d.Banned = ‘No‘ and a.Request_at >= date("2013-10-01") and a.Request_at <= date("2013-10-03")
group by 1
order by 1;
时间: 2024-08-10 03:08:26

262. Trips and Users (Hard)的相关文章

[LeetCode] 262. Trips and Users 旅行和用户

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’). +----+----

LeetCode Problems List 题目汇总

No. Title Level Rate 1 Two Sum Medium 17.70% 2 Add Two Numbers Medium 21.10% 3 Longest Substring Without Repeating Characters Medium 20.60% 4 Median of Two Sorted Arrays Hard 17.40% 5 Longest Palindromic Substring Medium 20.70% 6 ZigZag Conversion Ea

Leetcode problems classified by company 题目按公司分类(Last updated: October 2, 2017)

Sorted by frequency of problems that appear in real interviews.Last updated: October 2, 2017Google (214)534 Design TinyURL388 Longest Absolute File Path683 K Empty Slots340 Longest Substring with At Most K Distinct Characters681 Next Closest Time482

【算法揭秘】Google Trips中存在了280年的古老算法揭秘

算法工程比较有意思的地方在于它永远不过时,不知道什么时候比较古老但是比较有用的算法可能会在我们的设计中体现,昨天,google发布了它的google trips, 一个新的app帮助你创建你在城市中的非常不错的行程.而这个算法确实在280年之前就已经被论证过的. 1736年,欧拉发表了著名的有关柯尼斯堡的七座桥的著名论文,七座桥问题,如下: 在这篇论文中,欧拉研究了以下问题:能否旅行者所有桥只走一次就能够逛遍整所城市(大陆被七座桥隔开)?最后论文给出了结果,对于柯尼斯堡这个城市来说来说,不能.为

Codeforces Round #262 (Div. 2) 总结:二分

B. Little Dima and Equation 思路:本来前两题都很快做出来的,然后觉得ranting应该可以增加了.然后觉得代码没问题了,又想到了一组cha人的数据,然后就锁了,然后刚锁就被别人cha了看了代码才发现尼玛忘了判断小于10^9了,然后C反正想了好多种方法都不会就没心情了,就这样rating又降了 #pragma comment(linker, "/STACK:1024000000,1024000000") #include<iostream> #in

Codeforces Round #262 (Div. 2)

Codeforces Round #262 (Div. 2) A:水题,直接不断模拟即可 B:由于s(x)大小最大到1e9,所以数位和最多为81,这样只要枚举s(x),就只要枚举1到81即可,然后在计算出x,判断是否符合,符合就加进答案 C:二分高度,然后判断的时候for一遍,每次不符合的位置就去浇水,从左往右推一遍即可 D:构造,如果k >= 5, 那么就可以直接放偶数,奇数,偶数,奇数,由于偶数和偶数+1异或必然为1,所以这样放4个异或和就到最小的0了,然后k = 1,2,4都可以特判到,关

[家里蹲大学数学杂志]第262期广州大学2013年数学分析考研试题参考解答

一.($3\times 15'=45'$) 1.  求 $\dps{\ls{n}(a^n+b^n)^\frac{1}{n}}$, 其中 $a>b>0$. 解答: 由 $$\bex a<(a^n+b^n)^\frac{1}{n}<2^\frac{1}{n}a \eex$$ 及 $\dps{\ls{n}2^\frac{1}{n}=1}$ (参考第二大题第 4 小题), 夹逼原理知原极限 $=a$. 2.  求 $\dps{\lim_{x\to 0}\frac{\arctan x-x}{

Codeforces Round #262 (Div. 2)460A. Vasya and Socks(简单数学题)

题目链接:http://codeforces.com/contest/460/problem/A A. Vasya and Socks time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Vasya has n pairs of socks. In the morning of each day Vasya has to put o

Codeforces Round #262 (Div. 2) 460B. Little Dima and Equation(枚举)

题目链接:http://codeforces.com/problemset/problem/460/B B. Little Dima and Equation time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Little Dima misbehaved during a math lesson a lot and the nas