SQL 查找第一次出现

SQL Find First Occurrence

我已经做了大约一个小时了,但几乎没有任何进展 - 以为我会来这里 help/advice。

因此,给定我的示例 table:

+-----------+-----------------------------+--------------+
| MachineID | DateTime                    | AlertType    |
+-----------+-----------------------------+--------------+
| 56        | 2015-10-05 00:00:23.0000000 | 2000         |
| 42        | 2015-10-05 00:01:26.0000000 | 1006         |
| 50        | 2015-10-05 00:08:33.0000000 | 1018         |
| 56        | 2015-10-05 00:08:48.0000000 | 2003         |
| 56        | 2015-10-05 00:10:15.0000000 | 2000         |
| 67        | 2015-10-05 00:11:59.0000000 | 3001         |
| 60        | 2015-10-05 00:13:02.0000000 | 1006         |
| 67        | 2015-10-05 00:13:08.0000000 | 3000         |
| 56        | 2015-10-05 00:13:09.0000000 | 2003         |
| 67        | 2015-10-05 00:14:50.0000000 | 1018         |
| 67        | 2015-10-05 00:15:00.0000000 | 1018         |
| 47        | 2015-10-05 00:16:55.0000000 | 1006         |
+-----------+-----------------------------+--------------+

我如何获得第一次出现的 MachineID w/ 2000 的 AlertType 以及 2003 年相同 MachineID w/ 和 AlertType 的最后一次出现。

这是我尝试过的 - 但它没有输出我期望的结果。

SELECT *
FROM [Alerts] a
where 
    DateTime >= '2015-10-05 00:00:00'
    AND  DateTime <= '2015-10-06 00:00:00'
    and not exists(
        select b.MachineID 
        from [Alerts] b
        where   b.AlertType=a.AlertType and
                b.MachineID<a.MachineID
    )
order by a.DateTime ASC

编辑: 上面的代码没有得到我想要的,因为我没有特别告诉它搜索 AlertType = 2000AlertType = 2003,但即使我尝试了,我仍然无法收集到我想要的结果。

这是我希望输出显示的内容:

+-----------+-----------------------------+--------------+
| MachineID | DateTime                    | AlertType    |
+-----------+-----------------------------+--------------+
| 56        | 2015-10-05 00:00:23.0000000 | 2000         |
| 56        | 2015-10-05 00:13:09.0000000 | 2003         |
+-----------+-----------------------------+--------------+

如有任何帮助,我们将不胜感激!

不确定,但是:

select * from [Table] 
WHERE [DateTime] IN (
            SELECT MIN([DateTime]) as [DateTime] 
            FROM [Table] 
            WHERE AlertType = 2000 
            GROUP BY MachineId 
                UNION ALL 
            SELECT MAX([DateTime]) as [DateTime] 
            FROM [Table] 
            WHERE AlertType = 2003 
            GROUP BY MachineId)
ORDER BY MachineId, AlertType

不确定你的尝试与你的问题有什么关系,但要回答这个问题:

How would I get the first occurrence of MachineID w/ an AlertType of 2000 and the last occurrence of the same MachineID w/ and AlertType of 2003.

简单:

SELECT * FROM (
    SELECT TOP 1 * FROM Alerts WHERE AlertType='2000' ORDER BY Datetime ASC
    UNION ALL 
    SELECT TOP 1 * FROM Alerts WHERE AlertType='2003' ORDER BY Datetime DESC
) t

看起来你的外部部分包含 2015-10-05 到 2015-10-06 之间的所有记录,其中包括按日期排序的所有记录。内部部分仅在没有记录符合外部日期范围时发生。

看起来 GSazheniuk 是对的,但我不确定您是只想要 2 条记录还是与 MachineID 和两个警报匹配的所有内容?

我想每个人都没有注意到您的警报类型不是决定因素,而是补充因素。 这应该给你你正在寻找的东西。我走过了整个过程。 `IF OBJECT_ID('tempdb..#alerts') 不是 NULL DROP table #alerts

    CREATE TABLE #alerts
    (
    MachineID int,
    dte DATETIME,
    alerttype int
    )
INSERT INTO #alerts VALUES ('56','20151005 00:00:23','2000')
INSERT INTO #alerts VALUES ('42','20151005 00:01:26','1006')
INSERT INTO #alerts VALUES ('50','20151005 00:08:33','1018')
INSERT INTO #alerts VALUES ('56','20151005 00:08:48','2003')
INSERT INTO #alerts VALUES ('56','20151005 00:10:15','2000')
INSERT INTO #alerts VALUES ('67','20151005 00:11:59','3001')
INSERT INTO #alerts VALUES ('60','20151005 00:13:02','1006')
INSERT INTO #alerts VALUES ('67','20151005 00:13:08','3000')
INSERT INTO #alerts VALUES ('56','20151005 00:13:09','2003')
INSERT INTO #alerts VALUES ('67','20151005 00:14:50','1018')
INSERT INTO #alerts VALUES ('67','20151005 00:15:00','1018')
INSERT INTO #alerts VALUES ('47','20151005 00:16:55','1006')
GO
WITH rnk as ( --identifies the order of the records. 
    Select 
        MachineID,
        dte = dte,
        rnk = RANK() OVER (partition BY machineid ORDER BY dte DESC) --ranks the machine ID's based on date (first to Last)
        FROM #alerts
        ),
    agg  as( --Pulls your first  and last record
    SELECT 
        MachineID,
        frst = MIN(rnk), 
        lst = MAX(rnk) 
    FROM rnk 
    GROUP BY MachineID
    )
SELECT
    pop.MachineID,
    pop.dte,
    pop.alerttype

FROM #alerts pop
JOIN rnk r ON pop.MachineID = r.MachineID AND pop.dte = r.dte --the date join allows you to hook into your ranks
JOIN agg ON pop.MachineID = agg.MachineID 
WHERE agg.frst = r.rnk OR agg.lst = r.rnk -- or clause can be replaced by two queries with a union all
ORDER BY 1,2 --viewability... machineID, date`

我个人使用交叉应用来执行这样的任务,但 CTE 对于此练习在视觉上更加友好。