【leetcode Database】262. Trips and Users

布满荆棘的人生 2022-07-19 02:23 233阅读 0赞

题目:

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 theUsers table. Status is an ENUM type of (‘completed’, ‘cancelled_by_driver’, ‘cancelled_by_client’).

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

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

  1. +----------+--------+--------+
  2. | Users_Id | Banned | Role |
  3. +----------+--------+--------+
  4. | 1 | No | client |
  5. | 2 | Yes | client |
  6. | 3 | No | client |
  7. | 4 | No | client |
  8. | 10 | No | driver |
  9. | 11 | No | driver |
  10. | 12 | No | driver |
  11. | 13 | No | driver |
  12. +----------+--------+--------+

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.

  1. +------------+-------------------+
  2. | Day | Cancellation Rate |
  3. +------------+-------------------+
  4. | 2013-10-01 | 0.33 |
  5. | 2013-10-02 | 0.00 |
  6. | 2013-10-03 | 0.50 |
  7. +------------+-------------------+

解析:

首先找出日期内符合规则的所有订单,然后再求出取消率即可。代码如下:

  1. SELECT Request_at AS 'Day',
  2. ROUND(SUM(CASE WHEN Status="completed" THEN 0 ELSE 1 END)/COUNT(*),2) AS 'Cancellation Rate' FROM
  3. (SELECT * FROM Trips WHERE
  4. Client_Id NOT IN (SELECT Users_Id FROM Users WHERE Banned = "Yes" AND Role = "client")
  5. AND Request_at >= "2013-10-01" AND Request_at <= "2013-10-03") t
  6. GROUP BY Request_at ORDER BY Request_at ASC;

发表评论

表情:
评论列表 (有 0 条评论,233人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Trip

    [Trip][] 给出一个长度为n序列\\(\\\{a\_i\\\}\\),长度为m的序列\\(\\\{b\_i\\\}\\),求它们的不同lcs序列按字典序输出,\\(n,