-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: add sql * feat: add sql * feat: add sql * feat: add sql
- Loading branch information
1 parent
6265801
commit 283ac10
Showing
23 changed files
with
864 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
sql/src/main/java/com/leetcode/aggregation/middle/game_play.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
|
||
-- 550. 游戏玩法分析 IV | ||
Create table If Not Exists Activity_1 (player_id int, device_id int, event_date date, games_played int); | ||
Truncate table Activity_1; | ||
insert into Activity_1 (player_id, device_id, event_date, games_played) values ('1', '2', '2016-03-01', '5'); | ||
insert into Activity_1 (player_id, device_id, event_date, games_played) values ('1', '2', '2016-03-02', '6'); | ||
insert into Activity_1 (player_id, device_id, event_date, games_played) values ('2', '3', '2017-06-25', '1'); | ||
insert into Activity_1 (player_id, device_id, event_date, games_played) values ('3', '1', '2016-03-02', '0'); | ||
insert into Activity_1 (player_id, device_id, event_date, games_played) values ('3', '4', '2018-07-03', '5'); | ||
|
||
select | ||
ifnull(round(count(distinct result.player_id) / count(distinct Activity_1.player_id), 2), 0) as fraction | ||
from( | ||
select | ||
expected.player_id as player_id | ||
from ( | ||
select | ||
player_id, | ||
DATE_ADD(min(event_date), interval 1 day ) as second_sign | ||
from | ||
Activity_1 | ||
group by | ||
player_id | ||
)as expected, Activity_1 | ||
where | ||
expected.player_id = Activity_1.player_id and expected.second_sign = Activity_1.event_date | ||
) as result, Activity_1 |
28 changes: 28 additions & 0 deletions
28
sql/src/main/java/com/leetcode/high_search/easy/employee_department.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
Create table If Not Exists Employee_4 (employee_id int, department_id int, primary_flag ENUM('Y','N')); | ||
Truncate table Employee_4; | ||
insert into Employee_4 (employee_id, department_id, primary_flag) values ('1', '1', 'N'); | ||
insert into Employee_4 (employee_id, department_id, primary_flag) values ('2', '1', 'Y'); | ||
insert into Employee_4 (employee_id, department_id, primary_flag) values ('2', '2', 'N'); | ||
insert into Employee_4 (employee_id, department_id, primary_flag) values ('3', '3', 'N'); | ||
insert into Employee_4 (employee_id, department_id, primary_flag) values ('4', '2', 'N'); | ||
insert into Employee_4 (employee_id, department_id, primary_flag) values ('4', '3', 'Y'); | ||
insert into Employee_4 (employee_id, department_id, primary_flag) values ('4', '4', 'N'); | ||
|
||
with t as ( | ||
select | ||
employee_id, | ||
department_id, | ||
primary_flag, | ||
count(*) over (partition by employee_id) as count_over | ||
from | ||
Employee_4 | ||
) | ||
|
||
select | ||
employee_id, | ||
department_id | ||
from | ||
t | ||
where | ||
count_over=1 | ||
or primary_flag='Y' |
40 changes: 40 additions & 0 deletions
40
sql/src/main/java/com/leetcode/high_search/easy/product_price_date.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
|
||
Create table If Not Exists Products (product_id int, new_price int, change_date date); | ||
Truncate table Products; | ||
insert into Products (product_id, new_price, change_date) values ('1', '20', '2019-08-14'); | ||
insert into Products (product_id, new_price, change_date) values ('2', '50', '2019-08-14'); | ||
insert into Products (product_id, new_price, change_date) values ('1', '30', '2019-08-15'); | ||
insert into Products (product_id, new_price, change_date) values ('1', '5', '2019-08-16'); | ||
insert into Products (product_id, new_price, change_date) values ('2', '65', '2019-08-17'); | ||
insert into Products (product_id, new_price, change_date) values ('3', '20', '2019-08-18'); | ||
|
||
|
||
select | ||
p1.product_id, | ||
ifnull(p2.new_price, 10) as price | ||
from ( | ||
select | ||
distinct product_id | ||
from | ||
Products | ||
) as p1 -- 所有的产品 | ||
left join ( | ||
select | ||
product_id, | ||
new_price | ||
from | ||
Products | ||
where (product_id, change_date) | ||
in ( | ||
select | ||
product_id, | ||
max(change_date) | ||
from | ||
Products | ||
where | ||
change_date <= '2019-08-16' | ||
group by | ||
product_id | ||
) | ||
) as p2 -- 在 2019-08-16 之前有过修改的产品和最新的价格 | ||
on p1.product_id = p2.product_id |
56 changes: 56 additions & 0 deletions
56
sql/src/main/java/com/leetcode/high_search/easy/search_manager_employee.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
-- 1731. 每位经理的下属员工数量 | ||
|
||
|
||
use leetcode; | ||
Create table If Not Exists Employees_3(employee_id int, name varchar(20), reports_to int, age int); | ||
Truncate table Employees_3; | ||
insert into Employees_3 (employee_id, name, reports_to, age) values ('9', 'Hercy', NULL, '43'); | ||
insert into Employees_3 (employee_id, name, reports_to, age) values ('6', 'Alice', '9', '41'); | ||
insert into Employees_3 (employee_id, name, reports_to, age) values ('4', 'Bob', '9', '36'); | ||
insert into Employees_3 (employee_id, name, reports_to, age) values ('2', 'Winston', NULL, '37'); | ||
|
||
select | ||
* | ||
from | ||
Employees_3 as e | ||
left join | ||
Employees_3 as t | ||
on | ||
e.employee_id = t.reports_to | ||
where | ||
t.name is not null; | ||
|
||
select | ||
e.employee_id, | ||
e.name, | ||
t.age | ||
from | ||
Employees_3 as e | ||
left join | ||
Employees_3 as t | ||
on | ||
e.employee_id = t.reports_to | ||
where | ||
t.name is not null; | ||
|
||
select | ||
a.employee_id, | ||
a.name, | ||
count(a.name) as reports_count , | ||
round(avg(a.age)) as average_age | ||
from( | ||
select | ||
e.employee_id, | ||
e.name, | ||
t.age | ||
from | ||
Employees_3 as e | ||
left join | ||
Employees_3 as t | ||
on | ||
e.employee_id = t.reports_to | ||
where | ||
t.name is not null | ||
) as a | ||
group by | ||
a.employee_id |
22 changes: 22 additions & 0 deletions
22
sql/src/main/java/com/leetcode/highlevel/delete_person_repeat.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
-- 196. 删除重复的电子邮箱 | ||
Create table If Not Exists Person (id int, email varchar(255)); | ||
Truncate table Person; | ||
insert into Person (id, email) values ('1', '[email protected]'); | ||
insert into Person (id, email) values ('2', '[email protected]'); | ||
insert into Person (id, email) values ('3', '[email protected]'); | ||
|
||
|
||
delete from | ||
Person | ||
where id not in ( | ||
select | ||
min_id | ||
from ( | ||
select | ||
min(id) as min_id | ||
from | ||
Person | ||
group by | ||
)as t | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
-- 1667. 修复表中的名字 | ||
Create table If Not Exists Users_3 (user_id int, name varchar(40)); | ||
Truncate table Users_3; | ||
insert into Users_3 (user_id, name) values ('1', 'aLice'); | ||
insert into Users_3 (user_id, name) values ('2', 'bOB'); | ||
|
||
|
||
select user_id, | ||
concat(upper(left(name,1)), lower(right(name,length(name)-1))) as name | ||
from Users_3 | ||
order by | ||
user_id | ||
|
||
|
||
select | ||
|
||
from |
48 changes: 48 additions & 0 deletions
48
sql/src/main/java/com/leetcode/highlevel/list_all_order.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
|
||
use leetcode; | ||
Create table If Not Exists Products_1 (product_id int, product_name varchar(40), product_category varchar(40)); | ||
Create table If Not Exists Orders (product_id int, order_date date, unit int); | ||
Truncate table Products_1; | ||
insert into Products_1 (product_id, product_name, product_category) values ('1', 'Leetcode Solutions', 'Book'); | ||
insert into Products_1 (product_id, product_name, product_category) values ('2', 'Jewels of Stringology', 'Book'); | ||
insert into Products_1 (product_id, product_name, product_category) values ('3', 'HP', 'Laptop'); | ||
insert into Products_1 (product_id, product_name, product_category) values ('4', 'Lenovo', 'Laptop'); | ||
insert into Products_1 (product_id, product_name, product_category) values ('5', 'Leetcode Kit', 'T-shirt'); | ||
Truncate table Orders; | ||
insert into Orders (product_id, order_date, unit) values ('1', '2020-02-05', '60'); | ||
insert into Orders (product_id, order_date, unit) values ('1', '2020-02-10', '70'); | ||
insert into Orders (product_id, order_date, unit) values ('2', '2020-01-18', '30'); | ||
insert into Orders (product_id, order_date, unit) values ('2', '2020-02-11', '80'); | ||
insert into Orders (product_id, order_date, unit) values ('3', '2020-02-17', '2'); | ||
insert into Orders (product_id, order_date, unit) values ('3', '2020-02-24', '3'); | ||
insert into Orders (product_id, order_date, unit) values ('4', '2020-03-01', '20'); | ||
insert into Orders (product_id, order_date, unit) values ('4', '2020-03-04', '30'); | ||
insert into Orders (product_id, order_date, unit) values ('4', '2020-03-04', '60'); | ||
insert into Orders (product_id, order_date, unit) values ('5', '2020-02-25', '50'); | ||
insert into Orders (product_id, order_date, unit) values ('5', '2020-02-27', '50'); | ||
insert into Orders (product_id, order_date, unit) values ('5', '2020-03-01', '50'); | ||
|
||
|
||
# Write your MySQL query statement below | ||
|
||
select | ||
product_name, | ||
sum(unit) | ||
from ( | ||
select | ||
o.product_id, | ||
p.product_name, | ||
o.unit | ||
from | ||
Orders as o | ||
left join | ||
Products_1 as p | ||
on | ||
o.product_id = p.product_id | ||
where | ||
o.order_date between '2020-02-01' and '2020-02-29' | ||
) as t | ||
group by | ||
product_id | ||
having | ||
sum(unit) >= 100; |
26 changes: 26 additions & 0 deletions
26
sql/src/main/java/com/leetcode/highlevel/search_patient.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
|
||
Create table If Not Exists Patients (patient_id int, patient_name varchar(30), conditions varchar(100)); | ||
Truncate table Patients; | ||
insert into Patients (patient_id, patient_name, conditions) values ('1', 'Daniel', 'YFEV COUGH'); | ||
insert into Patients (patient_id, patient_name, conditions) values ('2', 'Alice', ''); | ||
insert into Patients (patient_id, patient_name, conditions) values ('3', 'Bob', 'DIAB100 MYOP'); | ||
insert into Patients (patient_id, patient_name, conditions) values ('4', 'George', 'ACNE DIAB100'); | ||
insert into Patients (patient_id, patient_name, conditions) values ('5', 'Alain', 'DIAB201'); | ||
|
||
|
||
select patient_id, | ||
patient_name, | ||
conditions | ||
from | ||
Patients | ||
where | ||
conditions like '% DIAB1%' or conditions like 'DIAB1%' | ||
|
||
|
||
|
||
select patient_id, | ||
patient_name, | ||
conditions | ||
from Patients | ||
where | ||
(conditions regexp '^DIAB1') or (conditions regexp '\\sDIAB1'); -- ok |
20 changes: 20 additions & 0 deletions
20
sql/src/main/java/com/leetcode/search/easy/employee_bonus.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
|
||
Create table If Not Exists Employee (empId int, name varchar(255), supervisor int, salary int); | ||
Create table If Not Exists Bonus (empId int, bonus int); | ||
Truncate table Employee; | ||
insert into Employee (empId, name, supervisor, salary) values ('3', 'Brad', NULL, '4000'); | ||
insert into Employee (empId, name, supervisor, salary) values ('1', 'John', '3', '1000'); | ||
insert into Employee (empId, name, supervisor, salary) values ('2', 'Dan', '3', '2000'); | ||
insert into Employee (empId, name, supervisor, salary) values ('4', 'Thomas', '3', '4000'); | ||
Truncate table Bonus; | ||
insert into Bonus (empId, bonus) values ('2', '500'); | ||
insert into Bonus (empId, bonus) values ('4', '2000'); | ||
|
||
select | ||
name, bonus | ||
from | ||
Employee as a | ||
left join | ||
Bonus as b | ||
on a.empId = b.empId | ||
where bonus < 1000 or bonus is null; |
26 changes: 26 additions & 0 deletions
26
sql/src/main/java/com/leetcode/search/easy/machine_avg_process_time.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
|
||
-- 1661. 每台机器的进程平均运行时间 | ||
use leetcode; | ||
Create table If Not Exists leetcode.Activity (machine_id int, process_id int, activity_type ENUM('start', 'end'), | ||
timestamp | ||
float); | ||
Truncate table leetcode.Activity; | ||
insert into Activity (machine_id, process_id, activity_type, timestamp) values ('0', '0', 'start', '0.712'); | ||
insert into Activity (machine_id, process_id, activity_type, timestamp) values ('0', '0', 'end', '1.52'); | ||
insert into Activity (machine_id, process_id, activity_type, timestamp) values ('0', '1', 'start', '3.14'); | ||
insert into Activity (machine_id, process_id, activity_type, timestamp) values ('0', '1', 'end', '4.12'); | ||
insert into Activity (machine_id, process_id, activity_type, timestamp) values ('1', '0', 'start', '0.55'); | ||
insert into Activity (machine_id, process_id, activity_type, timestamp) values ('1', '0', 'end', '1.55'); | ||
insert into Activity (machine_id, process_id, activity_type, timestamp) values ('1', '1', 'start', '0.43'); | ||
insert into Activity (machine_id, process_id, activity_type, timestamp) values ('1', '1', 'end', '1.42'); | ||
insert into Activity (machine_id, process_id, activity_type, timestamp) values ('2', '0', 'start', '4.1'); | ||
insert into Activity (machine_id, process_id, activity_type, timestamp) values ('2', '0', 'end', '4.512'); | ||
insert into Activity (machine_id, process_id, activity_type, timestamp) values ('2', '1', 'start', '2.5'); | ||
insert into Activity (machine_id, process_id, activity_type, timestamp) values ('2', '1', 'end', '5'); | ||
|
||
select | ||
machine_id, | ||
ROUND(SUM(CASE WHEN activity_type='end' THEN timestamp ELSE -timestamp END)/count(distinct process_id), 3) as processing_time | ||
from | ||
Activity | ||
group by machine_id; |
Oops, something went wrong.