August 17, 2023

Advanced Queries For AWS Timestream

The best time to establish protocols with your clients is when you onboard them.


Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse varius enim in eros elementum tristique. Duis cursus, mi quis viverra ornare, eros dolor interdum nulla, ut commodo diam libero vitae erat. Aenean faucibus nibh et justo cursus id rutrum lorem imperdiet. Nunc ut sem vitae risus tristique posuere.

Welcome back to the next instalment of our Timestream journey! In our previous blog, we explored the fundamentals of Timestream, Amazon Web Services’ purpose-built time series database. We covered its key concepts, data model, and basic techniques for inserting data in Timestream, laying a solid foundation for harnessing the potential of this powerful service.

In this continuation, we delve deeper into Timestream’s capabilities, focusing on advanced queries and analytics. With these advanced techniques at your disposal, you’ll be able to extract valuable insights, perform complex calculations, and uncover patterns within your time series data.

Throughout this blog, we will embark on an exploration of the advanced querying features of Timestream, empowering you to take full advantage of its analytical prowess. We’ll uncover the hidden potential of window functions, harness the power of time series aggregations, dive into the world of pattern matching and regular expressions, and much more.

Window Function

Window functions in Timestream give you extensive analytical capabilities by allowing you to execute computations and aggregations on a specific window or subset of your time series data. It lets you to do computations on the values within a sliding window that traverses through your data. Window functions assist calculations that incorporate data point ordering by working on a set of rows determined by the window frame, which can be based on time intervals, row locations, or a mix of both. This feature is valuable for trend analysis, generating moving averages, finding peaks and valleys, and detecting abnormalities in time series data.

Key Components For Window Function:

Partition By: As the name suggest it determines how the data divided into specific group or attributes window function, such as pageName, clientX, clientY from our previous example. Each partitions are processed independently and window function is applied within each partition.

Order By: This clause is used to order the rows within the partitions or in other words it can used to query the partitions in logical order such as based on timestamp.

Window Frame or Interval: It is determined by the frame specification, which can be defined in terms of a range of rows or a time interval. The frame specification can include the current row, a number of preceding rows, a number of following rows, or a combination of these.

Aggregate Functions: As we know AWS Timestream provides a wide variety of aggregate functions like SUM, AVG, MIN, MAX, COUNT, etc. These can be used to compute a single result based on the values which are in specified window frame.

Lets create a window frame query for our table to find out how many users visited the page.

In the above query, the count(*) function is used as a window function to calculate the visit count within a window frame of 1 hour preceding the current row. The data is partitioned by the dimensions clientX, clientY, and pageName, ensuring that the visit count is calculated independently for each combination of those dimensions.

This query will retrieve the time, clentX, clientY, pageName, and the visit count for each row in the Timestream table, considering a sliding window of 1 hour preceding the current row.

Pattern Matching and Regular Expressions:

Yes! You can do pattern matching and regular expression in the queries. It is a powerful way to filter and find a specific patterns within our time series data, this can be mainly used to understand the textual patterns in the data.

Key Components of Regular Expression

LIKE: This can be used to match patterns based on the wildcard characters. The wildcard characters used in ‘LIKE’ are

  • % (percent sign): Matches any sequence of characters (including zero characters).
  • _ (underscore): Matches any single character.

Above query will fetch all the page name which starts with ‘product’.

RLIKE: This can be used to do pattern matching, it can be more flexible and powerful way of defining a complex pattern. You can use regular expression syntax to match any specific string or pattern in our data.

Using the above query we can fetch all the page name which contains ‘product’ and their clientX, clientY are of three digits.

Statistical Functions

Statistical functions can be combined to get summarisation of time series data by calculating various statistical measures. It also allow’s you to gain insights into the distribution, trends, and characteristics of data. Some commonly used statistical functions are MIN, MAX, COUNT, AVG, SUM, STDDEV (variability of the data points around the mean), VARIANCE (average squared deviation from the mean)

Let’s see an example query where we can analyze the statistical characteristics of user clicks that match specific conditions related to pageName and clientX, clientY ranges.


With this continuation of our Timestream exploration, we are equipped with the tools and knowledge to leverage advanced querying techniques and unlock the full potential of Timestream’s analytical capabilities. By mastering window functions, time series aggregations, pattern matching, and more, we will be empowered to extract meaningful insights, make informed decisions, and derive maximum value from your time series data.

August 17, 2023
min read
Subscribe to our newsletter
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.
Share this article:

More articles