專案十:行程和使用者(難度:困難)
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
建表:
create table trips(
id int not null primary key,
client_id int,
driver_id int,
city_id int,
status enum("completed", "cancelled_by_driver", "cancelled_by_client"),
request_at date,
foreign key(client_id) references users(users_id),
foreign key(driver_id) references users(users_id)
);
插入資料:
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");
users 表存所有使用者。每個使用者有唯一鍵 users_id。banned 表示這個使用者是否被禁止,role 則是乙個表示(『client』, 『driver』, 『partner』)的列舉型別。
建表:
create table users(
users_id int primary key,
banned enum("yes", "no"),
role enum("client", "driver", "role")
);
插入資料:
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");
寫一段 sql 語句查出 **2023年10月1日 **至 **2023年10月3日 **期間非禁止使用者的取消率。取消率(cancellation rate)保留兩位小數。
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 |
建表:
create table employee2(
id int not null,
name char(10) not null,
salary long,
departmentid int
);
插入資料:
insert into employee2 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);
編寫乙個 sql 查詢,找出每個部門工資前三高的員工。
select d.name department, e.name employee2, e.salary salary
from employee2 e inner join department d
on e.departmentid = d.id
where (
select count(distinct c.salary)
from employee2 c
where e.salary < c.salary and e.departmentid = c.departmentid
) < 3
order by e.departmentid, e.salary desc;
輸出:
專案十二 分數排名 - (難度:中等)
依然是昨天的分數表,實現排名功能,但是排名是非連續的,如下: ±------±-----+ | score | rank | ±------±-----+ | 4.00 | 1 | | 4.00 | 1 | | 3.85 | 3 | | 3.65 | 4 | | 3.65 | 4 | | 3.50 | 6 | ±------±-----
select score as score,(
select count(score)+1
from scores s1
where s1.score>s2.score
) as "rank"
from scores s2
order by score desc;
輸出:
待續 四)spark學習系列
這章節主要延續的 三 中的shuffle模組提供的shuffle read。在spark 中,shuffle read是提供給shuffle的後繼用來讀取shuffle資料的。當需要讀取遠端資料的時候,1.每次只會開最多5個執行緒進行讀取 2.每次請求的數量不會超過maxmbinflight,預設是...
CUDA學習系列教程(四)
gpu程式一般步驟 過程中,一般要盡量降低資料通訊的消耗,所以如果程式需要複製大量的資料到gpu,顯然不是很合適使用gpu運算,最理想的情況是,每次複製的資料很小,然後運算量很大,輸出的結果還是很小,複製回cpu。核函式是gpu每個thread上執行的程式。必須通過 gobal 函式型別限定符定義。...
pandas學習總結系列 四
duplicated 處理重複資料 對於重複資料可以檢視是否重複,除了第一次出現的。第一次都視為沒有重複。df2.duplicated a keep false 0 true 1 true 2 true 3 true 4 true 5 false 6 false刪除重複資料 df2.drop dup...