0

可能重复:
如何按小时或按 10 分钟分组时间

随着 MSSQL 得到这个结果:

在此处输入图像描述

从以下查询:

SELECT [DATESTAMP],[ZONE],[ALARM] 
FROM [UNIT].[dbo].[LOG] 
WHERE [DATESTAMP] BETWEEN @StartDate AND @EndDate 
    AND [ALARM] ='ERROR' 
    AND [ZONE] IN ('BOX 11','BOX 12') 
ORDER BY DATESTAMP DESC

我将如何更改查询以仅获取行 2,5,9,13,17,19,20 ?

这意味着我只想要跨越大约 1 分钟的组中的第一行(最旧的)。在该组内,而不会出现比 15 分钟更频繁的错误,Freewheelin

4

2 回答 2

0

结束了:

SELECT  MIN([DATESTAMP]) as FirstOccurance FROM ....
WHERE ....
....
GROUP BY DATEPART(minute, [DATESTAMP])
ORDER BY FirstOccurance DESC

-再次感谢大家

于 2012-12-14T22:50:40.763 回答
0

如果我理解你,你想要每个特定分钟的最早条目,对吗?如果是这样,试试这个:

SELECT
    DateStamp,
    ZONE,
    Alarm
FROM
    [UNIT].[dbo].[LOG]  
    JOIN 
    (   SELECT 
            DateMinute = DATEADD(second, -DATEPART(second, DateStamp), DateStamp),
            Earliest = MIN(DateStamp)
        FROM
            [UNIT].[dbo].[LOG]  
        GROUP BY
            DATEADD(second, -DATEPART(second, DateStamp), DateStamp)
    ) X ON [UNIT].[dbo].[LOG].DateStamp = X.Earliest
于 2012-12-14T21:11:59.190 回答