在多个字段中查找具有相同数据的记录

时间:2014-09-18 15:11:16

标签: sql ms-access ms-access-2010

我有一个表格,用于定义在制造环境中处理产品的部门和步骤。表设计就是这样:

CustPartNum (primary key)
DeptOne
StepOne
DeptTwo
StepTwo
DeptThree
StepThree
...
...
...
DeptTwenty
StepTwenty

所有字段均为Text数据类型。

我需要编写一个查询,查找单词" Pack"在Dept字段中出现不止一次。 DeptNUMBER字段本质上是一个路由。例如,DeptOne可能是" CM",而StepOne将是在该部门发生的指令。我需要查找具有多个" Pack"的实例的所有记录。在部门领域。

我尝试过查找重复查询,但不相信这是如何实现结果。

1 个答案:

答案 0 :(得分:2)

该表格设计不合理,但您可以使用以下内容:

SELECT *
FROM
(
  SELECT s.*,
    IIf(s.DeptOne = "Pack", 1, 0) AS D1,
    IIf(s.DeptTwo = "Pack", 1, 0) AS D2,
    IIf(s.DeptThree = "Pack", 1, 0) AS D3,
    IIf(s.DeptFour = "Pack", 1, 0) AS D4,
    IIf(s.DeptFive = "Pack", 1, 0) AS D5,
    IIf(s.DeptSix = "Pack", 1, 0) AS D6,
    IIf(s.DeptSeven = "Pack", 1, 0) AS D7,
    IIf(s.DeptEight = "Pack", 1, 0) AS D8,
    IIf(s.DeptNine = "Pack", 1, 0) AS D9,
    IIf(s.DeptTen = "Pack", 1, 0) AS D10,
    IIf(s.DeptEleven = "Pack", 1, 0) AS D11,
    IIf(s.DeptTwelve = "Pack", 1, 0) AS D12,
    IIf(s.DeptThirteen = "Pack", 1, 0) AS D13,
    IIf(s.DeptFourteen = "Pack", 1, 0) AS D14,
    IIf(s.DeptFifteen = "Pack", 1, 0) AS D15,
    IIf(s.DeptSixteen = "Pack", 1, 0) AS D16,
    IIf(s.DeptSeventeen = "Pack", 1, 0) AS D17,
    IIf(s.DeptEightteen = "Pack", 1, 0) AS D18,
    IIf(s.DeptNineteen = "Pack", 1, 0) AS D19,
    IIf(s.DeptTwenty = "Pack", 1, 0) AS D20
FROM MyTable s
) m
WHERE (m.D1 + m.D2 + m.D3 + m.D4 + m.D5 + m.D6 + m.D7 + m.D8 + 
       m.D9 + m.D10 + m.D11 + m.D12 + m.D13 + m.D14 + m.D15 + 
       m.D16 + m.D17 + m.D18 + m.D19 + m.D20) > 1