有2张桌子:
Employee
(id_employee, worker_name)
Groups
(id_employee, group_name)
这是为您准备的创建脚本:
CREATE TABLE Employee (
id_employee int identity(1,1) NOT NULL CONSTRAINT PK_Employee PRIMARY KEY CLUSTERED,
worker_name nvarchar(100) CONSTRAINT UQ_Employee_worker_name UNIQUE
);
CREATE TABLE Groups (
id_employee int NOT NULL CONSTRAINT FK_Groups_id_employee FOREIGN KEY REFERENCES Employee (id_employee),
group_name varchar(10) NOT NULL,
CONSTRAINT PK_Groups PRIMARY KEY CLUSTERED (group_name, id_employee)
);
INSERT Employee
SELECT 'worker 1'
UNION ALL SELECT 'worker 2'
UNION ALL SELECT 'worker 3'
UNION ALL SELECT 'worker 4';
INSERT Groups
SELECT 1, 'a1'
UNION ALL SELECT 2, 'a1'
UNION ALL SELECT 3, 'a2'
UNION ALL SELECT 4, 'a2'
UNION ALL SELECT 1, 'b1'
UNION ALL SELECT 2, 'b1'
UNION ALL SELECT 3, 'b2'
UNION ALL SELECT 4, 'b2'
UNION ALL SELECT 2, 'b3'
UNION ALL SELECT 3, 'b3'
UNION ALL SELECT 4, 'b3';
我需要一个查询,对于给定的id_group
将返回具有完全相同员工的所有其他组。
例如:
SELECT for 'a1'
-> 应该返回 'b1' 因为在这两个组中都有:工人 1 和工人 2
SELECT for 'a2'
-> 应该返回“b2”,因为在这两个组中都有:工人 3 和工人 4
请注意,组需要完全相同,所有成员都a1
需要在其中,b1
并且两个组的大小也需要相同。