首页 > 试题广场 >

平均工资

[编程题]平均工资
  • 热度指数:178996 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
查找排除在职(to_date = '9999-01-01' )员工的最大、最小salary之后,其他的9999-01-01在职员工的平均工资avg_salary
CREATE TABLE `salaries` ( `emp_no` int(11) NOT NULL,
`salary` int(11) NOT NULL,
`from_date` date NOT NULL,
`to_date` date NOT NULL,
PRIMARY KEY (`emp_no`,`from_date`));
如:
INSERT INTO salaries VALUES(10001,85097,'2001-06-22','2002-06-22');
INSERT INTO salaries VALUES(10001,88958,'2002-06-22','9999-01-01');
INSERT INTO salaries VALUES(10002,72527,'2001-08-02','9999-01-01');
INSERT INTO salaries VALUES(10003,43699,'2000-12-01','2001-12-01');
INSERT INTO salaries VALUES(10003,43311,'2001-12-01','9999-01-01');
INSERT INTO salaries VALUES(10004,70698,'2000-11-27','2001-11-27');
INSERT INTO salaries VALUES(10004,74057,'2001-11-27','9999-01-01');
输出格式:
avg_salary
73292
示例1

输入

drop table if exists  `salaries` ; 
CREATE TABLE `salaries` (
`emp_no` int(11) NOT NULL,
`salary` float(11,3) NOT NULL,
`from_date` date NOT NULL,
`to_date` date NOT NULL,
PRIMARY KEY (`emp_no`,`from_date`));
INSERT INTO salaries VALUES(10001,85097,'2001-06-22','2002-06-22');
INSERT INTO salaries VALUES(10001,88958,'2002-06-22','9999-01-01');
INSERT INTO salaries VALUES(10002,72527,'2001-08-02','9999-01-01');
INSERT INTO salaries VALUES(10003,43699,'2000-12-01','2001-12-01');
INSERT INTO salaries VALUES(10003,43311,'2001-12-01','9999-01-01');
INSERT INTO salaries VALUES(10004,70698,'2000-11-27','2001-11-27');
INSERT INTO salaries VALUES(10004,74057,'2001-11-27','9999-01-01');

输出

73292.000
为什么WHERE里套用子查询别名会报错啊
SELECT AVG(subsq.salary) AS avg_salary
FROM (SELECT salary
      FROM salaries
      WHERE to_date = '9999-01-01') AS subsq
WHERE   subsq.salaries != (SELECT (MAX(subsq.salary) FROM subsq))
    AND subsq.salaries != (SELECT (MIN(subsq.salary) FROM subsq));


发表于 2024-09-15 13:38:16 回复(0)
with t1 as (
    select *,lead(salary) over(order by salary) as 'lead_salary',
    lag(salary) over(order by salary) as 'lag_salary'
    from salaries where to_date='9999-01-01' 
) # 求工资排序后的前一个值和后一个值
select avg(salary) as 'avg_salary' from t1 where lead_salary is not null and lag_salary is not null

发表于 2024-08-16 14:09:15 回复(0)
select avg(salary) avg_salary
from salaries
where to_date='9999-01-01'
    and salary not in (select max(salary) from salaries where to_date='9999-01-01')
    and salary not in (select min(salary) from salaries where to_date='9999-01-01')

发表于 2024-07-03 16:47:44 回复(1)
select
    avg(salary)
from salaries
where salary <> (
    select
        max(salary)
    from salaries
    where to_date = '9999-01-01'
) and salary <> (
    select
        min(salary)
    from salaries
    where to_date = '9999-01-01'
)
and to_date = '9999-01-01'
发表于 2024-06-05 01:00:09 回复(0)
select avg(a.salary) as avg_salary
from
(select *,rank() over(order by salary desc) rankmax,rank() over(order by salary) rankmin
from salaries
where to_date = '9999-01-01') as a
where a.rankmax>1 and a.rankmin>
发表于 2024-05-21 11:04:11 回复(0)
SELECT
    avg(salary) AS avg_salary
FROM salaries
WHERE salary NOT IN(
    SELECT max(salary)
    FROM salaries
    WHERE to_date = '9999-01-01'
)
    AND salary NOT IN(
        SELECT min(salary)
        FROM salaries
        WHERE to_date = '9999-01-01'
    )
    AND to_date = '9999-01-01';

发表于 2024-04-02 13:12:15 回复(3)
就小于最大的,大于最小的,between也行
发表于 2024-03-26 14:17:12 回复(0)
select avg(salary) as avg_salary
from salaries
where to_date = '9999-01-01'
and salary not in
(select max(salary)
from salaries 
where to_date = '9999-01-01'
union
select min(salary)
from salaries 
where to_date = '9999-01-01'
)

