View Source Timescale.Hyperfunctions (Timescale v0.1.1)
Timescale hyperfunctions are a specialized set of functions that allow you to analyze time-series data. You can use hyperfunctions to analyze anything you have stored as time-series data, including IoT devices, IT systems, marketing analytics, user behavior, financial metrics, and cryptocurrency.
approximate-row-count
Approximate Row Count
Timescale offers a hyperfunction, approximate_row_count
, for retrieving the approximate row count of hypertables.
This hyperfunction may be useful for tables with a large number of rows, where the exact number is not needed.
As Ecto does not support raw SQL through the Ecto.Query API, we have chosen
not to directly support approximate_row_count
, for it be an inconsistent API.
If this is a useful feature to your application, you can use the following code:
defmacro approximate_row_count(relation) do
query = "SELECT approximate_row_count($1::TEXT)"
quote do
{:ok, %Postgrex.Result{rows: [[num_rows]]}} =
Ecto.Adapters.SQL.query(MyApp.MyRepo, unquote(query), [unquote(relation)])
num_rows
end
end
Link to this section Summary
Functions
The first aggregate allows you to get the value of one column as ordered by another.
For example, first(temperature, time)
returns the earliest temperature value based on time within an aggregate group.
The histogram function represents the distribution of a set of values as an array of equal-width buckets. It partitions the dataset into a specified number of buckets (nbuckets) ranging from the inputted min and max values.
Fills in missing values by linear interpolation.
The last aggregate allows you to get the value of one column as ordered by another.
For example, last(temperature, time)
returns the latest temperature value based on time within an aggregate group.
Fill in missing values by carrying the last observed value forward.
Allows you to aggregate results into arbitrary time buckets. You can group by seconds, minutes, hours, days
and also weeks.
For example, time_bucket(timestamp, "5 minutes")
would allow you to group results into 5 minute buckets.
Works similar to time_bucket/2
but also activates gap-filling for the interval inferred
by the where clause of the query.
Link to this section Functions
The first aggregate allows you to get the value of one column as ordered by another.
For example, first(temperature, time)
returns the earliest temperature value based on time within an aggregate group.
The histogram function represents the distribution of a set of values as an array of equal-width buckets. It partitions the dataset into a specified number of buckets (nbuckets) ranging from the inputted min and max values.
The return value is an array containing nbuckets+2 buckets, with the middle nbuckets bins for values in the stated range, the first bucket at the head of the array for values under the lower min bound, and the last bucket for values greater than or equal to the max bound. Each bucket is inclusive on its lower bound, and exclusive on its upper bound. Therefore, values equal to the min are included in the bucket starting with min, but values equal to the max are in the last bucket.
Fills in missing values by linear interpolation.
The last aggregate allows you to get the value of one column as ordered by another.
For example, last(temperature, time)
returns the latest temperature value based on time within an aggregate group.
Fill in missing values by carrying the last observed value forward.
Allows you to aggregate results into arbitrary time buckets. You can group by seconds, minutes, hours, days
and also weeks.
For example, time_bucket(timestamp, "5 minutes")
would allow you to group results into 5 minute buckets.
This function also accepts the following optional parameters (passed in as a keyword list):
:offset
- The time interval used to offset all of the timebuckets by. (Postgres type: "INTERVAL"):origin
- The timestamp used to align all of the time buckets. (Postgres type: "TIMESTAMP")
Works similar to time_bucket/2
but also activates gap-filling for the interval inferred
by the where clause of the query.