MySQL 任務四 打卡 DW

2021-09-23 07:41:36 字數 4498 閱讀 8221

專案十:

行程和使用者(難度:困難) trips 表中存所有計程車的行程資訊。每段行程有唯一鍵 id,client_id 和 driver_id 是 users 表中 users_id 的外來鍵。status 是列舉型別,列舉成員為 (『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|

users 表存所有使用者。每個使用者有唯一鍵 users_id。banned 表示這個使用者是否被禁止,role 則是乙個表示(『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 |

寫一段 sql 語句查出 **2023年10月1日 **至 **2023年10月3日 **期間非禁止使用者的取消率。基於上表,你的 sql 語句應返回如下結果,取消率(cancellation rate)保留兩位小數。

| day | cancellation rate |

| 2013-10-01 | 0.33 |

| 2013-10-02 | 0.00 |

| 2013-10-03 | 0.50 |

create table users (

users_id int not null primary key,

banned varchar(20) default 'no',

role varchar(20) check (role in ('client','driver','partner'))

);create table trips (

id int not null primary key,

client_id int not null references users(users_id),

driver_id int not null references users(users_id),

city_id int,

status varchar(50) check (status in ('completed','cancelled_by_driver','cancelled_by_client')),

request_at date

);insert into users values

(1,'no','client'),

(2,'yes','client'),

(3,'no','client'),

(4,'no','client'),

(10,'no','driver'),

(11,'no','driver'),

(12,'no','driver'),

(13,'no','driver');

insert into trips values

(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');

select request_at as day, round(count(status != 'completed' or null)/count(*),2) as "cancellation rate"

from trips, users

where trips.client_id = users.users_id

and banned="no" and request_at between "2013-10-1" and "2013-10-3"

group by request_at;

專案十一:

各部門前3高工資的員工(難度:中等) 將專案7中的employee表清空,重新插入以下資料(其實是多插入5,6兩行):

| id | name | salary | departmentid |

| 1 | joe | 70000 | 1 |

| 2 | henry | 80000 | 2 |

| 3 | sam | 60000 | 2 |

| 4 | max | 90000 | 1 |

| 5 | janet | 69000 | 1 |

| 6 | randy | 85000 | 1 |

編寫乙個 sql 查詢,找出每個部門工資前三高的員工。例如,根據上述給定的**,查詢結果應返回:

| department | employee | salary |

| it | max | 90000 |

| it | randy | 85000 |

| it | joe | 70000 |

| sales | henry | 80000 |

| sales | sam | 60000 |

此外,請考慮實現各部門前n高工資的員工功能。

create table employee(

id int not null primary key,

name varchar(255) not null,

salary int not null,

departmentid int not null

);create table department(

id int not null primary key,

name varchar(255) not null

);insert into employee values

(1,"joe",70000,1),

(2,"henry",80000,2),

(3,"sam",60000,2),

(4,"max",90000,1),

(5,"janet",69000,1),

(6,"randy",85000,1);

insert into department values

('1','it'),

('2','sales');

專案十二

分數排名 - (難度:中等) 依然是昨天的分數表,實現排名功能,但是排名是非連續的,如下:

| score | rank |

| 4.00 | 1 |

| 4.00 | 1 |

| 3.85 | 3 |

| 3.65 | 4 |

| 3.65 | 4 |

| 3.50 | 6 |

select score,(select count(score)+1

from score

where score>s.score) as r

from score as s

order by score desc;

Python基礎第8期 任務1打卡

1 環境搭建 anaconda環境配置 直譯器 2 python初體驗 print and input 3 python基礎講解 python變數特性 命名規則 注釋方法 python中 作用 學會使用dir 及和help import使用 pep8介紹 4 python數值基本知識 python中...

Python基礎第8期 任務1打卡

1 環境搭建 anaconda環境配置 直譯器 2 python初體驗 print and input 3 python基礎講解 python變數特性 命名規則 注釋方法 python中 作用 學會使用dir 及和help import使用 pep8介紹 4 python數值基本知識 python中...

Python基礎第8期 任務1打卡

1 環境搭建 anaconda環境配置 直譯器 2 python初體驗 print and input 3 python基礎講解 python變數特性 命名規則 注釋方法 python中 作用 學會使用dir 及和help import使用 pep8介紹 4 python數值基本知識 python中...