@@ -48,92 +48,3 @@ The expected output:
48
48
demo000008 | {0 ,0 ,311 ,345 ,116 ,228 ,0 }
49
49
demo000009 | {295 ,92 ,105 ,50 ,8 ,8 ,442 }
50
50
```
51
-
52
- ---
53
-
54
- ## interpolate() <tag type =" community " >Community</tag >
55
-
56
- The ` interpolate ` function does linear interpolation for missing values.
57
- It can only be used in an aggregation query with [ time_bucket_gapfill] ( /hyperfunctions/gapfilling-interpolation/time_bucket_gapfill/ ) .
58
- The ` interpolate ` function call cannot be nested inside other function calls.
59
-
60
- ### Required Arguments
61
-
62
- | Name| Type| Description|
63
- | ---| ---| ---|
64
- | ` value ` | INTEGER | The value to interpolate (int2/int4/int8/float4/float8) |
65
-
66
- ### Optional Arguments
67
-
68
- | Name| Type| Description|
69
- | ---| ---| ---|
70
- | ` prev ` | RECORD | The lookup expression for values before the gapfill time range |
71
- | ` next ` | RECORD | The lookup expression for values after the gapfill time range |
72
-
73
- Because the interpolation function relies on having values before and after
74
- each bucketed period to compute the interpolated value, it might not have
75
- enough data to calculate the interpolation for the first and last time bucket
76
- if those buckets do not otherwise contain valid values.
77
- For example, the interpolation would require looking before this first
78
- time bucket period, yet the query's outer time predicate WHERE time > ...
79
- normally restricts the function to only evaluate values within this time range.
80
- Thus, the ` prev ` and ` next ` expression tell the function how to look for
81
- values outside of the range specified by the time predicate.
82
- These expressions will only be evaluated when no suitable value is returned by the outer query
83
- (i.e., the first and/or last bucket in the queried time range is empty).
84
- The returned record for ` prev ` and ` next ` needs to be a time, value tuple.
85
- The datatype of time needs to be the same as the time datatype in the ` time_bucket_gapfill ` call.
86
- The datatype of value needs to be the same as the ` value ` datatype of the ` interpolate ` call.
87
-
88
- ### Sample Usage
89
-
90
- Get the temperature every day for each device over the last week interpolating for missing readings:
91
- ``` sql
92
- SELECT
93
- time_bucket_gapfill(' 1 day' , time , now() - INTERVAL ' 1 week' , now()) AS day,
94
- device_id,
95
- avg (temperature) AS value,
96
- interpolate(avg (temperature))
97
- FROM metrics
98
- WHERE time > now () - INTERVAL ' 1 week'
99
- GROUP BY day, device_id
100
- ORDER BY day;
101
-
102
- day | device_id | value | interpolate
103
- -- ----------------------+-----------+-------+-------------
104
- 2019 - 01 - 10 01 :00 :00 + 01 | 1 | |
105
- 2019 - 01 - 11 01 :00 :00 + 01 | 1 | 5 .0 | 5 .0
106
- 2019 - 01 - 12 01 :00 :00 + 01 | 1 | | 6 .0
107
- 2019 - 01 - 13 01 :00 :00 + 01 | 1 | 7 .0 | 7 .0
108
- 2019 - 01 - 14 01 :00 :00 + 01 | 1 | | 7 .5
109
- 2019 - 01 - 15 01 :00 :00 + 01 | 1 | 8 .0 | 8 .0
110
- 2019 - 01 - 16 01 :00 :00 + 01 | 1 | 9 .0 | 9 .0
111
- (7 row)
112
- ```
113
-
114
- Get the average temperature every day for each device over the last 7 days interpolating for missing readings with lookup queries for values before and after the gapfill time range:
115
- ``` sql
116
- SELECT
117
- time_bucket_gapfill(' 1 day' , time , now() - INTERVAL ' 1 week' , now()) AS day,
118
- device_id,
119
- avg (value) AS value,
120
- interpolate(avg (temperature),
121
- (SELECT (time ,temperature) FROM metrics m2 WHERE m2 .time < now() - INTERVAL ' 1 week' AND m .device_id = m2 .device_id ORDER BY time DESC LIMIT 1 ),
122
- (SELECT (time ,temperature) FROM metrics m2 WHERE m2 .time > now() AND m .device_id = m2 .device_id ORDER BY time DESC LIMIT 1 )
123
- ) AS interpolate
124
- FROM metrics m
125
- WHERE time > now () - INTERVAL ' 1 week'
126
- GROUP BY day, device_id
127
- ORDER BY day;
128
-
129
- day | device_id | value | interpolate
130
- -- ----------------------+-----------+-------+-------------
131
- 2019 - 01 - 10 01 :00 :00 + 01 | 1 | | 3 .0
132
- 2019 - 01 - 11 01 :00 :00 + 01 | 1 | 5 .0 | 5 .0
133
- 2019 - 01 - 12 01 :00 :00 + 01 | 1 | | 6 .0
134
- 2019 - 01 - 13 01 :00 :00 + 01 | 1 | 7 .0 | 7 .0
135
- 2019 - 01 - 14 01 :00 :00 + 01 | 1 | | 7 .5
136
- 2019 - 01 - 15 01 :00 :00 + 01 | 1 | 8 .0 | 8 .0
137
- 2019 - 01 - 16 01 :00 :00 + 01 | 1 | 9 .0 | 9 .0
138
- (7 row)
139
- ```
0 commit comments