0

我是 SQL 的新手。我正在处理一个应该只从表中返回特定行的查询。(请参阅下面的示例源表和所需的输出)。有人可以帮我弄清楚这个吗?我尝试搜索有关迭代行的相关主题,但未能找到我寻求的答案。谢谢。

样品表

Machine   Station   Defect Code CreateTime
1         LU        f1          8/20/2013 7:07
1         LU        f2          8/20/2013 7:07
1         SLC       d1          8/20/2013 7:08
1         SLC       d2          8/20/2013 7:09
1         SLC       d3          8/20/2013 7:10
1         SLC       d1          8/20/2013 7:10
1         SLC       d2          8/20/2013 7:11
1         SLC       d1          8/20/2013 7:17
1         SLC       d1          8/20/2013 7:20
1         UL        f3          8/20/2013 7:26
1         UL        f4          8/20/2013 7:26
1         UL        f3          8/20/2013 7:26
1         UL        f4          8/20/2013 7:26
2         LU        f2          8/20/2013 7:29
2         SLC       d1          8/20/2013 7:31
2         SLC       d1          8/20/2013 7:38
2         SLC       d2          8/20/2013 7:38
2         SLC       d3          8/20/2013 7:39
2         SLC       d2          8/20/2013 7:39
2         SLC       d4          8/20/2013 7:39
2         SLC       d5          8/20/2013 7:42
2         SLC       d1          8/20/2013 7:43
2         SLC       d1          8/20/2013 7:52
2         SLC       d1          8/20/2013 7:53
2         SLC       d2          8/20/2013 7:53
2         SLC       d2          8/20/2013 7:54
2         SLC       d4          8/20/2013 7:55
2         SLC       d5          8/20/2013 7:56

需要的结果:

OCAP 在 Machine1 上触发。d1 多次出现

Machine Station Defect Code CreateTime
1       SLC     d1          8/20/2013 7:08
1       SLC     d1          8/20/2013 7:10
1       SLC     d1          8/20/2013 7:17
1       SLC     d1          8/20/2013 7:20

OCAP 在 Machine1 上触发。d1 连续出现

Machine Station Defect Code CreateTime
1       SLC     d1          8/20/2013 7:17
1       SLC     d1          8/20/2013 7:20

OCAP 在机器 2 上触发。d1 连续发生

Machine Station Defect Code CreateTime
2       SLC     d1          8/20/2013 7:31
2       SLC     d1          8/20/2013 7:38

OCAP 在机器 2 上触发。d2 多次出现

Machine Station Defect Code CreateTime
2       SLC     d2          8/20/2013 7:38
2       SLC     d2          8/20/2013 7:39
2       SLC     d2          8/20/2013 7:53
4

1 回答 1

1

多次出现是最容易的。您计算可以找到的每种组合的数量,然后选择计数大于 3 的组合。

SELECT S.Machine,
       S.Station,
       S.DefectCode,
       S.CreateTime
FROM ( SELECT Machine,
              Station,
              DefectCode,
              CreateTime,
              COUNT(CreateTime) OVER (PARTITION BY Machine, Station, DefectCode) AS DefectCount
       FROM SAMPLE
       WHERE Machine = :machine ) S
WHERE S.DefectCount > 3

连续出现有点困难(下面的查询可能不是最优的)。假设连续是“连续日期”,您可以尝试以下操作:

WITH OrderedSample
AS ( SELECT Machine,
            Station,
            DefectCode,
            CreateTime,
            ROW_NUMBER() OVER (ORDER BY CreateTime) AS RowNr
     FROM SAMPLE
     WHERE Machine = :machine )

SELECT Current.Machine,
       Current.Station,
       Current.DefectCode,
       Current.CreateTime
FROM OrderedSample Current
WHERE EXISTS ( SELECT 1
               FROM OrderedSample Next
               WHERE Current.Machine = Next.Machine
               AND   Current.Station = Next.Station
               AND   Current.DefectCode = Next.DefectCode
               AND   Current.RowNr = Next.RowNr - 1 )
OR    EXISTS ( SELECT 1
               FROM OrderedSample Prev
               WHERE Current.Machine = Prev.Machine
               AND   Current.Station = Prev.Station
               AND   Current.DefectCode = Prev.DefectCode
               AND   Current.RowNr = Prev.RowNr + 1 )

编辑

刚刚意识到第二个查询变得更容易一些,因为您使用的是固定机器:

WITH OrderedSample
AS ( SELECT Machine,
            Station,
            DefectCode,
            CreateTime,
            ROW_NUMBER() OVER (ORDER BY CreateTime) AS RowNr
     FROM SAMPLE
     WHERE Machine = :machine )

SELECT Curr.Machine,
       Curr.Station,
       Curr.DefectCode,
       Curr.CreateTime, RowNr
FROM OrderedSample Curr
WHERE EXISTS ( SELECT 1
               FROM OrderedSample Next
               WHERE Curr.Station = Next.Station
               AND   Curr.DefectCode = Next.DefectCode
               AND   Curr.RowNr = Next.RowNr - 1 )
OR    EXISTS ( SELECT 1
               FROM OrderedSample Prev
               WHERE Curr.Station = Prev.Station
               AND   Curr.DefectCode = Prev.DefectCode
               AND   Curr.RowNr = Prev.RowNr + 1 )

编辑 2:刚刚注意到 Machine & Station 是两列,已编辑

编辑 3:基于Sql Fiddle 示例的一些修复

于 2013-08-22T06:43:15.160 回答