【发布时间】:2018-07-24 02:22:31
【问题描述】:
我有一张这样的桌子
ts item infoA infoB
2018-02-03 12:00:00 A null null
2018-02-03 12:01:00 null A1 null
2018-02-03 12:02:00 A null null
2018-02-03 12:03:00 null null null
2018-02-03 12:04:00 null A2 null
2018-02-03 12:05:00 null null null
2018-02-03 12:06:00 B null null
2018-02-03 12:07:00 null null B1
2018-02-03 12:08:00 null null null
我只想在相关项目上用时间序列填充空数据
ts item infoA infoB
2018-02-03 12:00:00 A null null
2018-02-03 12:01:00 A A1 null
2018-02-03 12:02:00 A A1 null
2018-02-03 12:03:00 A A1 null
2018-02-03 12:04:00 A A2 null
2018-02-03 12:05:00 A A2 null
2018-02-03 12:06:00 B null null
2018-02-03 12:07:00 B null B1
2018-02-03 12:08:00 B null B1
我发现了一个 AGGREGATE 函数 GapFill() 从 this 使用该功能,我可以从
select t1.ts, t1.item, t2.infoA, t3.infoB
from
(select ts,gapfill(item) OVER (ORDER BY ts)) t1
LEFT JOIN (select ts,gapfill(infoA) OVER (ORDER BY ts) as infoA) on (t1.ts = t2.ts and t1.item='A') t2
LEFT JOIN (select ts,gapfill(infoB) OVER (ORDER BY ts) as infoB) on (t1.ts = t3.ts and t1.item='B') t3
如果我有很多列,如何简化查询。
【问题讨论】:
标签: sql postgresql