首页 > 试题广场 >

查找薪水记录超过15条的员工号emp_no以及其对应的记录次

[编程题]查找薪水记录超过15条的员工号emp_no以及其对应的记录次
  • 热度指数:473410 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
有一个薪水表,salaries简况如下:
emp_no
salary
from_date
to_date
10001 60117 1986-06-26 1987-06-26
10001
62102
1987-06-26
1988-06-25
10001
66074 1988-06-25
1989-06-25
10001
66596 1989-06-25
1990-06-25
10001
66961 1990-06-25
1991-06-25
10001
71046 1991-06-25
1992-06-24
10001
74333 1992-06-24
1993-06-24
10001
75286 1993-06-24
1994-06-24
10001
75994 1994-06-24
1995-06-24
10001
76884 1995-06-24
1996-06-23
10001
80013 1996-06-23
1997-06-23
10001
81025 1997-06-23
1998-06-23
10001
81097 1998-06-23
1999-06-23
10001
84917 1999-06-23
2000-06-22
10001
85112 2000-06-22
2001-06-22
10001
85097 2001-06-22 2002-06-22
10002
72527 1996-08-03
1997-08-03

请你查找薪水记录超过15条的员工号emp_no以及其对应的记录次数t,以上例子输出如下:
emp_no t
10001 16

示例1

输入

drop table if exists  `salaries` ; 
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,60117,'1986-06-26','1987-06-26');
INSERT INTO salaries VALUES(10001,62102,'1987-06-26','1988-06-25');
INSERT INTO salaries VALUES(10001,66074,'1988-06-25','1989-06-25');
INSERT INTO salaries VALUES(10001,66596,'1989-06-25','1990-06-25');
INSERT INTO salaries VALUES(10001,66961,'1990-06-25','1991-06-25');
INSERT INTO salaries VALUES(10001,71046,'1991-06-25','1992-06-24');
INSERT INTO salaries VALUES(10001,74333,'1992-06-24','1993-06-24');
INSERT INTO salaries VALUES(10001,75286,'1993-06-24','1994-06-24');
INSERT INTO salaries VALUES(10001,75994,'1994-06-24','1995-06-24');
INSERT INTO salaries VALUES(10001,76884,'1995-06-24','1996-06-23');
INSERT INTO salaries VALUES(10001,80013,'1996-06-23','1997-06-23');
INSERT INTO salaries VALUES(10001,81025,'1997-06-23','1998-06-23');
INSERT INTO salaries VALUES(10001,81097,'1998-06-23','1999-06-23');
INSERT INTO salaries VALUES(10001,84917,'1999-06-23','2000-06-22');
INSERT INTO salaries VALUES(10001,85112,'2000-06-22','2001-06-22');
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,'1996-08-03','1997-08-03');

输出

10001|17
select 
    emp_no
    ,count(emp_no) t
from salaries
group by emp_no
having(count(emp_no))>15

发表于 2024-07-18 15:15:27 回复(0)
select emp_no,count(salary)
from salaries
group by emp_no
having count(salary)>15

发表于 2024-04-28 10:10:08 回复(0)
select emp_no,count(*) t from salaries
group by emp_no
having t>15;

编辑于 2024-02-05 12:11:59 回复(0)
# 请你查找薪水记录超过15条的员工号emp_no以及其对应的记录次数t,以上例子输出如下:

-- 方法一,注意去重复;
# select 
#  emp_no,
#  cnt
# from (
#   select emp_no ,count(1)over(partition by emp_no) cnt from salaries
# ) tt
# where cnt>=15
# group by emp_no, cnt
# ;

-- 方法二
select 
 emp_no,
 count(1) cnt
 from salaries
group by emp_no
having cnt>=15
;

发表于 2023-08-14 15:47:27 回复(0)
select emp_no,count(salary) t from salaries
group by emp_no
having t>15
发表于 2023-07-24 23:49:58 回复(0)
SELECT emp_no ,COUNT(salary) as t
FROM salaries 
GROUP BY emp_no
HAVING t > 15

发表于 2022-12-14 13:17:10 回复(0)
select 
    emp_no,
    count(*) t
from salaries 
group by emp_no
having count(*)>15

发表于 2022-12-06 22:20:56 回复(0)
select distinct emp_no,count(emp_no) t from salaries
group by emp_no
having t>15
发表于 2022-09-01 00:21:57 回复(0)
select emp_no,count(salary)as t from salaries
group by emp_no
having t > 15;
发表于 2022-08-06 20:07:05 回复(0)
select
  emp_no,
  count(emp_no)
from
  salaries
group by
  emp_no
having
  count(emp_no) > 15
发表于 2022-08-04 13:32:54 回复(0)
select 
    emp_no,
    count(emp_no) as t
from salaries 
group by emp_no
having t>15;
# 这有个大于15的条件,但不能写在where,因为where后面不能跟聚合条件,>已经是聚合条件了
发表于 2022-08-02 22:45:41 回复(0)
一直把have写成hive,我是大数据学废了
发表于 2022-07-07 21:52:36 回复(0)
select emp_no,count(emp_no) as t
from salaries
group by emp_no
having t > 15;
发表于 2022-06-01 11:04:39 回复(0)
select emp_no,count(emp_no) as t from salaries group by emp_no having count(emp_no)>=15
发表于 2022-05-21 19:05:33 回复(0)
select 
emp_no,count(*) as t
from salaries
group by emp_no
having count(*)>=15

发表于 2022-05-15 11:31:25 回复(0)
select emp_no,count(*) t from salaries group by emp_no having t>15;

发表于 2022-05-11 11:16:51 回复(0)
select
    emp_no,
    count(*) t
from
    salaries
group by
    emp_no
having       
    t>15
发表于 2022-04-25 19:23:18 回复(0)
SELECT emp_no,COUNT(*) t 
FROM salaries
GROUP BY 1
HAVING t > 15

发表于 2022-04-24 18:47:49 回复(0)