SQL中的回写timeseries数据

SQL中的回写timeseries数据,sql,time-series,vertica,Sql,Time Series,Vertica,我在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 -------------------------

我在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的所有可能值。

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

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;

平板电脑、src、val上的索引可能有助于Vertica以外的数据库中的子查询。

你说得对。我更新了示例。Vertica不使用索引。