首页 > 试题广场 >

汇总各个部门当前员工的title类型的分配数目

[编程题]汇总各个部门当前员工的title类型的分配数目
  • 热度指数:310346 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
有一个部门表departments简况如下:
dept_no dept_name
d001 Marketing
d002 Finance

有一个,部门员工关系表dept_emp简况如下:
emp_no
dept_no 
from_date 
to_date
10001 d001
1986-06-26 9999-01-01
10002 d001
1996-08-03 9999-01-01
10003 d002 1995-12-03 9999-01-01


有一个职称表titles简况如下:
emp_no title form_date to_date
10001 Senior Engineer 1986-06-26 9999-01-01
10002
Staff 1996-08-03 9999-01-01
10003
Senior Engineer
1995-12-03 9999-01-01


汇总各个部门当前员工的title类型的分配数目,即结果给出部门编号dept_no、dept_name、其部门下所有的员工的title以及该类型title对应的数目count,结果按照dept_no升序排序,dept_no一样的再按title升序排序
dept_no
dept_name
title count
d001
Marketing
Senior  Engineer 1
d001
Marketing
Staff 1
d002
Finance
Senior  Engineer
1

示例1

输入

drop table if exists  `departments` ; 
drop table if exists  `dept_emp` ; 
drop table if exists  titles ;
CREATE TABLE `departments` (
`dept_no` char(4) NOT NULL,
`dept_name` varchar(40) NOT NULL,
PRIMARY KEY (`dept_no`));
CREATE TABLE `dept_emp` (
`emp_no` int(11) NOT NULL,
`dept_no` char(4) NOT NULL,
`from_date` date NOT NULL,
`to_date` date NOT NULL,
PRIMARY KEY (`emp_no`,`dept_no`));
CREATE TABLE titles (
`emp_no` int(11) NOT NULL,
`title` varchar(50) NOT NULL,
`from_date` date NOT NULL,
`to_date` date DEFAULT NULL);
INSERT INTO departments VALUES('d001','Marketing');
INSERT INTO departments VALUES('d002','Finance');
INSERT INTO dept_emp VALUES(10001,'d001','1986-06-26','9999-01-01');
INSERT INTO dept_emp VALUES(10002,'d001','1996-08-03','9999-01-01');
INSERT INTO dept_emp VALUES(10003,'d002','1995-12-03','9999-01-01');
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');

输出

d001|Marketing|Senior Engineer|1
d001|Marketing|Staff|1
d002|Finance|Senior Engineer|1
select d.dept_no,d.dept_name,title,count(title) as count from departments as d
inner join dept_emp as de on d.dept_no=de.dept_no and de.to_date='9999-01-01'
inner join titles as t on t.emp_no=de.emp_no and t.to_date='9999-01-01'
group by d.dept_no,d.dept_name,title
order by d.dept_no,title;
发表于 2024-10-26 17:24:34 回复(0)
select
d.dept_no,
m.dept_name,
t.title,
count(*) as count
from dept_emp d
join titles t
on d.emp_no = t.emp_no
left join departments m
on d.dept_no = m.dept_no
group by d.dept_no,m.dept_name,t.title
order by d.dept_no,t.title
发表于 2024-09-30 10:05:31 回复(0)
SELECT DISTINCT de.dept_no,d.dept_name,t.title,COUNT(de.dept_no) OVER(PARTITION BY de.dept_no,title ORDER BY d.dept_no)
FROM dept_emp de
JOIN departments d ON de.dept_no=d.dept_no
JOIN titles t ON de.emp_no=t.emp_no
ORDER BY dept_no,title
发表于 2024-09-04 14:08:45 回复(0)
select
    de.dept_no,
    ds.dept_name,
    title,
    count(title) as count
from
    dept_emp de
    left join titles t on de.emp_no = t.emp_no
    left join departments ds on ds.dept_no = de.dept_no
GROUP BY
    de.dept_no,
    title
ORDER BY
    dept_no,
    title asc

发表于 2024-07-27 18:13:00 回复(0)
SELECT distinct
    dt.dept_no,
    ds.dept_name,
    dt.title,
    dt.count
FROM
    (
        SELECT
            d.*,
            t.title,
            COUNT(t.title) over (
                PARTITION BY
                    d.dept_no,
                    t.title
                ORDER BY
                    d.dept_no,
                    t.title ASC
            ) AS COUNT
        FROM
            dept_emp d
            JOIN titles t ON d.emp_no = t.emp_no
    ) dt
    left JOIN departments ds ON dt.dept_no = ds.dept_no
ORDER BY
    dt.dept_no ASC,
    dt.title ASC;

发表于 2024-06-22 23:43:56 回复(0)
select
    d.dept_no,
    d.dept_name,
    t.title,
    count(*) count
from departments d left join dept_emp de
using (dept_no)
join titles t
on de.emp_no=t.emp_no
group by d.dept_no,title 
order by d.dept_no,t.title
发表于 2024-06-10 14:18:29 回复(2)
我出息了 会做题 对的 竟然