发表于 2024-03-17 13:24:28 回复(0)
select avg(salary) avg_salary from salaries
where to_date='9999-01-01' 
and salary not in (select max(salary) from salaries where to_date='9999-01-01')
and salary not in (select min(salary) from salaries where to_date='9999-01-01');

发表于 2024-02-07 13:56:28 回复(0)
#使用子查询和窗口排序解题
select avg(salary) avg_salary 
from (select salary,rank()over(order by salary) rk,rank()over(order by salary desc) rkd
from salaries
where to_date ='9999-01-01') af
where rk<>1 and rkd<>1

发表于 2024-01-23 16:31:22 回复(0)
select avg(salary) from salaries as t1,
(select min(salary) as min,max(salary) as max from salaries where to_date='9999-01-01') as t2
where t1.salary not in (t2.min,t2.max)
and to_date='9999-01-01'

发表于 2024-01-14 16:28:22 回复(0)
select avg(salary) from salaries
 where salary  not in ((select min(salary) from salaries 
where to_date='9999-01-01'),(select max(salary) from salaries where to_date='9999-01-01'))
  and to_date='9999-01-01';
发表于 2023-11-18 17:08:15 回复(0)
# 查找排除在职(to_date = '9999-01-01' )员工的最大、最小salary之后,其他的在职员工的平均工资avg_salary。

with dwd_data as (
select 
    emp_no,
    salary,
    max(salary) over() as max_salary,
    min(salary) over() as min_salary
from salaries
where to_date = '9999-01-01'
)

select avg(salary) from dwd_data 
where salary not in (max_salary,min_salary);

发表于 2023-09-12 16:50:07 回复(0)
select AVG(salary)avg_salary  from salaries where to_date = '9999-01-01' and salary != (select min(salary) from salaries where to_date = '9999-01-01') and salary != (select MAX(salary) from salaries where to_date = '9999-01-01')

发表于 2023-08-17 20:55:10 回复(0)
麻了,为什么我的输出保留了三位小数呀
select 
    round(avg(salary),0)
from 
    salaries
where 
    to_date='9999-01-01'
    and salary not in(
                      (select 
                          max(salary)
                      from 
                          salaries
                      where
                          to_date='9999-01-01')
                     ,(select 
                          max(salary)
                      from 
                          salaries
                      where
                          to_date='9999-01-01')
                     )
                        

发表于 2023-05-24 11:13:10 回复(0)
SELECT
	avg(salary)
FROM
	salaries a
WHERE
	a.salary > (
		SELECT
			min(salary)
		FROM
			salaries
		WHERE
			to_date = '9999-01-01'
	)
AND salary < (
	SELECT
		max(salary)
	FROM
		salaries
	WHERE
		to_date = '9999-01-01'
)
AND to_date = '9999-01-01'

发表于 2023-02-17 14:47:05 回复(0)
统计在职员工 一次过滤就可以:
select (sum(salary)-max(salary)-min(salary))/(count(*)-2)
from salaries
where to_date = '9999-01-01'
发表于 2023-02-16 12:03:42 回复(1)
select 
    avg(salary)
from 
    salaries
where 
    to_date='9999-01-01'
and
    salary  not in   (
                    select 
                        max(salary)
                    from 
                        salaries                    
                    ) 
and salary  not in  (
                    select 
                        min(salary)
                    from
                        salaries
                    )

发表于 2023-01-03 22:06:27 回复(0)
select avg(salary) as avg_salary
from salaries
where salary not in (
    select max(salary)
    from salaries
    union
    select min(salary)
    from salaries
    where to_date='9999-01-01'
) and to_date='9999-01-01';

发表于 2022-12-24 15:26:19 回复(0)
#方法一:用rank()over()
select 
    avg(salary) as avg_salary
from (
    select 
        salary,
        rank()over(order by salary desc) as desc_ranking,
        rank()over(order by salary asc) as asc_ranking
    from salaries
    where to_date = '9999-01-01'
    ) as new_table
where new_table.desc_ranking > 1  # 最大值排名为1,去除
    and new_table.asc_ranking > 1 # 最小值排名为1,去除
    
#方法二:用min(),max()
select
    avg(salary) as avg_salary
from salaries
where to_date = '9999-01-01'    # 注意,最重要的条件要放在最先去跑,筛选出在职员工后再排序
  and salary != (select max(salary) from salaries)
  and salary != (select min(salary) from salaries)
# 这里不论是not in还是<>,!=都可以,因为select子查询只返回一行数据
发表于 2022-11-03 13:48:48 回复(1)