【问题标题】:Back fill timeseries data in SQLSQL中的回填时间序列数据
【发布时间】:2015-07-06 01:25:01
【问题描述】:

我的 SQL (Vertica) 数据库表中的数据如下所示...

      ts  src val
---------------------------------
10:25:10    C  72
10:25:09    A  13
10:25:08    A  99
10:25:05    B  22
10:25:02    C  71

我需要将它“旋转”到列中,并像这样用src 列回填最后一个已知值。

      ts a_val b_val c_val
----------------------------
10:25:10    13    22    72
10:25:09    13    22    71
10:25:08    99    22    71
10:25:05  null    22    71
10:25:02  null  null    71

我提前知道src 的所有可能值。

【问题讨论】:

  • 你是对的。我更新了示例。

标签: sql time-series vertica


【解决方案1】:

可能最简单的方法是使用相关子查询。这不一定有最好的性能:

select t.ts,
       (select t2.val from table t2 where t2.ts <= t.ts and t2.src = 'a' order by t2.ts desc) as val_a,
       (select t2.val from table t2 where t2.ts <= t.ts and t2.src = 'b' order by t2.ts desc) as val_b,
       (select t2.val from table t2 where t2.ts <= t.ts and t2.src = 'c' order by t2.ts desc) as val_c
from table t;

table(ts, src, val) 上的索引可能有助于 Vertica 以外的数据库中的子查询。

【讨论】:

  • Vertica 不使用索引。
猜你喜欢
  • 1970-01-01
  • 2018-08-13
  • 2020-03-19
  • 1970-01-01
  • 2020-05-31
  • 1970-01-01
  • 2018-07-24
  • 1970-01-01
  • 2019-10-01
相关资源
最近更新 更多