如何返回满足特定条件的特定行

时间:2013-08-22 06:10:37

标签: sql-server-2008

我是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

需要的结果:

在Machine1上触发OCAP。 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

在Machine1上触发OCAP。 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

1 个答案:

答案 0 :(得分: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 :刚注意到机器&站是两列,编辑

编辑3 :基于Sql Fiddle example

的一些修复