力扣SQL仅数据库(570-579)
570. 至少有5名直接下属的经理
需求: 编写一个解决方案,找出至少有五个直接下属的经理
数据准备:
Create table If Not Exists Employee (id int, name varchar(255), department varchar(255), managerId int)
Truncate table Employee
insert into Employee (id, name, department) values ('101', 'John', 'A')
insert into Employee (id, name, department, managerId) values ('102', 'Dan', 'A', '101')
insert into Employee (id, name, department, managerId) values ('103', 'James', 'A', '101')
insert into Employee (id, name, department, managerId) values ('104', 'Amy', 'A', '101')
insert into Employee (id, name, department, managerId) values ('105', 'Anne', 'A', '101')
insert into Employee (id, name, department, managerId) values ('106', 'Ron', 'B', '101')
代码实现:
select e2.name from employee e1 join employee e2 on e1.managerId = e2.id
group by e1.managerId,e2.name having count(e2.id)>=5;
571. 给定数字的频率查询中位数
需求:编写解决方案,解压 Numbers
表,报告数据库中所有数字的 中位数 。结果四舍五入至 一位小数 。中位数是将数据样本中半数较高值和半数较低值分隔开的值。
数据准备:
Create table If Not Exists Numbers (num int, frequency int)
Truncate table Numbers
insert into Numbers (num, frequency) values ('0', '7')
insert into Numbers (num, frequency) values ('1', '1')
insert into Numbers (num, frequency) values ('2', '3')
insert into Numbers (num, frequency) values ('3', '1')
代码实现:
with recursive t1(num,frequency) as(
select num,frequency from numbers
union
select num,(frequency-1) as frequency from t1 where frequency>=2)
,t2 as (SELECT num,row_number() over (order by num) ord FROM t1)
,t3 as (select round(max(ord)/2 ) shu1,round(count(1)/2+1)shu2 from t2 having count(1)%2=0)
,t4 as (select round(count(1)/2+0.5)shu3 from t2 having count(1)%2=1)
,t5 as (select round(sum(num)/2,2) as median from t2 join t3 on ord=shu1 or ord=shu2
union all
select round(num,2) as median from t2 join t4 on ord=shu3)
select sum(median)as median from t5;
574. 当选者
需求:编写解决方案来报告获胜候选人的名字(即获得最多选票的候选人)。
生成的测试用例保证 只有一个候选人赢得 选举
数据准备:
Create table If Not Exists Candidate (id int, name varchar(255))
Create table If Not Exists Vote (id int, candidateId int)
Truncate table Candidate
insert into Candidate (id, name) values ('1', 'A')
insert into Candidate (id, name) values ('2', 'B')
insert into Candidate (id, name) values ('3', 'C')
insert into Candidate (id, name) values ('4', 'D')
insert into Candidate (id, name) values ('5', 'E')
Truncate table Vote
insert into Vote (id, candidateId) values ('1', '2')
insert into Vote (id, candidateId) values ('2', '4')
insert into Vote (id, candidateId) values ('3', '3')
insert into Vote (id, candidateId) values ('4', '2')
insert into Vote (id, candidateId) values ('5', '5')
代码实现:
with t1 as (select name,count(name)con from vote v join candidate c on v.candidateId=c.id group by name)
select name from t1 where con>=(select max(con) from t1);
577. 员工奖金
需求:编写解决方案,报告每个奖金 少于 1000
的员工的姓名和奖金数额。
以 任意顺序 返回结果表。
数据准备:
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', 'None', '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')
代码实现:
直接对奖金筛选时会将奖金为null的数据筛选掉,因此增加一列判断奖金的列来对奖金进行筛选,即将该增加列的null值转换为0,不会对原有的null值改变,也可过滤出小于1000(包括null)的列
with t as (select Employee.*,bonus,ifnull(bonus,0) b from employee left join bonus on Employee.empId=bonus.empId)
select name,max(bonus) bonus from t where b<1000 group by empid, name;
578. 查询回答率最高的问题
需求:编写一个解决方案以报告 回答率 最高的问题。如果有多个问题具有相同的最大 回答率 ,返回 question_id
最小的那个
回答率 是指:同一问题编号中回答次数占显示次数的比率。
数据准备:
Create table If Not Exists SurveyLog (id int, action varchar(255), question_id int, answer_id int, q_num int, timestamp int)
Truncate table SurveyLog
insert into SurveyLog (id, action, question_id, answer_id, q_num, timestamp) values ('5', 'show', '285', 'None', '1', '123')
insert into SurveyLog (id, action, question_id, answer_id, q_num, timestamp) values ('5', 'answer', '285', '124124', '1', '124')
insert into SurveyLog (id, action, question_id, answer_id, q_num, timestamp) values ('5', 'show', '369', 'None', '2', '125')
insert into SurveyLog (id, action, question_id, answer_id, q_num, timestamp) values ('5', 'skip', '369', 'None', '2', '126')
代码实现:
with t1 as (select question_id show_id,count(1) con1 from SurveyLog where action='show' group by question_id,action)
, t2 as (select question_id answer_id,count(1) con2 from SurveyLog where action='answer' group by question_id,action)
,t3 as (select *,ifnull(t2.con2,0)/t1.con1 cc from t1 left join t2 on t1.show_id=t2.answer_id)
select show_id survey_log from t3 order by cc desc,show_id limit 1;
借助answer_id和q_num列时可更快速解题:
with t1 as (select question_id,sum(if(answer_id is null,0,1))/count(q_num) as cc from surveylog
group by question_id order by cc desc , question_id )
select question_id survey_log from t1 limit 1;
579. 查询员工的累计薪水
需求:
编写一个解决方案,在一个统一的表中计算出每个员工的 累计工资汇总 。
员工的 累计工资汇总 可以计算如下:
- 对于该员工工作的每个月,将 该月 和 前两个月 的工资 加 起来。这是他们当月的 3 个月总工资和 。如果员工在前几个月没有为公司工作,那么他们在前几个月的有效工资为
0
。 - 不要 在摘要中包括员工 最近一个月 的 3 个月总工资和。
- 不要 包括雇员 没有工作 的任何一个月的 3 个月总工资和。
返回按 id
升序排序 的结果表。如果 id
相等,请按 month
降序排序
数据准备:
Create table If Not Exists Employee (id int, month int, salary int)
Truncate table Employee
insert into Employee (id, month, salary) values ('1', '1', '20')
insert into Employee (id, month, salary) values ('2', '1', '20')
insert into Employee (id, month, salary) values ('1', '2', '30')
insert into Employee (id, month, salary) values ('2', '2', '30')
insert into Employee (id, month, salary) values ('3', '2', '40')
insert into Employee (id, month, salary) values ('1', '3', '40')
insert into Employee (id, month, salary) values ('3', '3', '60')
insert into Employee (id, month, salary) values ('1', '4', '60')
insert into Employee (id, month, salary) values ('3', '4', '70')
insert into Employee (id, month, salary) values ('1', '7', '90')
insert into Employee (id, month, salary) values ('1', '8', '90')
代码实现:
先通过im !=0 将最大的月份过滤掉,再求出当月上一个月,上上个月的月份,将当月与出现的上一个月相减得出月份差cha1,当月与出现的上上个月相减得出月份差cha2;
当cha1=1且cha2=2说明该三个月的月份相连,求和窗口大小为当前月及前两月,即向上数2行;
当cha1=1且cha2>2 说明该月与当月出现的上一个月的月份相连,上月与出现的上上个月的月份不相连,求和窗格大小为当月与出现的上月,即向上数1行;
当cha1=2 时cha2只会等大,此时该月与出现的上个月份相差两个月,刚好在三个月内,求和窗格大小为当月与出现的上个月,即向上数1行;
其他情况即为仅统计当前月份。
with t2 as (select *,month-lm1 cha1,month-lm2 cha2
from
(
select Id,Month,Salary,
lead(Month,1,0) over(partition by Id order by Month) lm,
lag(Month,1,0) over(partition by Id order by Month) lm1,
lag(Month,2,0) over(partition by Id order by Month) lm2
from Employee
)t1 where lm !=0
order by Id,Month )
select Id,Month,
case when cha1=1 and cha2=2 then sum(salary) over(partition by id rows between 2 preceding and current row)
when cha1=1 and cha2>2 then sum(salary) over(partition by id rows between 1 preceding and current row)
when cha1=2 then sum(salary) over(partition by id rows between 1 preceding and current row)
else salary
end Salary
from t2 order by id,month desc;