MS访问所有可能的组合字段,无需重复

时间:2016-09-19 18:15:18

标签: ms-access

我有一个名为Tbl_Employee的表。 该表中有2个字段,名为Emp_Name& EMP_ID。 Emp_ID是一个关键字段。

现在我在这个数据库中有4名员工,但这会随着时间的推移而增长。

我正在努力寻找所有可能的员工组合。

" Mark L"

" Jesse L"

" Blake P"

"布鲁克斯A"

输出将是这样的:

Mark L

Jesse L

Blake P

布鲁克斯A

Mark L - Jesse L

Mark L - Jesse L - Blake P

Mark L - Jesse L - Brooks A

Mark L - Blake P

Mark L - Brooks A

等...

我做了一个查询来做我想要的但我无法弄清楚如何过滤掉重复的记录。它也不会只显示一个名称。

下面的查询也必须在添加员工时进行更改。

这是我的代码:

SELECT Emp1.Emp_Name AS Emp1,Emp2.Emp_Name AS Emp2,Emp3.Emp_Name AS Emp3,Emp1.Employee_ID AS ID1,Emp2.Employee_ID AS ID2,Emp3.Employee_ID AS ID3,[Emp1]& " - " &安培; [Emp2]& " - " &安培; [Emp3] AS Expr1,[ID1]& " - " &安培; [ID2]& " - " &安培; [ID3] AS ID FROM Tbl_Employee AS Emp1,Tbl_Employee AS Emp2,Tbl_Employee AS Emp3;

非常感谢任何帮助。

由于

1 个答案:

答案 0 :(得分:1)

这假设emp_id是一个数字字段。

Select Emp_name as combo
from tbl_employee

union all SELECT T1.Emp_Name & " - " & T2.Emp_Name 
FROM tbl_employee AS T1, tbl_employee AS T2
WHERE T2.emp_id > T1.emp_id

union all 
SELECT T1.Emp_Name & " - " & T2.Emp_Name & " - " & T3.emp_name
FROM tbl_employee AS T1, tbl_employee AS T2,  tbl_employee as T3
WHERE T3.emp_id > T2.emp_id 
      and T2.emp_id > t1.emp_id

union all 
SELECT T1.Emp_Name & " - " & T2.Emp_Name & " - " & T3.emp_name & " - " & T4.emp_name
FROM tbl_employee AS T1, tbl_employee AS T2,  tbl_employee as T3, tbl_employee as T4
WHERE T4.emp_id > T3.emp_id 
      and T3.emp_id > T2.emp_id 
      and T2.emp_id > t1.emp_id

然后,您可以向SQL添加另一个块以进行更大的选择 - 每个分组5,6或更多。