SQL - 时间序列 - 对于缺失值返回 0

标签 sql sql-server azure time-series

我有一个名为 MY_TABLE(例如)的 MS/Azure SQL 表,其中包含两列:

  • datetime : INT = UNIX 纪元(测量的日期和时间)
  • val : DECIMAL(测量值)

此表表示时间序列,通常分辨率为 1 分钟。数据中可能存在一些差距/缺失值。 SQL 数据库链接到 Django Web 应用程序,其中时间序列将显示在图表上。

我使用以下查询来检索两个日期之间 1 小时分辨率的聚合数据:

    WITH time_table as (
        SELECT cast(dateadd(second, datetime, '19700101') as DATETIME) as calendar_date,val,datetime as epoch
        FROM MY_TABLE
        WHERE datetime>=1451628000 and datetime<1452755200
    )
    SELECT min(epoch),avg(val) 
    FROM time_table
    GROUP BY YEAR(calendar_date),MONTH(calendar_date),DAY(calendar_date),datepart(hour,calendar_date)
    ORDER BY min(calendar_date) ASC

此查询返回 1 小时时间段内聚合的平均值。

问题

  1. 即使表中没有相应的数据,如何在所有时间步骤将此查询修改为值 (0)。更清楚地说,我希望结果是一个连续的时间序列,没有任何时间间隙

我可以使用Python/Pandas轻松执行此操作,但我觉得这不会被优化

最佳答案

您可以使用纪元形式和日期时间形式生成小时表:

create table dbo.Hours (
   EpochStart bigint not null primary key clustered
 , EpochEnd   bigint not null
 , Datetime_Hour datetime not null
 , Datetime_NextHour datetime not null
);

declare @fromdate datetime = '20160101';
declare @thrudate datetime = '20161231';

;with n as (select n from (values(0),(1),(2),(3),(4),(5),(6),(7),(8),(9)) t(n))
, Hours as (
  select top ((datediff(day, @fromdate,@thrudate)+1)*24)
      [Datetime_Hour]=convert(datetime,dateadd(hour,row_number() over(order by (select 1))-1,@fromdate))
     ,[Datetime_NextHour]=convert(datetime,dateadd(hour,row_number() over(order by (select 1)),@fromdate))
  from n as deka cross join n as hecto cross join n as kilo 
                cross join n as tenK cross join n as hundredK
   order by 1
)
insert into dbo.Hours
select
   EpochStart = datediff(second,'19700101',Datetime_Hour)
 , EpochEnd   = datediff(second,'19700101',Datetime_NextHour)
 , Datetime_Hour
 , Datetime_NextHour
from Hours;

然后在查询中使用它,如下所示:

select 
   h.EpochStart
 , avg(val)
from dbo.Hours h
  left join MY_TABLE t
    on t.datetime >= h.EpochStart
   and t.datetime <  h.EpochEnd
where h.EpochStart >= 1451628000
  and h.EpochStart <  1452755200
group by h.EpochStart

rextester 演示:http://rextester.com/INZW16141

关于SQL - 时间序列 - 对于缺失值返回 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43077388/

相关文章:

c# - 部署后 Appsetting.json 无法与 Azure 函数应用一起使用

mysql - 对 MySQL 中 SUM 创建的总分进行排名

mysql - 为什么 MariaDBb 允许将文本插入 smallint

sql - 与 SQL 中的 IFERROR 等效的功能?

c# - 加速 SqlDataSource 的技巧?

azure - 我可以在 azure 函数内执行多少个任务?

sql - 按加入分组并计算组的最大值

MySQL:多个 SUMS 计算组合值 WHERE 条件

sql - 如何在 UPDATE 语句的集合列表中使用聚合

caching - Azure创建区域 "This operation is not supported by the cache."