力扣SQL仅数据库(570-579)

news/2025/4/26 12:15:05/

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.idgroup 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) lm2from 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 salaryend Salary
from t2 order by id,month desc;

http://www.ppmy.cn/news/1521033.html

相关文章

Mysql梳理1——数据库概述(上)

笔记来源&#xff1a;【MySQL数据库入门到大牛&#xff0c;mysql安装到优化&#xff0c;百科全书级&#xff0c;全网天花板】 https://www.bilibili.com/video/BV1iq4y1u7vj 目录 11.2 引入 11.2. 1 数据库与数据库管理系统 11.2.2 数据库与数据库管理系统的关系 11.2.…

nnunetv2(一)配置文件和nnUNetv2_convert_MSD_dataset命令

文章目录 setup.pypyproject.tomlconfiguration.pynnUNetv2_convert_MSD_dataset 如有错误&#xff0c;欢迎评论 setup.py 可以使用pip install .命令来安装nnunet v2 pyproject.toml 配置文件 [project] name "nnunetv2" # 项目名称 version "2.5" # …

jmeter中响应时间、TPS、服务器资源图表

插件下载可以参考前面文章&#xff1a;相关插件 一、响应时间图表 jmeter中的聚合报告已经足够显示响应时间&#xff0c;但是不会显示很详细&#xff0c;下面使用监听器中的插件查看&#xff0c; 添加后&#xff0c;可以不用更改任何配置&#xff0c;直接使用默认即可统计响应…

vue3 + ts + element ui plus 添加阿里图标库图标(只添加一个, 并引入)

先创建一个vue文件, 引入svg代码, 这个文件放components里也挺好 ** catalogIcon.vue代码如下: ** <template><svg t"1725419972935" class"icon" viewBox"0 0 1024 1024" version"1.1" xmlns"http://www.w3.org/20…

深度学习--机器学习相关(3)

1.K-近邻算法 KNN 听起来像是某种神经网络的名字&#xff0c;如RNN、CNN 等&#xff0c;其实不然&#xff0c;这是一种经典、简单的分类算法K- 近邻算法(K-Nearest Neighbor,KNN)。与无监督学习讲解的聚类算法不同&#xff0c;KNN 是一个有监督算法。 有监督学习是一种学习算…

多目标应用:四种多目标优化算法(NSGA2、NSPSO、NSDBO、NSCOA)求解柔性作业车间调度问题(FJSP),MATLAB代码

一、柔性作业车间调度问题 柔性作业车间调度问题(Flexible Job Scheduling Problem, FJSP) 的描述如下&#xff1a;n个工件 { J , J 2 , . . , J n } \{J,J_2,..,J_n\} {J,J2​,..,Jn​}要在 m m m 台机器 { M 1 , M 2 , . . , M m } \{M_1,M_2,..,M_m\} {M1​,M2​,..,Mm​} …

div3 970

Problem - D - Codeforces 关键在于如果是环的话&#xff0c;环中的每一个的值都是一样的 #include<bits/stdc.h> #define int long long using namespace std; signed main(){int nn;cin>>nn;while(nn--){int n;cin>>n;int a[n1],i0;while(i<n)cin>…

com.baomidou.mybatisplus.annotation.DbType 无法引入

com.baomidou.mybatisplus.annotation.DbType 无法引入爆红 解决 解决 ❤️ 3.4.1 是mybatis-plus版本&#xff0c;根据实际的配置→版本一致 <dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-annotation</artifactId>&…