首页 > 试题广场 >

从titles表获取按照title进行分组,注意对于重复的e

[编程题]从titles表获取按照title进行分组,注意对于重复的e
  • 热度指数:146517 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
从titles表获取按照title进行分组,每组个数大于等于2,给出title以及对应的数目t。
注意对于重复的emp_no进行忽略(即emp_no重复的title不计算,title对应的数目t不增加)。
CREATE TABLE IF NOT EXISTS `titles` (
`emp_no` int(11) NOT NULL,
`title` varchar(50) NOT NULL,
`from_date` date NOT NULL,
`to_date` date DEFAULT NULL);
如插入:
INSERT INTO titles VALUES(10001,'Senior Engineer','1986-06-26','9999-01-01');
INSERT INTO titles VALUES(10002,'Staff','1996-08-03','9999-01-01');
INSERT INTO titles VALUES(10003,'Senior Engineer','1995-12-03','9999-01-01');
INSERT INTO titles VALUES(10004,'Engineer','1986-12-01','1995-12-01');
INSERT INTO titles VALUES(10004,'Senior Engineer','1995-12-01','9999-01-01');
INSERT INTO titles VALUES(10005,'Senior Staff','1996-09-12','9999-01-01');
INSERT INTO titles VALUES(10005,'Staff','1989-09-12','1996-09-12');
INSERT INTO titles VALUES(10006,'Senior Engineer','1990-08-05','9999-01-01');
INSERT INTO titles VALUES(10007,'Senior Staff','1996-02-11','9999-01-01');
INSERT INTO titles VALUES(10007,'Staff','1989-02-10','1996-02-11');
INSERT INTO titles VALUES(10008,'Assistant Engineer','1998-03-11','2000-07-31');
INSERT INTO titles VALUES(10009,'Assistant Engineer','1985-02-18','1990-02-18');
INSERT INTO titles VALUES(10009,'Engineer','1990-02-18','1995-02-18');
INSERT INTO titles VALUES(10009,'Senior Engineer','1995-02-18','9999-01-01');
INSERT INTO titles VALUES(10010,'Engineer','1996-11-24','9999-01-01');
INSERT INTO titles VALUES(10010,'Engineer','1996-11-24','9999-01-01');


输入描述:


输出描述:
title t
Assistant Engineer 2
Engineer 3
省略 省略
Staff 3
示例1

输入

输出

此题应注意以下三点:
1、先用GROUP BY title将表格以title分组,再用COUNT(DISTINCT emp_no)可以统计同一title值且不包含重复emp_no值的记录条数
2、根据题意,输出每个title的个数为t,故用AS语句将COUNT(DISTINCT emp_no)的值转换为t
3、由于WHERE后不可跟COUNT()函数,故用HAVING语句来限定t>=2的条件
SELECT title, COUNT(DISTINCT emp_no) AS t FROM titles
GROUP BY title HAVING t >= 2

发表于 2017-07-12 20:07:22 回复(30)
小编你们的语文都是体育老师教的吗
写清楚是emp_no重复的title不计算很难吗
发表于 2019-08-29 19:54:17 回复(11)
//先找出emp_no唯一的表格,再在其中查找
select title,count(*) as t
from (select distinct emp_no,title,from_date,to_date
     from titles )
group by title having t>=2;

发表于 2017-07-10 11:07:32 回复(13)
员工在不同阶段,可能担任了相同的职务,要对这种情况进行去除
发表于 2019-09-22 14:44:24 回复(1)
去重复用distinct

select title ,count(distinct emp_no)as n
from titles
group by title
having n>=2;
发表于 2017-07-20 16:20:39 回复(1)
本题说选择title并且按照title分组,title分组数大于2的选出来,so
第一句   select title,count(title) as t
从titles表选,可是最后要去重emp_no,所以我们需要在选之前去重
第二句:from (select title,distinct(emp_no) from titles)
最后分组title体现出来
group by title having t >=2
发表于 2019-03-23 21:49:49 回复(2)

