操作sql表以获取一天内超额预订的员工

nwwlzxa7  于 2021-06-18  发布在  Mysql
关注(0)|答案(1)|浏览(313)

我有三张table

1. Employees (empid, lname, fname, emptype, cellphone, homphone, ftpt)
2. Schedule(date, empid, dept_name, start_time, shift_length)
3. Departments(dept_name, dept manager)`

我们必须生成一个表,列出每天被安排超过一次而超额预订的所有员工。
select语句后面的最后一个表如下所示:

按员工id排序。
下面是3个表的创建表:

CREATE TABLE Departments(department_name VARCHAR(30));
CREATE TABLE Employees(empid int PRIMARY KEY NOT NULL, lname VARCHAR(20) NOT NULL, fname VARCHAR(20) NOT NULL, emptype VARCHAR(5) NOT NULL, cellphone VARCHAR(20), homephone VARCHAR(20), ftpt VARCHAR(3) NOT NULL);
CREATE TABLE Schedule(date VARCHAR(20) NOT NULL, empid INT NOT NULL, department VARCHAR(30) NOT NULL, start_time VARCHAR(5) NOT NULL, shift_length INT NOT NULL );

示例数据计划
包含数据的文件-https://drive.google.com/drive/folders/1hsf4rbgovqrtzvypavef6fuakb29q2o_?usp=sharing

n1bvdmb6

n1bvdmb61#

您提供的一些列名不一致,因此您可能需要更改其中一些列名以匹配数据库中的实际列名。但这应该让你开始。

SELECT empid, lname as lastName, fname as firstName, date as [OVERBOOKED DATE], start_time as sfrom, Departments.dept_name as dname, dept_manager as MANAGER FROM Employees JOIN Schedule ON Employees.empid=Schedule.empid JOIN Departments on Schedule.dept_name=Departments.dept_name WHERE empid IN (SELECT empid FROM Schedule WHERE COUNT(*) > 1 GROUP BY date, empid)

相关问题