[英]Manipulating a sql table to get employees who are overbooked in a day
I have 3 Tables我有3张桌子
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)`
We have to generate a table listing all employees who are overbooked by being scheduled more than once a day.我们必须生成一个表格,列出所有因每天安排超过一次而超额预定的员工。
The final table after the select statement should look like this : select 语句后的最终表应如下所示:
it is ordered by the employee ID.它是按员工 ID 排序的。
Here is the create table for the 3 tables:这是 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 );
Sample data Schedule EMPLOYEES样本数据安排员工
Files with data - https://drive.google.com/drive/folders/1hSf4rbGOvqrTZVyPaveF6fUAKB29Q2o_?usp=sharing包含数据的文件 - https://drive.google.com/drive/folders/1hSf4rbGOvqrTZVyPaveF6fUAKB29Q2o_?usp=sharing
Some of the column names you gave are inconsistent, so you may need to change some of them to match what is actually in your database.您提供的某些列名不一致,因此您可能需要更改其中一些列名以匹配数据库中的实际内容。 But this should get you started.但这应该让你开始。
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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.