在SQL Server中计算日期时间字符串列表中的最小值,最大值和平均值

时间:2017-03-22 12:23:51

标签: sql-server-2012 sql-server-2008-r2

Insert Into Heartbeat values
( 1 ,'3/20/2017 07:05'),
( 1 ,'3/20/2017 07:15'),
( 1 ,'3/20/2017 07:35'),
( 1 ,'3/20/2017 07:55'),
( 2 ,'3/20/2017 07:11'),
( 2 ,'3/20/2017 07:19'),
( 2 ,'3/20/2017 07:45'),
( 2 ,'3/20/2017 07:58')

;with cte as (
        Select SensorID
              ,MinDT        = Min(DatetimeInformation)
              ,MaxDT        = Max(DatetimeInformation)
        From  @YourTable
        Group By SensorID
                ,Convert(date,DatetimeInformation)
                ,DatePart(HOUR,DatetimeInformation)
)
Select SensorID
      ,Date  = Convert(date,MinDT)
      ,TimeDuration = Format(MinDT,'htt')+ ' - ' + Format(DateAdd(HOUR,1,minDT),'htt')
      ,DateTimeInformationList = Stuff((Select ', ' +Format(DatetimeInformation,'M/dd/yyyy h:mm') 
                                         From  HeartBeat
                                         Where SensorID=A.SensorID 
                                           and DatetimeInformation between A.MinDT and A.MaxDT  
                                         Order By DatetimeInformation 
                                         For XML Path ('')
                                       ),1,2,'')
 from  cte A

返回

Date         TimeDuration   DateTimeInformationList
1           2017-03-20   7AM - 8AM      3/20/2017 7:05, 3/20/2017 7:15, 3/20/2017 7:35, 3/20/2017 7:55
2           2017-03-20   7AM - 8AM      3/20/2017 7:11, 3/20/2017 7:19, 3/20/2017 7:45, 3/20/2017 7:58

我能够获得帮助,将每小时的Datetimeinformation分组,这是在查询之上。 我需要得到该TimeDuration(早上7点到早上8点)的所有DatetimeInformationList的平均时间间隔(分钟或秒)

1 个答案:

答案 0 :(得分:0)

以下是对您正在寻找的CTE的修改。

;with cte as (
    Select h1.SensorID
        ,MinDT        = Min(h1.DatetimeInformation)
        ,MaxDT        = Max(h2.DatetimeInformation)
        ,AvgInterval    = Avg(datediff(second, h1.DateTimeInformation, h2.DateTimeInformation))
    From  #HeartBeat h1
    INNER JOIN #Heartbeat h2
        ON h1.SensorID = h2.SensorID
    WHERE h2.DateTimeInformation = (
        SELECT min(dateTimeInformation)
        FROM #Heartbeat h3
        WHERE h3.SensorID = h1.SensorID
            AND h3.DateTimeInformation > h1.DateTimeInformation
        )
    Group By h1.SensorID
            ,Convert(date,h1.DatetimeInformation)
            ,DatePart(HOUR,h1.DatetimeInformation)
)
Select A.SensorID
    ,Date  = Convert(date,MinDT)
    ,TimeDuration = Format(MinDT,'htt')+ ' - ' + Format(DateAdd(HOUR,1,minDT),'htt')
    ,DateTimeInformationList = Stuff((Select ', ' +Format(DatetimeInformation,'M/dd/yyyy h:mm') 
                                    From  #HeartBeat
                                    Where SensorID=A.SensorID 
                                        and DatetimeInformation between A.MinDT and A.MaxDT  
                                    Order By DatetimeInformation 
                                    For XML Path ('')
                                ),1,2,'')
    ,AverageInterval = Cast(Cast(AvgInterval / 60 as int) As varchar(2)) + ':' + Cast(AvgInterval % 60 AS varchar(2)) 

来自cte A

输出:

SensorID    Date    TimeDuration    DateTimeInformationList                                     AverageInterval
1       2017-03-20  7AM - 8AM   3/20/2017 7:05, 3/20/2017 7:15, 3/20/2017 7:35, 3/20/2017 7:55  16:40
2       2017-03-20  7AM - 8AM   3/20/2017 7:11, 3/20/2017 7:19, 3/20/2017 7:45, 3/20/2017 7:58  15:40