SELECT d.dept_no,	d.dept_name	,t.title,COUNT(*) 
FROM departments d , dept_emp de ,titles t
where d.dept_no = de.dept_no
and de.emp_no = t.emp_no
and de.to_date='9999-01-01'
and t.to_date='9999-01-01'   
GROUP BY d.dept_no,t.title
ORDER BY d.dept_no, t.title;

发表于 2024-05-28 18:32:36 回复(4)
select
    m.dept_no as dept_no,
    n.dept_name as dept_name,
    m.title as title,
    m.count as count
from
(
    select
        a.dept_no,b.title,count(a.emp_no) as count
    from
    (
        select emp_no,dept_no  from dept_emp where to_date='9999-01-01'
    ) as a
    join
    (
        select emp_no,title from titles where to_date='9999-01-01'
    ) as b
    on a.emp_no=b.emp_no
    group by  a.dept_no,b.title
) as m
join
(
    select * from departments
) as n
on m.dept_no=n.dept_no
order by m.dept_no asc,m.title asc
发表于 2024-05-03 15:07:12 回复(0)
为什么只对title分类不行啊,呜呜呜呜
编辑于 2024-04-02 18:08:59 回复(0)
SELECT
    de.dept_no,
    d.dept_name,
    t.title,
    count(t.title) AS count
FROM dept_emp de
LEFT JOIN departments d USING(dept_no)
LEFT JOIN titles t USING(emp_no)
WHERE t.title = ANY(
    SELECT title
    FROM titles 
    JOIN dept_emp USING(emp_no)
    WHERE dept_no = de.dept_no
)
AND t.to_date = '9999-01-01'
GROUP BY de.dept_no, d.dept_name, t.title
ORDER BY de.dept_no, t.title;

发表于 2024-03-27 12:10:50 回复(0)
  1. 将三表联表查询
  2. 通过部门编号 dept_no 和头衔 title 分组
  3. 通过 count() 数数,通过 order by 排序
select d.dept_no, d.dept_name, t.title, count(t.title) 'count'
from departments d
inner join dept_emp de on d.dept_no = de.dept_no
inner join titles t on de.emp_no = t.emp_no
group by d.dept_no, t.title
order by d.dept_no asc, t.title asc;


编辑于 2024-02-25 19:17:11 回复(0)
select a.dept_no, a.dept_name, a.title, count(a.title)
from
(select d.dept_no, d.dept_name, t.title
from departments d
left join dept_emp de on d.dept_no = de.dept_no
left join titles t on de.emp_no = t.emp_no) a
group by a.dept_no,a.title
order by a.dept_no,a.title
这个对吗?
编辑于 2024-02-21 17:54:47 回复(0)
SELECT
    d1.dept_no,
    d1.dept_name,
    d2.title,
    d2.count 
FROM
    departments d1
    LEFT JOIN (
    SELECT
        t1.title,
        d1.dept_no,
        count( t1.title ) AS count 
    FROM
        dept_emp d1
        LEFT JOIN titles t1 ON d1.emp_no = t1.emp_no 
    GROUP BY
    d1.dept_no ,
        t1.title
    
    ) d2 ON d1.dept_no = d2.dept_no
想问问我这个有啥问题吗

编辑于 2024-02-16 19:34:28 回复(0)
select
a.dept_no,a.dept_name,c.title,count(1) as count
from departments a,dept_emp b,titles c
where a.dept_no=b.dept_no and b.emp_no=c.emp_no
group by a.dept_no,a.dept_name,c.title
order by a.dept_no,c.title;
编辑于 2023-12-07 18:21:16 回复(0)
select 
d.dept_no,
d.dept_name,
t.title,
count(distinct t.emp_no) as count
from dept_emp de
left join titles t on t.emp_no = de.emp_no
left join departments d on  de.dept_no = d.dept_no
group by 1,2,3
order by d.dept_no asc,t.title asc

没有限制日期是9999-01-01 但是也ac了
发表于 2023-11-26 19:01:01 回复(0)
-- 以下是我的解答,求问一下为什么这里不用dense rank去限定排名一样的工资呢?

SELECT d.dept_no, d.dept_name, t.title,
        COUNT(t.title) AS count
FROM departments d, dept_emp de, titles t
WHERE d.dept_no = de.dept_no 
    AND de.emp_no = t.emp_no 
    AND de.to_date = '9999-01-01' 
    AND t.to_date = '9999-01-01'
GROUP BY d.dept_no, t.title
ORDER BY d.dept_no ASC, t.title ASC

发表于 2023-11-16 18:36:42 回复(0)
谁可以帮忙解释一下为什么我的代码输出的结果是这样的吗


发表于 2023-11-15 14:22:52 回复(0)
select b.dept_no,a.dept_name,c.title,count(b.emp_no) count from dept_emp b 
inner join departments a on a.dept_no=b.dept_no 
 inner join titles c on b.emp_no=c.emp_no
where b.to_date='9999-01-01' and c.to_date='9999-01-01'
group by b.dept_no,a.dept_name,c.title
order by b.dept_no,c.title
发表于 2023-10-23 15:03:45 回复(0)