diff --git "a/52\345\217\267 \347\250\213\345\270\205\347\277\224/10\346\234\21013\346\227\245\344\275\234\344\270\232.md" "b/52\345\217\267 \347\250\213\345\270\205\347\277\224/10\346\234\21013\346\227\245\344\275\234\344\270\232.md" new file mode 100644 index 0000000000000000000000000000000000000000..13c189d967529c401682265f240a6fadcf500e14 --- /dev/null +++ "b/52\345\217\267 \347\250\213\345\270\205\347\277\224/10\346\234\21013\346\227\245\344\275\234\344\270\232.md" @@ -0,0 +1,126 @@ +# 作业 + +create database text1 charset utf8; + +use text1; + +create table if not exists `employee` +( + `eid` int not null auto_increment comment '员工id' primary key, + `ename` varchar(20) not null comment '员工名称', + `dname` varchar(50) not null comment '部门名称', + `hiredate` datetime not null comment '入职日期', + `birth` date not null comment '生日', + `salary` double null comment '基本薪资', + `start_sal` double null comment '入职薪资' +); + +insert into `employee` (`ename`, `dname`, `hiredate`,`birth`, `salary`,`start_sal`) values ('傅嘉熙', '开发部', '2002-08-20 12:00:04','1980-12-10', 9000,6500); +insert into `employee` (`ename`, `dname`, `hiredate`, `birth`,`salary`,`start_sal`) values ('武晟睿', '开发部', '2002-06-12 13:54:12', '1984-2-5',9500,6000); +insert into `employee` (`ename`, `dname`, `hiredate`, `birth`,`salary`,`start_sal`) values ('孙弘文', '开发部', '2003-10-16 08:27:06','1979-8-7', 9400,8000); +insert into `employee` (`ename`, `dname`, `hiredate`,`birth`, `salary`,`start_sal`) values ('潘乐驹', '开发部', '2004-04-22 03:56:11','1980-5-12', 9500,6800); +insert into `employee` (`ename`, `dname`, `hiredate`,`birth`, `salary`,`start_sal`) values ('潘昊焱', '人事部', '2007-02-24 03:40:02','1987-2-12', 5000,4500); +insert into `employee` (`ename`, `dname`, `hiredate`,`birth`, `salary`,`start_sal`) values ('沈涛', '人事部', '2012-12-14 09:16:37','1993-4-30', 6000,5500); +insert into `employee` (`ename`, `dname`, `hiredate`, `birth`,`salary`,`start_sal`) values ('江峻熙', '人事部', '2018-05-12 01:17:48','1990-6-8', 5000,3000); +insert into `employee` (`ename`, `dname`, `hiredate`,`birth`, `salary`,`start_sal`) values ('陆远航', '人事部', '2018-04-14 03:35:57','1989-11-13', 5500,5000); +insert into `employee` (`ename`, `dname`, `hiredate`, `birth`,`salary`,`start_sal`) values ('姜煜祺', '销售部', '2020-03-23 03:21:05','1995-1-1', 6000,5500); +insert into `employee` (`ename`, `dname`, `hiredate`, `birth`,`salary`,`start_sal`) values ('邹明', '销售部', '2015-11-23 23:10:06','1996-2-19', 6800,6000); +insert into `employee` (`ename`, `dname`, `hiredate`, `birth`,`salary`,`start_sal`) values ('董擎苍', '销售部', '2012-02-12 07:54:32','1985-10-7', 6500,4800); +insert into `employee` (`ename`, `dname`, `hiredate`,`birth`, `salary`,`start_sal`) values ('钟俊驰', '销售部', '2010-04-10 12:17:06','1981-3-25', 6000,3500); + + +-- 员工按工龄,每年增加50元薪水。实发薪资 = 基本薪资 + 工龄 * 50 + +-- 窗口函数 + +select * from employee; +#求每个部门的员工总数 +select DISTINCT dname,count(eid) over(partition by dname) from `employee`; +#求每个部门的平均工资 +with +num as (select eid,floor(datediff(NOW(),hiredate)/365) as 工龄 from `employee`) +select DISTINCT dname,avg(salary+n.工龄*50) over(partition by dname) as 平均工资 from employee e,num n where e.eid=n.eid; + +-- select DISTINCT dname,avg(salary) over(partition by dname) from employee; +#求每个部门的工资排名(从高到低,相同工资并列,并执行跳过排序) +select dname,salary,rank() over(partition by dname order by salary) from employee; +#求公司所有员工的年龄排序(相同年龄并列,执行跳过排序) +select dname,birth,rank() over(order by birth) from employee; +#求每个部门的员工工龄排序(相同年龄并列,执行顺序排序) + +select dname,birth,a.工龄,rank() over(partition by dname order by a.工龄) + from employee e,(select eid,floor(datediff(NOW(),hiredate)/365) as 工龄 from `employee`) a where e.eid=a.eid; + +#计算每个员工的工资与该部门平均工资的差额 +with +num as (select eid,floor(datediff(NOW(),hiredate)/365) as 工龄 from `employee`), +n_avg as (select e.ename,e.eid,dname,avg(salary+n.工龄*50) over(partition by dname) as 平均工资 from employee e,num n where e.eid=n.eid), +b as (select e.eid,dname,salary+n.工龄*50 as 工资 from employee e,num n where e.eid=n.eid) +select n.dname,n.ename,b.工资-n.平均工资 差额 from n_avg n,b where n.eid=b.eid; + + + + + +#按员工工资进行排序,比较相邻两个员工的工资,输出比较高的工资 +select ename,salary,max(salary) over (rows between 1 preceding and 1 following) as 比较高的工资 from employee; +#按员工工资进行排序,查询当前员工与前一位和后一位的工资平均值 +select ename,salary,avg(salary) over (rows between 1 preceding and 1 following) as 工资平均值 from employee; +#按员工工资进行排序,查询当前员工至最后一位员工的工资总和 +select ename,salary,sum(salary) over (rows between current row and unbounded following) as 工资总和 from employee; +#计算每个部门内最高薪资与平均薪资的差额 +select distinct dname,max(salary) over (partition by dname)- avg(salary) over (partition by dname) as 差额 from employee; +#找出各部门年薪第二高的员工 +select e.dname,e.salary,e.n as 年薪第二高 from +(select dname,salary,dense_rank() over (partition by dname order by salary) as n from employee) e where e.n=2; +#查询各部门中小于等于当前员工实际薪资的比例 +select dname,ename,salary, +cume_dist() over(partition by dname order by salary asc) '占比' +from employee; +#查询每个员工工资在全部员工中的排名比例 +select ename,salary,sum(salary) over(order by salary) as 总工资,salary/sum(salary) over() 排名比例 +from employee; +#查询每个部门工资排名在前25%的员工记录数 +select dname,ename,salary,e.a as '前25%的员工记录数' from + (select dname,ename,salary,cume_dist() over(partition by dname order by salary) as a from employee) e where e.a>0.25; +#每个部门按年龄进行排序,求当前员工与前一位员工的年龄差 +with +a as (select eid,floor(datediff(NOW(),birth)/365) as age from `employee`) +select ename,a.age as 年龄,max(a.age) over (rows between 1 preceding and current row)-a.age as 年龄差 from employee e,a where e.eid=a.eid; +#按入职日期进行排序,查询公司每个员工与后面一个员工的入职天数差异 +WITH +num as (select eid,floor(datediff(NOW(),hiredate)) as age from `employee`) +select ename,n.age as 入职日期,max(n.age) over (rows between 1 preceding and 1 following)-n.age as 入职天数差异 from employee e,num n where e.eid=n.eid; +#将每个部门的员工按工资平均分为2个组,组1为低工资,组2为高工资 +select dname,ename,salary, +ntile(2) over(partition by dname order by salary) '组' +from employee; +#将所有员工按照工龄分为4个组,并统计每个组的人数 +with +a as (select eid,floor(datediff(NOW(),hiredate)/365) as age from `employee`) +select ename,a.age as 工龄,ntile(4) over(order by a.age) as '组' from employee e,a where e.eid=a.eid; +#将员工按照工资分为3个组,并统计组别,每组平均工资,工资范围(first_value、last_value) + + + +### -- 非窗口函数 + +#按照工龄区分等级,小于5年为新员工,5-15年为老员工,大于15年为骨灰级员工,输出姓名,部门,工龄,级别 +select ename,dname,hiredate,(datediff(now(),hiredate)/365) as 工龄, + case when (datediff(now(),hiredate)/365) < 5 then '新员工' + when (datediff(now(),hiredate)/365) < 15 then '老员工' + else '骨灰级员工' end as 级别 +from employee; +#返回员工的实际年龄,如果小于当前日期则减1岁 +select ename,dname,floor((datediff(now(),birth)/365)) as 年龄 from employee; + + +select * from employee; +#求每个员工还有多少天过生日,并返回下次生日是星期几 +select ename, + abs(datediff(concat_ws('-',year(curdate()),month(birth),day(birth)),curdate())) as 距离生日天数, + date_format(concat_ws('-',year(curdate())+1,month(birth),day(birth)),'%W') as 下次生日星期 +from employee; + +#求每个员工当前实发工资与入职时工资的增长率,输出员工姓名,部门,入职工资,实际工资,增长率 +select ename,dname,hiredate,salary,round((salary-start_sal) / start_sal,2) as 增长率 from employee \ No newline at end of file