1. 程式人生 > >leetcode MYSQL資料庫題目

leetcode MYSQL資料庫題目

Write a SQL query to deleteall duplicate email entries in a table named Person, keeping only unique emails based on its smallestId.

+----+------------------+
| Id | Email            |
+----+------------------+
| 1  | [email protected] |
| 2  | [email protected]  |
| 3  | [email protected]
| +----+------------------+ Id is the primary key column for this table.

For example, after running your query, the above Person table should have the following rows:

+----+------------------+
| Id | Email            |
+----+------------------+
| 1  | [email protected] |
| 2  | [email protected]
| +----+------------------+
這個題是delete!!!
#way-1
#delete a from Person a,Person b where a.Email = b.Email and a.Id > b.Id;

#way-2
delete a from Person a inner join Person b on a.Email = b.Email and a.Id > b.Id;

197. Rising Temperature

Given a Weather table, write a SQL query to find all dates' Ids with higher temperature compared to its previous (yesterday's) dates.

+---------+------------+------------------+
| Id(INT) | Date(DATE) | Temperature(INT) |
+---------+------------+------------------+
|       1 | 2015-01-01 |               10 |
|       2 | 2015-01-02 |               25 |
|       3 | 2015-01-03 |               20 |
|       4 | 2015-01-04 |               30 |
+---------+------------+------------------+
For example, return the following Ids for the above Weather table:
+----+
| Id |
+----+
|  2 |
|  4 |
+----+
重點是TO_DAYS()
#way-1
#select a.ID from Weather a inner join Weather b on TO_DAYS(a.Date) = TO_DAYS(b.Date) + 1 and a.Temperature > b.Temperature;

#way-2
select a.ID from Weather a, Weather b where TO_DAYS(a.Date) = TO_DAYS(b.Date) + 1 and a.Temperature > b.Temperature;


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

+----+-----------+-----------+---------+--------------------+----------+
| 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        |
+------------+-------------------+

思路:首先在trip中找出日期區間內符合條件的記錄然後對於這些記錄根據日期(request_at列)進行分組,找出每組符合status!='completed'條件的元組的個數作為分子,分組內記錄個數總數作為分母,分子除以分母得出結果。

注意:

1、round(x,2)取小數點兩位

2、order by ... asc 升序

3、sum()  求和

4、

case sex

when  '1' then '男'

when '2' then '女'

else '其他'

end

select 
    request_at as Day, 
    round(sum(case Status when "completed" then 0 else 1 end)/count(*), 2) as 'Cancellation Rate'
from  
    (select * 
     from Trips  
     where 
        client_id not in 
            (select users_id 
            from Users 
            where banned = "yes" and role = "client") 
        and request_at >= "2013-10-01" and request_at <= "2013-10-03"
    ) as t  
group by request_at order by request_at asc;