1

在这个线程上,我有一个问题已成功回答。

在访问查询中搜索过滤的重复项

现在我有第二个问题。也许有人可以帮助我。

我有一个如下所示的查询:

  1. 查询 A_Gravur:

    SP1 | SP2     | SP3
    
     64 | CB75000 | 3
     64 | empty   | D
     70 | CB70010 | 1
     70 | CB70010 | 1
    119 | CB70050 | 5
    119 | empty   | D
    165 | S15002  | Schalter
    165 | S15002  | Schalter
    194 | R70034  | Poti
    194 | R10023  | Potentiometer
    196 | S10063  | Schalter
    196 | S10063  | Schalter
    197 | S10063  | Schalter
    198 | S10070  | 3
    199 | CB75000 | 5
    

此查询由另一个查询过滤以检查重复项。最终结果如下所示:

结果:

SP1 | SP2     | SP3

194 | R70034  | Poti 470k
194 | R10023  | Potentiometer
196 | S10063  | Schalter
197 | S10063  | Schalter
 64 | CB75000 | 3
199 | CB75000 | 5

并且此 SQL 代码如下所示:

  SELECT SP1, SP2, SP3
  FROM A_Gravur
  WHERE (SP1 IN
    (SELECT SP1
      FROM A_Gravur
      WHERE SP3<>'D'
      GROUP BY SP1
      HAVING COUNT(*)>1)
  OR SP2 IN
    (SELECT SP2
      FROM A_Gravur
      WHERE SP3<>'D'
      GROUP BY SP2
      HAVING COUNT(*)>1))
  AND CONCAT(CAST(SP1 AS CHAR),SP2,SP3) NOT IN
    (SELECT CONCAT(CAST(SP1 AS CHAR),SP2,SP3)
      FROM A_Gravur
      WHERE SP3 <> 'D'
      GROUP BY CONCAT(CAST(SP1 AS CHAR),SP2,SP3)
      HAVING COUNT(*)>1)
  AND SP3<>'D'
  ORDER BY SP1, SP2;

所以现在我的新问题:

我在查询 A_Gravur(SP4 和 SP5)中添加了 2 个新列并进行了重复搜索

SP1 | SP2     | SP3           | SP4     | SP5       |

194 | R70034  | Poti 470k     |123546   |88859     
194 | R10023  | Potentiometer |88859    |
196 | S10063  | Schalter      |7773     |
197 | S10063  | Schalter      |99992    |
 64 | CB75000 | 3             |177777   |
199 | CB75000 | 5             |99999    |

说明:在 SP4 中总是有值(Materialnumber)。现在,当我搜索重复项时,它应该只找到这个结果:

SP1 | SP2     | SP3           | SP4     | SP5       |

196 | S10063  | Schalter      |7773     |
197 | S10063  | Schalter      |99992    |
 64 | CB75000 | 3             |177777   |
199 | CB75000 | 5             |99999    |

因为 SP5 (88859) 中的数字也在 SP4 (88859) 中,而 SP1 在两条记录中的数字相同 (194)

但如果它看起来像这样,那么结果是正确的

SP1 | SP2     | SP3           | SP4     | SP5       |

194 | R70034  | Poti 470k     |123546   |0000001     
194 | R10023  | Potentiometer |88859    |
196 | S10063  | Schalter      |7773     |
197 | S10063  | Schalter      |99992    |
 64 | CB75000 | 3             |177777   |
199 | CB75000 | 5             |99999    |

谢谢您的帮助!!

4

1 回答 1

0

这可能会做你想要的:

SELECT SP1, SP2, SP3, SP4, SP5
FROM A_Gravur
WHERE (SP1 IN
  (SELECT SP1
    FROM A_Gravur
    WHERE SP3<>'D'
    GROUP BY SP1
    HAVING COUNT(*)>1)
OR SP2 IN
  (SELECT SP2
    FROM A_Gravur
    WHERE SP3<>'D'
    GROUP BY SP2
    HAVING COUNT(*)>1))
AND CONCAT(CAST(SP1 AS CHAR), SP2,
    CAST(COALESCE(SP5, SP4) AS CHAR)) NOT IN
  (SELECT CONCAT(CAST(SP1 AS CHAR), SP2,
    CAST(COALESCE(SP5, SP4) AS CHAR))
    FROM A_Gravur
    WHERE SP3 <> 'D'
    GROUP BY CONCAT(CAST(SP1 AS CHAR),SP2,
      CAST(COALESCE(SP5, SP4) AS CHAR))
    HAVING COUNT(*)>1)
AND SP3<>'D'
ORDER BY SP1, SP2, SP3, SP4;

CONCAT()选择第一个不为空的字段,因此如果存在则选择 SP5,否则选择 SP4。

这是SQL 小提琴。如果我没有正确理解,请您在那里修改测试数据并添加带有新 SQL Fiddle 地址的注释,以便我有很好的数据可以处理。

于 2013-03-24T19:14:02.990 回答