• 汇总各个部门当前员工的title类型的分配数目,结果给出部门编号dept_no、dept_name、其当前员工所有的title以及该类型title对应的数目count


    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 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);

    输入描述:

    输出描述:

    dept_nodept_nametitlecount
    d001 Marketing Senior Engineer 1
    d001 Marketing Staff 1
    d002 Finance Senior Engineer 1
    d003 Human Resources Senior Staff 1
    d004 Production Senior Engineer 2
    d005 Development Senior Staff 1
    d006 Quality Management Engineer 2
    d006 Quality Management Senior Engineer 1
    思路:看创建的表以及结果:tiltle是惟一的,需要的是 departments表和title表里面的dept_no,dept_name和title以及title的重复计数,但是departments和title表没有直接关系,
    1.连接dept_emp和titles表,判断to_data=‘9999-01-01’ ,d.emp_no=t.emp_no
    2.连接department和1操作之后的表,条件dept_no==
    3.group by dept_no,title
    SELECT de.dept_no, dp.dept_name, t.title, COUNT(t.title) AS count
    FROM titles AS t INNER JOIN dept_emp AS de 
    ON t.emp_no = de.emp_no AND de.to_date = '9999-01-01' AND t.to_date = '9999-01-01'
    INNER JOIN departments AS dp 
    ON de.dept_no = dp.dept_no
    GROUP BY de.dept_no, t.title
    select dp.dept_no, dp.dept_name, t.title, count(t.title) as count 
    from titles as t inner join dept_emp as de
    on t.emp_no = de.emp_no and t.to_date = '9999-01-01'and de.to_date = '9999-01-01'
    inner join departments as dp
    on de.dept_no = dp.dept_no
    group by t.title ,de.dept_no   --group by 后面的顺序修改,输出顺序不同,不是输出描述中所需要的表,导致出错不通过
  • 相关阅读:
    java线程的几种状态
    java事务的处理
    Java多线程中Sleep与Wait的区别
    分享一百多套开发视频教程的下载地址
    [Java]读取文件方法大全
    Android开发人员必备的10 个开发工具
    CentOS 安装MySQL rpm方式安装
    记录一些经典的算法
    CentOS 7安装Redis服务
    linux查看文件大小,磁盘占用情况 du df命令
  • 原文地址:https://www.cnblogs.com/pipiyan/p/10644819.html
Copyright © 2020-2023  润新知