分享一个比较好理解的答案:

select title, count(*) as t from (
    select distinct * from titles
)
group by title having t >= 2
发表于 2019-09-01 16:37:34 回复(0)
select title,count(*) as t
from (
  select distinct emp_no,title,from_date,to_date from titles
)
group by title
having t>=2;
发表于 2019-07-30 22:06:11 回复(1)
SELECT
    title,
    count(title) AS t
FROM
    (
        SELECT DISTINCT
            emp_no,title
        FROM
            titles
    ) AS temp
GROUP BY
    title
HAVING
    t >= 2;

1.通过子查询,返回distinct 后的数据表
2.再进行分组计算
发表于 2018-07-02 13:23:42 回复(1)
select title ,count(distinct emp_no) t from titles group by title having t>=2;
发表于 2017-12-08 15:53:51 回复(0)
SELECT 
	title,
	COUNT(DISTINCT emp_no) AS t
FROM titles
GROUP BY 
	title
HAVING 
	t>=2

发表于 2017-09-15 15:51:49 回复(0)
题意有点难懂...

员工可以内部调整(同title中有重复emp_no),可以跨职位调整(不同title中有重复emp_no)。要统计的是对每个title,曾经都有过多少员工干过。最后筛选大于等于2的。

1.group by title 之后 count(distinct emp_no) 
对每个title,去除其中emp_no重复的

2.先 distinct emp_no, title 之后 group by title 即可
去除emp_no和title同时重复的记录,之后再group by title。

这里方法2要注意:distinct后面接两个字段表示把这两个字段作为整体去重;只对一个字段去重,同时想选取多个字段,无法用distinct实现。话说回来,这里如果只对emp_no去重就做错了,员工1在部门1和部门2中都干过,去重后,部门1和部门2的t值必然有一个会少1。

发表于 2020-09-04 12:00:29 回复(0)
SELECT title, COUNT(DISTINCT emp_no) AS t
FROM titles
GROUP BY title
HAVING t >= 2;
发表于 2020-05-16 21:40:37 回复(0)
题目意思是说对重复的emp_no进行忽略处理,那么久用一个 distinct关键字去重,总觉这题目不严谨

select  title ,count(distinct emp_no) as t
from titles 

group by title 
having t >= 2


发表于 2020-03-07 16:54:49 回复(0)
SELECT title,COUNT(DISTINCT emp_no) AS t FROM titles
GROUP BY title HAVING t >=2
题目没有说清楚,想表达的意思是每个title下,emp_no重复的员工忽略不计
发表于 2019-09-17 11:18:54 回复(0)
解答:
select title,count(distinct(emp_no)) t from titles group by title having t>=2
1.因为需要先select,再在其中数据过滤,所以使用having;
2.因为过滤,所以需要关键字distinct。
发表于 2019-07-30 15:33:47 回复(0)
select title, count(distinct emp_no) as t
from titles
group by title
having t >= 2
发表于 2019-06-26 23:47:48 回复(1)
SELECT title, COUNT(DISTINCT emp_no) AS t FROM titles
GROUP BY title HAVING t >= 2;
发表于 2019-06-18 11:30:32 回复(0)
1.根据指定列进行分组使用group by,指定的列的值需要满足什么条件使用having,查找某个列出现的次数使用count,而查找出现的次数时相同的值不进行累加使用distinct关键字
SELECT title, COUNT(DISTINCT emp_no) AS t FROM titles GROUP BY title HAVING t>=2;

发表于 2017-09-03 21:33:15 回复(0)
通过的sql语句是:select  title,count(*) as t from (select distinct title,emp_no from titles)  group by title having t>=2;
这里要注意的是,如果你改成select distinct emp_no from titles),因为这样的话,得出的结果字段名就只有emp_no。所以就会报错,没有title这样的column name。所以要把title加上。
发表于 2017-08-30 09:48:53 回复(1)