Calendar table on Vertica DB
Calendar Table
In this post we will describe how can we create a calendar table on Vertica db using Timeseries analytics.
TIMESERIES Clause
Provides gap-filling and interpolation (GFI) computation, an important component of time series analytics computation. More information you can find on timeseries info.
Our query consists of 3 parts in order to have all the necessary fields.
Explanation
✒ First subquery, contain start & end dates. Between these 2 dates we will fill the gap using TIMESERIES clause. Timeseries will perform an operation on a query result set.
⚠ Then we should specify the length of time unit of time slice computation, which is '1 DAY'. This is the step that we use in order to fill the gap between the sample dates.
OVER() specifies that the timeseries operates on a query result set , that is the rows that are returned after the FROM . The timeseries clause requires an ORDER BY operation on the timestamp column, so we sort the data based on timestamp generated.
✒ Second sub-query, contains all the necessary fields. Here, we use 3 different functions to succeed that:
- TO_CHAR(): Converts date/time and numeric values into text strings.
- It's ideal to take abbreviations for day, month, year.
- DATE_PART(): Extracts a sub-field such as year or hour from a date/time expression.
- WEEK_ISO(): Returns the week of the year for the specified date
✒ Last but not least, we need to create a PK for the table. For this reason we use the analytic function ROW_NUMBER() over(partition by 1). So the operation of the ROW_NUMBER() is taking place of the result of the second query and is put everything into the same partition.
❗❗❗ partition by 1 : is used for syntax integrity. It can be omitted.
👀 The below video shows the result set of the query.
Σχόλια
Δημοσίευση σχολίου