SQL列表周期,其中值大于特定值

时间:2015-09-01 15:53:41

标签: sql-server sql-server-2014

我有一个像Id,DateTime,Device,Value这样的表。 我需要找到Value> gt = = 5的所有句点,并返回此期间的From和To dateTime。 设备可以是温度传感器,我需要一个温度超过5的From / To列表。

如果我们认为它是一个温度传感器,它每5分钟记录一次。表格将是这样的:

Id DateTime         Device Value
--------------------------------
1  2015.09.01 09:10  T1     3,2 
2  2015.09.01 09:15  T1     5,2 
3  2015.09.01 09:20  T1     6,2 
4  2015.09.01 09:25  T1     5,8 
5  2015.09.01 09:30  T1     3,2 
6  2015.09.01 09:35  T1     1,2 
7  2015.09.01 09:40  T1     5,6 
8  2015.09.01 09:45  T1     6,1 
9  2015.09.01 09:50  T1     5,0 
10 2015.09.01 09:55  T1     2,0 

我试图找到的时间是从09:15到09:25以及09:40到09:50。

在SQL Server 2014中是否有这样做的智能方法?

4 个答案:

答案 0 :(得分:2)

这是获得所需结果的一种方式:

SELECT MIN([DateTime]) AS StartPeriod, MAX([DateTime]) AS EndPeriod,
       Device
FROM (       
SELECT Id, [DateTime], Device, Value,
       ROW_NUMBER() OVER (PARTITION BY Device ORDER BY Id) -
       COUNT(CASE WHEN Value >= 5 THEN 1 END) 
       OVER (PARTITION BY Device ORDER BY Id) AS grp
FROM mytable ) AS t
WHERE t.Value >= 5
GROUP BY Device, grp

如果您希望每天获得PARTITION BY Device, [DateTime] - Start个字段,则可能需要End

Demo here

答案 1 :(得分:0)

是的,有一种聪明的方法可以做到这一点。

看看'PARTITION BY'和'OVER'条款。

使用这些功能我相信你应该能够对句点中的数据进行分区(分组)并获得每个组的DateTime列的MIN和MAX值。

答案 2 :(得分:0)

这基本上是answered before,诀窍在于你已经连续和连续地订购了id列。如果那不存在,你需要做更多的前期工作,以使减法正常工作。

select * into #tempTable from (
select Id = 1,[DateTime] = cast('2015.09.01 09:10' as datetime), device = 'T1', value = 3.2
union all select 2,cast('2015.09.01 09:15' as datetime),'T1',5.2
union all select 3,cast('2015.09.01 09:20' as datetime),'T1',6.2
union all select 4,cast('2015.09.01 09:25' as datetime),'T1',5.8
union all select 5,cast('2015.09.01 09:30' as datetime),'T1',3.2
union all select 6,cast('2015.09.01 09:35' as datetime),'T1',1.2
union all select 7,cast('2015.09.01 09:40' as datetime),'T1',5.6
union all select 8,cast('2015.09.01 09:45' as datetime),'T1',6.1
union all select 9,cast('2015.09.01 09:50' as datetime),'T1',5
union all select 10,cast('2015.09.01 09:55' as datetime),'T1',2) A


with T as (select *, row_number() over (order by id) - id as grp
    from #tempTable
    where value >=5
    )
select [to] = min(T.Datetime), [from] =  max(T.DateTime) 
from T
group by T.grp
order by T.grp

输出为

to                        from
2015-09-01 09:40:00.000   2015-09-01 09:50:00.000
2015-09-01 09:15:00.000   2015-09-01 09:25:00.000

(我必须做一些演员才能让你的数字/日期格式适合我)

答案 3 :(得分:0)

我一直在使用一个表User Defined Function,它在请求中占用请求的日期,并且函数内的游标允许填充返回的表,如下所示:

CREATE TABLE sensor (id int not null identity(1,1) primary key, 
    measureDate datetime, sensor nvarchar(10), measure float)

INSERT sensor (measureDate, sensor, measure) VALUES
('2015-09-01 09:10', 'T1', '3.2'), ('2015-09-01 09:15', 'T1', '5.2'),
('2015-09-01 09:20', 'T1', '6.2'), ('2015-09-01 09:25', 'T1', '5.8'),
('2015-09-01 09:30', 'T1', '3.2'), ('2015-09-01 09:35', 'T1', '1.2'),
('2015-09-01 09:40', 'T1', '5.6'), ('2015-09-01 09:45', 'T1', '6.1'),
('2015-09-01 09:50', 'T1', '5.0'), ('2015-09-01 09:55', 'T1', '2.0')
GO
CREATE FUNCTION [dbo].[getTimeSpansBelowMaxTemp] (@measureDate date) 
    RETURNS @timeSpans TABLE (fromTime time, toTime time) AS BEGIN

    DECLARE @measure float, @currentMeasure float = NULL
    DECLARE @measureTime time, @fromMeasureTime time, @toMeasureTime time
    DECLARE yourCursor CURSOR FOR
    SELECT CAST(measureDate AS time), measure 
        FROM sensor
            WHERE CAST(measureDate as date) = @measureDate
    OPEN yourCursor
    FETCH NEXT FROM yourCursor INTO @measureTime, @measure
    WHILE (@@FETCH_STATUS = 0) BEGIN -- Loops on all the measures of the given day
        IF @measure >= 5.0 BEGIN
                IF @currentMeasure IS NULL BEGIN -- Start of a period
                    SET @currentMeasure = @measure
                    SET @fromMeasureTime = @measureTime
                END
                SET @toMeasureTime = @measureTime
        END
        ELSE BEGIN
                IF @currentMeasure IS NOT NULL BEGIN -- End of a period
                    INSERT INTO @timeSpans VALUES (@fromMeasureTime, @toMeasureTime)
                    SET @currentMeasure = NULL
                END
        END 
        FETCH NEXT FROM yourCursor INTO @measureTime, @measure
    END

    CLOSE yourCursor
    DEALLOCATE yourCursor

    RETURN
END
GO

select * from dbo.[getTimeSpansBelowMaxTemp]('2015-09-01')