

For similar capabilities to Amazon Timestream for LiveAnalytics, consider Amazon Timestream for InfluxDB. It offers simplified data ingestion and single-digit millisecond query response times for real-time analytics. Learn more [here](https://docs.aws.amazon.com//timestream/latest/developerguide/timestream-for-influxdb.html).

# Query language reference
<a name="reference"></a>

**Note**  
This query language reference includes the following third-party documentation from the [Trino Software Foundation](https://trino.io/foundation.html) (formerly Presto Software Foundation), which is licensed under the Apache License, Version 2.0. You may not use this file except in compliance with this license. To get a copy of the Apache License, Version 2.0, see the [Apache website](http://www.apache.org/licenses/LICENSE-2.0).

Timestream for LiveAnalytics supports a rich query language for working with your data. You can see the available data types, operators, functions and constructs below.

You can also get started right away with Timestream's query language in the [Sample queries](sample-queries.md) section.

**Topics**
+ [Supported data types](supported-data-types.md)
+ [Built-in time series functionality](timeseries-specific-constructs.md)
+ [SQL support](supported-sql-constructs.md)
+ [Logical operators](logical-operators.md)
+ [Comparison operators](comparison-operators.md)
+ [Comparison functions](comparison-functions.md)
+ [Conditional expressions](conditional-expressions.md)
+ [Conversion functions](conversion-functions.md)
+ [Mathematical operators](mathematical-operators.md)
+ [Mathematical functions](mathematical-functions.md)
+ [String operators](string-operators.md)
+ [String functions](string-functions.md)
+ [Array operators](array-operators.md)
+ [Array functions](array-functions.md)
+ [Bitwise functions](bitwise-functions.md)
+ [Regular expression functions](regex-functions.md)
+ [Date / time operators](date-time-operators.md)
+ [Date / time functions](date-time-functions.md)
+ [Aggregate functions](aggregate-functions.md)
+ [Window functions](window-functions.md)
+ [Sample queries](sample-queries.md)

# Supported data types
<a name="supported-data-types"></a>

Timestream for LiveAnalytics's query language supports the following data types.

**Note**  
Data types supported for writes are described in [Data types](https://docs.aws.amazon.com/timestream/latest/developerguide/writes.html#writes.data-types).


| Data type | Description | 
| --- | --- | 
|  `int`  |  Represents a 32-bit integer.  | 
|  `bigint`  |  Represents a 64-bit signed integer.  | 
|  `boolean`  |  One of the two truth values of logic, `True` and `False`.  | 
|  `double`  |  Represents a 64-bit variable-precision data type. Implements [IEEE Standard 754 for Binary Floating-Point Arithmetic](https://standards.ieee.org/standard/754-2019.html).  The query language is for reading data. There are functions for `Infinity` and `NaN` double values which can be used in queries. But you cannot write those values to Timestream.   | 
|  `varchar`  |  Variable length character data with a maximum size of 2KB.  | 
|  `array[T,...]`  |  Contains one or more elements of a specified data type *T*, where *T* can be any of the data types supported in Timestream.  | 
|   `row(T,...)`   |  Contains one or more named fields of data type *T*. The fields may be of any data type supported by Timestream, and are accessed with the dot field reference operator: <pre>.</pre>  | 
|  `date`  |  Represents a date in the form `YYYY-MM-DD`. where *YYYY* is the year, *MM* is the month, and *DD* is the day, respectively. The supported range is from `1970-01-01` to `2262-04-11`.   *Example:*  <pre>1971-02-03</pre>  | 
|  `time`  |  Represents the time of day in [UTC](https://en.wikipedia.org/wiki/Coordinated_Universal_Time). The `time` datatype is represented in the form `HH.MM.SS.sssssssss.` Supports nanosecond precision.   *Example:*  <pre>17:02:07.496000000</pre>  | 
|  `timestamp`  |  Represents an instance in time using nanosecond precision time in UTC. `YYYY-MM-DD hh:mm:ss.sssssssss` Query supports timestamps in the range `1677-09-21 00:12:44.000000000` to `2262-04-11 23:47:16.854775807`.  | 
|  `interval`  |  Represents an interval of time as a string literal `Xt`, composed of two parts, *X* and *t*.  *X* is an numeric value greater than or equal to `0`, and *t* is a unit of time like second or hour. The unit is not pluralized. The unit of time *t* is must be one of the following string literals:  [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/timestream/latest/developerguide/supported-data-types.html)  *Examples:*  <pre>17s</pre> <pre>12second</pre> <pre>21hour</pre> <pre>2d</pre>  | 
|  `timeseries[row(timestamp, T,...)]`  |  Represents the values of a measure recorded over a time interval as an `array` composed of `row` objects. Each `row` contains a `timestamp` and one or more measure values of data type *T*, where *T* can be any one of `bigint`, `boolean`, `double`, or `varchar`. Rows are assorted in ascending order by `timestamp`. The *timeseries* datatype represents the values of a measure over time.  | 
|  `unknown`  |  Represents null data.  | 

# Built-in time series functionality
<a name="timeseries-specific-constructs"></a>

Timestream for LiveAnalytics provides built-in time series functionality that treat time series data as a first class concept.

Built-in time series functionality can be divided into two categories: views and functions.

You can read about each construct below.

**Topics**
+ [Timeseries views](timeseries-specific-constructs.views.md)
+ [Time series functions](timeseries-specific-constructs.functions.md)

# Timeseries views
<a name="timeseries-specific-constructs.views"></a>

Timestream for LiveAnalytics supports the following functions for transforming your data to the `timeseries` data type:

**Topics**
+ [CREATE\$1TIME\$1SERIES](#timeseries-specific-constructs.views.CREATE_TIME_SERIES)
+ [UNNEST](#timeseries-specific-constructs.views.UNNEST)

## CREATE\$1TIME\$1SERIES
<a name="timeseries-specific-constructs.views.CREATE_TIME_SERIES"></a>

 **CREATE\$1TIME\$1SERIES** is an aggregation function that takes all the raw measurements of a time series (time and measure values) and returns a timeseries data type. The syntax of this function is as follows: 

```
CREATE_TIME_SERIES(time, measure_value::<data_type>)
```

 where `<data_type>` is the data type of the measure value and can be one of bigint, boolean, double, or varchar. The second parameter cannot be null.

Consider the CPU utilization of EC2 instances stored in a table named **metrics** as shown below:


| Time | region | az | vpc | instance\$1id | measure\$1name | measure\$1value::double | 
| --- | --- | --- | --- | --- | --- | --- | 
|  2019-12-04 19:00:00.000000000  |  us-east-1  |  us-east-1d  |  vpc-1a2b3c4d  |  i-1234567890abcdef0  |  cpu\$1utilization  |  35.0  | 
|  2019-12-04 19:00:01.000000000  |  us-east-1  |  us-east-1d  |  vpc-1a2b3c4d  |  i-1234567890abcdef0  |  cpu\$1utilization  |  38.2  | 
|  2019-12-04 19:00:02.000000000  |  us-east-1  |  us-east-1d  |  vpc-1a2b3c4d  |  i-1234567890abcdef0  |  cpu\$1utilization  |  45.3  | 
|  2019-12-04 19:00:00.000000000  |  us-east-1  |  us-east-1d  |  vpc-1a2b3c4d  |  i-1234567890abcdef1  |  cpu\$1utilization  |  54.1  | 
|  2019-12-04 19:00:01.000000000  |  us-east-1  |  us-east-1d  |  vpc-1a2b3c4d  |  i-1234567890abcdef1  |  cpu\$1utilization  |  42.5  | 
|  2019-12-04 19:00:02.000000000  |  us-east-1  |  us-east-1d  |  vpc-1a2b3c4d  |  i-1234567890abcdef1  |  cpu\$1utilization  |  33.7  | 

Running the query:

```
SELECT region, az, vpc, instance_id, CREATE_TIME_SERIES(time, measure_value::double) as cpu_utilization FROM metrics
    WHERE measure_name=’cpu_utilization’
    GROUP BY region, az, vpc, instance_id
```

will return all series that have `cpu_utilization` as a measure value. In this case, we have two series: 


| region | az | vpc | instance\$1id | cpu\$1utilization | 
| --- | --- | --- | --- | --- | 
|  us-east-1  |  us-east-1d  |  vpc-1a2b3c4d  |  i-1234567890abcdef0  |  [\$1time: 2019-12-04 19:00:00.000000000, measure\$1value::double: 35.0\$1, \$1time: 2019-12-04 19:00:01.000000000, measure\$1value::double: 38.2\$1, \$1time: 2019-12-04 19:00:02.000000000, measure\$1value::double: 45.3\$1]  | 
|  us-east-1  |  us-east-1d  |  vpc-1a2b3c4d  |  i-1234567890abcdef1  |  [\$1time: 2019-12-04 19:00:00.000000000, measure\$1value::double: 35.1\$1, \$1time: 2019-12-04 19:00:01.000000000, measure\$1value::double: 38.5\$1, \$1time: 2019-12-04 19:00:02.000000000, measure\$1value::double: 45.7\$1]  | 

## UNNEST
<a name="timeseries-specific-constructs.views.UNNEST"></a>

 `UNNEST` is a table function that enables you to transform `timeseries` data into the flat model. The syntax is as follows: 

 `UNNEST` transforms a `timeseries` into two columns, namely, `time` and `value`. You can also use aliases with UNNEST as shown below: 

```
UNNEST(timeseries) AS <alias_name> (time_alias, value_alias)
```

where `<alias_name>` is the alias for the flat table, `time_alias` is the alias for the `time` column and `value_alias` is the alias for the `value` column.

For example, consider the scenario where some of the EC2 instances in your fleet are configured to emit metrics at a 5 second interval, others emit metrics at a 15 second interval, and you need the average metrics for all instances at a 10 second granularity for the past 6 hours. To get this data, you transform your metrics to the time series model using **CREATE\$1TIME\$1SERIES**. You can then use **INTERPOLATE\$1LINEAR** to get the missing values at 10 second granularity. Next, you transform the data back to the flat model using **UNNEST**, and then use **AVG** to get the average metrics across all instances.

```
WITH interpolated_timeseries AS (
    SELECT region, az, vpc, instance_id,
        INTERPOLATE_LINEAR(
            CREATE_TIME_SERIES(time, measure_value::double),
                SEQUENCE(ago(6h), now(), 10s)) AS interpolated_cpu_utilization
    FROM timestreamdb.metrics 
    WHERE measure_name= ‘cpu_utilization’ AND time >= ago(6h)
    GROUP BY region, az, vpc, instance_id
)
SELECT region, az, vpc, instance_id, avg(t.cpu_util)
FROM interpolated_timeseries
CROSS JOIN UNNEST(interpolated_cpu_utilization) AS t (time, cpu_util)
GROUP BY region, az, vpc, instance_id
```

 The query above demonstrates the use of **UNNEST** with an alias. Below is an example of the same query without using an alias for **UNNEST**: 

```
WITH interpolated_timeseries AS (
    SELECT region, az, vpc, instance_id,
        INTERPOLATE_LINEAR(
            CREATE_TIME_SERIES(time, measure_value::double),
                SEQUENCE(ago(6h), now(), 10s)) AS interpolated_cpu_utilization
    FROM timestreamdb.metrics 
    WHERE measure_name= ‘cpu_utilization’ AND time >= ago(6h)
    GROUP BY region, az, vpc, instance_id
)
SELECT region, az, vpc, instance_id, avg(value)
FROM interpolated_timeseries
CROSS JOIN UNNEST(interpolated_cpu_utilization)
GROUP BY region, az, vpc, instance_id
```

# Time series functions
<a name="timeseries-specific-constructs.functions"></a>

Amazon Timestream for LiveAnalytics supports timeseries functions, such as derivatives, integrals, and correlations, as well as others, to derive deeper insights from your time series data. This section provides usage information for each of these functions, as well as sample queries. Select a topic below to learn more. 

**Topics**
+ [Interpolation functions](timeseries-specific-constructs.functions.interpolation.md)
+ [Derivatives functions](timeseries-specific-constructs.functions.derivatives.md)
+ [Integral functions](timeseries-specific-constructs.functions.integrals.md)
+ [Correlation functions](timeseries-specific-constructs.functions.correlation.md)
+ [Filter and reduce functions](timeseries-specific-constructs.functions.filter-reduce.md)

# Interpolation functions
<a name="timeseries-specific-constructs.functions.interpolation"></a>

If your time series data is missing values for events at certain points in time, you can estimate the values of those missing events using interpolation. Amazon Timestream supports four variants of interpolation: linear interpolation, cubic spline interpolation, last observation carried forward (locf) interpolation, and constant interpolation. This section provides usage information for the Timestream for LiveAnalytics interpolation functions, as well as sample queries. 



## Usage information
<a name="w2aab7c59c13c13c11b7"></a>


| Function | Output data type | Description | 
| --- | --- | --- | 
|  `interpolate_linear(timeseries, array[timestamp])`  |  timeseries  |  Fills in missing data using [linear interpolation](https://wikipedia.org/wiki/Linear_interpolation).  | 
|  `interpolate_linear(timeseries, timestamp)`  |  double  |  Fills in missing data using [linear interpolation](https://wikipedia.org/wiki/Linear_interpolation).  | 
|  `interpolate_spline_cubic(timeseries, array[timestamp])`  |  timeseries  |  Fills in missing data using [cubic spline interpolation](https://wikiversity.org/wiki/Cubic_Spline_Interpolation#:~:text=Cubic%20spline%20interpolation%20is%20a,Lagrange%20polynomial%20and%20Newton%20polynomial.).  | 
|  `interpolate_spline_cubic(timeseries, timestamp)`  |  double  |  Fills in missing data using [cubic spline interpolation](https://wikiversity.org/wiki/Cubic_Spline_Interpolation#:~:text=Cubic%20spline%20interpolation%20is%20a,Lagrange%20polynomial%20and%20Newton%20polynomial.).  | 
|  `interpolate_locf(timeseries, array[timestamp])`  |  timeseries  |  Fills in missing data using the last sampled value.  | 
|  `interpolate_locf(timeseries, timestamp)`  |  double  |  Fills in missing data using the last sampled value.  | 
|  `interpolate_fill(timeseries, array[timestamp], double)`  |  timeseries  |  Fills in missing data using a constant value.  | 
|  `interpolate_fill(timeseries, timestamp, double)`  |  double  |  Fills in missing data using a constant value.  | 

## Query examples
<a name="w2aab7c59c13c13c11b9"></a>

**Example**  
Find the average CPU utilization binned at 30 second intervals for a specific EC2 host over the past 2 hours, filling in the missing values using linear interpolation:  

```
WITH binned_timeseries AS (
SELECT hostname, BIN(time, 30s) AS binned_timestamp, ROUND(AVG(measure_value::double), 2) AS avg_cpu_utilization
FROM "sampleDB".DevOps
WHERE measure_name = 'cpu_utilization'
    AND hostname = 'host-Hovjv'
    AND time > ago(2h)
GROUP BY hostname, BIN(time, 30s)
), interpolated_timeseries AS (
SELECT hostname,
    INTERPOLATE_LINEAR(
        CREATE_TIME_SERIES(binned_timestamp, avg_cpu_utilization),
            SEQUENCE(min(binned_timestamp), max(binned_timestamp), 15s)) AS interpolated_avg_cpu_utilization
FROM binned_timeseries
GROUP BY hostname
)
SELECT time, ROUND(value, 2) AS interpolated_cpu
FROM interpolated_timeseries
CROSS JOIN UNNEST(interpolated_avg_cpu_utilization)
```

**Example**  
Find the average CPU utilization binned at 30 second intervals for a specific EC2 host over the past 2 hours, filling in the missing values using interpolation based on the last observation carried forward:  

```
WITH binned_timeseries AS (
SELECT hostname, BIN(time, 30s) AS binned_timestamp, ROUND(AVG(measure_value::double), 2) AS avg_cpu_utilization
FROM "sampleDB".DevOps
WHERE measure_name = 'cpu_utilization'
    AND hostname = 'host-Hovjv'
    AND time > ago(2h)
GROUP BY hostname, BIN(time, 30s)
), interpolated_timeseries AS (
SELECT hostname,
    INTERPOLATE_LOCF(
        CREATE_TIME_SERIES(binned_timestamp, avg_cpu_utilization),
            SEQUENCE(min(binned_timestamp), max(binned_timestamp), 15s)) AS interpolated_avg_cpu_utilization
FROM binned_timeseries
GROUP BY hostname
)
SELECT time, ROUND(value, 2) AS interpolated_cpu
FROM interpolated_timeseries
CROSS JOIN UNNEST(interpolated_avg_cpu_utilization)
```

# Derivatives functions
<a name="timeseries-specific-constructs.functions.derivatives"></a>

Derivatives are used calculate the rate of change for a given metric and can be used to proactively respond to an event. For example, suppose you calculate the derivative of the CPU utilization of EC2 instances over the past 5 minutes, and you notice a significant positive derivative. This can be indicative of increased demand on your workload, so you may decide want to spin up more EC2 instances to better handle your workload. 

Amazon Timestream supports two variants of derivative functions. This section provides usage information for the Timestream for LiveAnalytics derivative functions, as well as sample queries. 



## Usage information
<a name="w2aab7c59c13c13c13b9"></a>


| Function | Output data type | Description | 
| --- | --- | --- | 
|  `derivative_linear(timeseries, interval)`  |  timeseries  |  Calculates the [derivative](https://wikipedia.org/wiki/Derivative) of each point in the `timeseries` for the specified `interval`.  | 
|  `non_negative_derivative_linear(timeseries, interval)`  |  timeseries  |  Same as `derivative_linear(timeseries, interval)`, but only returns positive values.  | 

## Query examples
<a name="w2aab7c59c13c13c13c11"></a>

**Example**  
Find the rate of change in the CPU utilization every 5 minutes over the past 1 hour:  

```
SELECT DERIVATIVE_LINEAR(CREATE_TIME_SERIES(time, measure_value::double), 5m) AS result 
FROM “sampleDB”.DevOps 
WHERE measure_name = 'cpu_utilization' 
AND hostname = 'host-Hovjv' and time > ago(1h) 
GROUP BY hostname, measure_name
```

**Example**  
Calculate the rate of increase in errors generated by one or more microservices:  

```
WITH binned_view as (
    SELECT bin(time, 5m) as binned_timestamp, ROUND(AVG(measure_value::double), 2) as value            
    FROM “sampleDB”.DevOps  
    WHERE micro_service = 'jwt'  
    AND time > ago(1h) 
    AND measure_name = 'service_error'
    GROUP BY bin(time, 5m)
)
SELECT non_negative_derivative_linear(CREATE_TIME_SERIES(binned_timestamp, value), 1m) as rateOfErrorIncrease
FROM binned_view
```

# Integral functions
<a name="timeseries-specific-constructs.functions.integrals"></a>

You can use integrals to find the area under the curve per unit of time for your time series events. As an example, suppose you're tracking the volume of requests received by your application per unit of time. In this scenario, you can use the integral function to determine the total volume of requests served per specified interval over a specific time period.

Amazon Timestream supports one variant of integral functions. This section provides usage information for the Timestream for LiveAnalytics integral function, as well as sample queries. 



## Usage information
<a name="w2aab7c59c13c13c15b9"></a>


| Function | Output data type | Description | 
| --- | --- | --- | 
|  `integral_trapezoidal(timeseries(double))` `integral_trapezoidal(timeseries(double), interval day to second)` `integral_trapezoidal(timeseries(bigint))` `integral_trapezoidal(timeseries(bigint), interval day to second)` `integral_trapezoidal(timeseries(integer), interval day to second)` `integral_trapezoidal(timeseries(integer))`  |  double  |  Approximates the [integral](https://wikipedia.org/wiki/Integral) per the specified `interval day to second` for the `timeseries` provided, using the [trapezoidal rule](https://wikipedia.org/wiki/Trapezoidal_rule). The interval day to second parameter is optional and the default is `1s`. For more information about intervals, see [Interval and duration](date-time-functions.md#date-time-functions-interval-duration).  | 

## Query examples
<a name="w2aab7c59c13c13c15c11"></a>

**Example**  
Calculate the total volume of requests served per five minutes over the past hour by a specific host:  

```
SELECT INTEGRAL_TRAPEZOIDAL(CREATE_TIME_SERIES(time, measure_value::double), 5m) AS result FROM sample.DevOps 
WHERE measure_name = 'request' 
AND hostname = 'host-Hovjv' 
AND time > ago (1h) 
GROUP BY hostname, measure_name
```

# Correlation functions
<a name="timeseries-specific-constructs.functions.correlation"></a>

Given two similar length time series, correlation functions provide a correlation coefficient, which explains how the two time series trend over time. The correlation coefficient ranges from `-1.0` to `1.0`. `-1.0` indicates that the two time series trend in opposite directions at the same rate. whereas `1.0` indicates that the two timeseries trend in the same direction at the same rate. A value of `0` indicates no correlation between the two time series. For example, if the price of oil increases, and the stock price of an oil company increases, the trend of the price increase of oil and the price increase of the oil company will have a positive correlation coefficient. A high positive correlation coefficient would indicate that the two prices trend at a similar rate. Similarly, the correlation coefficient between bond prices and bond yields is negative, indicating that these two values trends in the opposite direction over time.

Amazon Timestream supports two variants of correlation functions. This section provides usage information for the Timestream for LiveAnalytics correlation functions, as well as sample queries. 



## Usage information
<a name="w2aab7c59c13c13c19c11"></a>


| Function | Output data type | Description | 
| --- | --- | --- | 
|  `correlate_pearson(timeseries, timeseries)`  |  double  |  Calculates [Pearson's correlation coefficient](https://wikipedia.org/wiki/Pearson_correlation_coefficient) for the two `timeseries`. The timeseries must have the same timestamps.  | 
|  `correlate_spearman(timeseries, timeseries)`  |  double  |  Calculates [Spearman's correlation coefficient](https://en.wikipedia.org/wiki/Spearman%27s_rank_correlation_coefficient) for the two `timeseries`. The timeseries must have the same timestamps.  | 

## Query examples
<a name="w2aab7c59c13c13c19c13"></a>

**Example**  

```
WITH cte_1 AS (
    SELECT INTERPOLATE_LINEAR(
        CREATE_TIME_SERIES(time, measure_value::double), 
        SEQUENCE(min(time), max(time), 10m)) AS result 
    FROM sample.DevOps 
    WHERE measure_name = 'cpu_utilization' 
    AND hostname = 'host-Hovjv' AND time > ago(1h) 
    GROUP BY hostname, measure_name
), 
cte_2 AS (
    SELECT INTERPOLATE_LINEAR(
        CREATE_TIME_SERIES(time, measure_value::double), 
        SEQUENCE(min(time), max(time), 10m)) AS result 
    FROM sample.DevOps 
    WHERE measure_name = 'cpu_utilization' 
    AND hostname = 'host-Hovjv' AND time > ago(1h) 
    GROUP BY hostname, measure_name
) 
SELECT correlate_pearson(cte_1.result, cte_2.result) AS result 
FROM cte_1, cte_2
```

# Filter and reduce functions
<a name="timeseries-specific-constructs.functions.filter-reduce"></a>

Amazon Timestream supports functions for performing filter and reduce operations on time series data. This section provides usage information for the Timestream for LiveAnalytics filter and reduce functions, as well as sample queries. 



## Usage information
<a name="w2aab7c59c13c13c23b7"></a>


| Function | Output data type | Description | 
| --- | --- | --- | 
|  `filter(timeseries(T), function(T, Boolean))`  |  timeseries(T)  |  Constructs a time series from an the input time series, using values for which the passed `function` returns `true`.  | 
|  `reduce(timeseries(T), initialState S, inputFunction(S, T, S), outputFunction(S, R))`  |  R  |  Returns a single value, reduced from the time series. The `inputFunction` will be invoked on each element in timeseries in order. In addition to taking the current element, inputFunction takes the current state (initially `initialState`) and returns the new state. The `outputFunction` will be invoked to turn the final state into the result value. The `outputFunction` can be an identity function.  | 

## Query examples
<a name="w2aab7c59c13c13c23b9"></a>

**Example**  
Construct a time series of CPU utilization of a host and filter points with measurement greater than 70:  

```
WITH time_series_view AS (
    SELECT INTERPOLATE_LINEAR(
        CREATE_TIME_SERIES(time, ROUND(measure_value::double,2)), 
            SEQUENCE(ago(15m), ago(1m), 10s)) AS cpu_user
    FROM sample.DevOps
    WHERE hostname = 'host-Hovjv' and measure_name = 'cpu_utilization'
        AND time > ago(30m)
    GROUP BY hostname
)
SELECT FILTER(cpu_user, x -> x.value > 70.0) AS cpu_above_threshold
from time_series_view
```

**Example**  
Construct a time series of CPU utilization of a host and determine the sum squared of the measurements:  

```
WITH time_series_view AS (
    SELECT INTERPOLATE_LINEAR(
        CREATE_TIME_SERIES(time, ROUND(measure_value::double,2)), 
            SEQUENCE(ago(15m), ago(1m), 10s)) AS cpu_user
    FROM sample.DevOps
    WHERE hostname = 'host-Hovjv' and measure_name = 'cpu_utilization'
        AND time > ago(30m)
    GROUP BY hostname
)
SELECT REDUCE(cpu_user,
    DOUBLE '0.0',
    (s, x) -> x.value * x.value + s,
    s -> s)
from time_series_view
```

**Example**  
Construct a time series of CPU utilization of a host and determine the fraction of samples that are above the CPU threshold:  

```
WITH time_series_view AS (
    SELECT INTERPOLATE_LINEAR(
        CREATE_TIME_SERIES(time, ROUND(measure_value::double,2)), 
            SEQUENCE(ago(15m), ago(1m), 10s)) AS cpu_user
    FROM sample.DevOps
    WHERE hostname = 'host-Hovjv' and measure_name = 'cpu_utilization'
        AND time > ago(30m)
    GROUP BY hostname
)
SELECT ROUND(
    REDUCE(cpu_user, 
      -- initial state 
      CAST(ROW(0, 0) AS ROW(count_high BIGINT, count_total BIGINT)),
      -- function to count the total points and points above a certain threshold
      (s, x) -> CAST(ROW(s.count_high + IF(x.value > 70.0, 1, 0), s.count_total + 1) AS ROW(count_high BIGINT, count_total BIGINT)),
      -- output function converting the counts to fraction above threshold
      s -> IF(s.count_total = 0, NULL, CAST(s.count_high AS DOUBLE) / s.count_total)), 
    4) AS fraction_cpu_above_threshold
from time_series_view
```

# SQL support
<a name="supported-sql-constructs"></a>

Timestream for LiveAnalytics supports some common SQL constructs. You can read more below.

**Topics**
+ [SELECT](supported-sql-constructs.SELECT.md)
+ [Subquery support](supported-sql-constructs.subquery-support.md)
+ [SHOW statements](supported-sql-constructs.SHOW.md)
+ [DESCRIBE statements](supported-sql-constructs.DESCRIBE.md)
+ [UNLOAD](supported-sql-constructs.UNLOAD.md)

# SELECT
<a name="supported-sql-constructs.SELECT"></a>

**SELECT** statements can be used to retrieve data from one or more tables. Timestream's query language supports the following syntax for **SELECT** statements:

```
[ WITH with_query [, ...] ]
            SELECT [ ALL | DISTINCT ] select_expr [, ...]
            [ function (expression) OVER (
            [ PARTITION BY partition_expr_list ]
            [ ORDER BY order_list ]
            [ frame_clause ] )
            [ FROM from_item [, ...] ]
            [ WHERE condition ]
            [ GROUP BY [ ALL | DISTINCT ] grouping_element [, ...] ]
            [ HAVING condition]
            [ { UNION | INTERSECT | EXCEPT } [ ALL | DISTINCT ] select ]
            [ ORDER BY order_list ]
            [ LIMIT [ count | ALL ] ]
```

where 
+ `function (expression)` is one of the supported [window functions](window-functions.md).
+ `partition_expr_list` is:

  ```
  expression | column_name [, expr_list ]
  ```
+ `order_list` is:

  ```
  expression | column_name [ ASC | DESC ] 
  [ NULLS FIRST | NULLS LAST ]
  [, order_list ]
  ```
+ `frame_clause` is:

  ```
  ROWS | RANGE
  { UNBOUNDED PRECEDING | expression PRECEDING | CURRENT ROW } |
  {BETWEEN
  { UNBOUNDED PRECEDING | expression { PRECEDING | FOLLOWING } |
  CURRENT ROW}
  AND
  { UNBOUNDED FOLLOWING | expression { PRECEDING | FOLLOWING } |
  CURRENT ROW }}
  ```
+ `from_item` is one of:

  ```
  table_name [ [ AS ] alias [ ( column_alias [, ...] ) ] ]
  from_item join_type from_item [ ON join_condition | USING ( join_column [, ...] ) ]
  ```
+ `join_type` is one of:

  ```
  [ INNER ] JOIN
  LEFT [ OUTER ] JOIN
  RIGHT [ OUTER ] JOIN
  FULL [ OUTER ] JOIN
  ```
+ `grouping_element` is one of:

  ```
  ()
  expression
  ```

# Subquery support
<a name="supported-sql-constructs.subquery-support"></a>

 Timestream supports subqueries in `EXISTS` and `IN` predicates. The `EXISTS` predicate determines if a subquery returns any rows. The `IN` predicate determines if values produced by the subquery match the values or expression of in IN clause. The Timestream query language supports correlated and other subqueries. 

```
SELECT t.c1
FROM (VALUES 1, 2, 3, 4, 5) AS t(c1)
WHERE EXISTS
(SELECT t.c2
 FROM (VALUES 1, 2, 3) AS t(c2)
 WHERE t.c1= t.c2
)
ORDER BY t.c1
```


| c1 | 
| --- | 
|  1  | 
|  2  | 
|  3  | 

```
SELECT t.c1
FROM (VALUES 1, 2, 3, 4, 5) AS t(c1)
WHERE t.c1 IN
(SELECT t.c2
 FROM (VALUES 2, 3, 4) AS t(c2)
)
ORDER BY t.c1
```


| c1 | 
| --- | 
|  2  | 
|  3  | 
|  4  | 

# SHOW statements
<a name="supported-sql-constructs.SHOW"></a>

You can view all the databases in an account by using the `SHOW DATABASES` statement. The syntax is as follows:

```
SHOW DATABASES [LIKE pattern]
```

where the `LIKE` clause can be used to filter database names.

You can view all the tables in an account by using the `SHOW TABLES` statement. The syntax is as follows:

```
SHOW TABLES [FROM database] [LIKE pattern]
```

where the `FROM` clause can be used to filter database names and the `LIKE` clause can be used to filter table names.

You can view all the measures for a table by using the `SHOW MEASURES` statement. The syntax is as follows:

```
SHOW MEASURES FROM database.table [LIKE pattern]
```

where the `FROM` clause will be used to specify the database and table name and the `LIKE` clause can be used to filter measure names.

# DESCRIBE statements
<a name="supported-sql-constructs.DESCRIBE"></a>

You can view the metadata for a table by using the `DESCRIBE` statement. The syntax is as follows:

```
DESCRIBE database.table
```

where `table` contains the table name. The describe statement returns the column names and data types for the table.

# UNLOAD
<a name="supported-sql-constructs.UNLOAD"></a>

Timestream for LiveAnalytics supports an `UNLOAD` command as an extension to its SQL support. Data types supported by `UNLOAD` are described in [Supported data types](supported-data-types.md). The `time` and `unknown` types do not apply to `UNLOAD`.

```
UNLOAD (SELECT statement)
 TO 's3://bucket-name/folder'
 WITH ( option = expression [, ...] )
```

where option is

```
{ partitioned_by = ARRAY[ col_name[,…] ] 
 | format = [ '{ CSV | PARQUET }' ] 
 | compression = [ '{ GZIP | NONE }' ]
 | encryption = [ '{ SSE_KMS | SSE_S3 }' ]
 | kms_key = '<string>'
 | field_delimiter ='<character>'
 | escaped_by = '<character>'
 | include_header = ['{true, false}']
 | max_file_size = '<value>'
}
```

SELECT statement  
The query statement used to select and retrieve data from one or more Timestream for LiveAnalytics tables.   

```
(SELECT column 1, column 2, column 3 from database.table
      where measure_name = "ABC" and timestamp between ago (1d) and now() )
```

TO clause  

```
TO 's3://bucket-name/folder'
```
or  

```
TO 's3://access-point-alias/folder'
```
The `TO` clause in the `UNLOAD` statement specifies the destination for the output of the query results. You need to provide the full path, including either Amazon S3 bucket-name or Amazon S3 access-point-alias with folder location on Amazon S3 where Timestream for LiveAnalytics writes the output file objects. The S3 bucket should be owned by the same account and in the same region. In addition to the query result set, Timestream for LiveAnalytics writes the manifest and metadata files to specified destination folder. 

PARTITIONED\$1BY clause  

```
partitioned_by = ARRAY [col_name[,…] , (default: none)
```
The `partitioned_by` clause is used in queries to group and analyze data at a granular level. When you export your query results to the S3 bucket, you can choose to partition the data based on one or more columns in the select query. When partitioning the data, the exported data is divided into subsets based on the partition column and each subset is stored in a separate folder. Within the results folder that contains your exported data, a sub-folder `folder/results/partition column = partition value/` is automatically created. However, note that partitioned columns are not included in the output file.   
`partitioned_by` is not a mandatory clause in the syntax. If you choose to export the data without any partitioning, you can exclude the clause in the syntax.   

**Example**  
Assuming you are monitoring clickstream data of your website and have 5 channels of traffic namely `direct`, `Social Media`, `Organic Search`, `Other`, and `Referral`. When exporting the data, you can choose to partition the data using the column `Channel`. Within your data folder, `s3://bucketname/results`, you will have five folders each with their respective channel name, for instance, `s3://bucketname/results/channel=Social Media/.` Within this folder you will find the data of all the customers that landed on your website through the `Social Media` channel. Similarly, you will have other folders for the remaining channels.
Exported data partitioned by Channel column  

![\[Folder structure showing channels: Direct, Organic search, Other, Referral, and Social media.\]](http://docs.aws.amazon.com/timestream/latest/developerguide/images/unload-results.png)


FORMAT  

```
format = [ '{ CSV | PARQUET }' , default: CSV
```
The keywords to specify the format of the query results written to your S3 bucket. You can export the data either as a comma separated value (CSV) using a comma (,) as the default delimiter or in the Apache Parquet format, an efficient open columnar storage format for analytics. 

COMPRESSION  

```
compression = [ '{ GZIP | NONE }' ], default: GZIP
```
You can compress the exported data using compression algorithm GZIP or have it uncompressed by specifying the `NONE` option.

ENCRYPTION  

```
encryption = [ '{ SSE_KMS | SSE_S3 }' ], default: SSE_S3
```
The output files on Amazon S3 are encrypted using your selected encryption option. In addition to your data, the manifest and metadata files are also encrypted based on your selected encryption option. We currently support SSE\$1S3 and SSE\$1KMS encryption. SSE\$1S3 is a server-side encryption with Amazon S3 encrypting the data using 256-bit advanced encryption standard (AES) encryption. SSE\$1KMS is a server-side encryption to encrypt data using customer-managed keys.

KMS\$1KEY  

```
kms_key = '<string>'
```
KMS Key is a customer-defined key to encrypt exported query results. KMS Key is securely managed by AWS Key Management Service (AWS KMS) and used to encrypt data files on Amazon S3.

FIELD\$1DELIMITER  

```
field_delimiter ='<character>' , default: (,)
```
When exporting the data in CSV format, this field specifies a single ASCII character that is used to separate fields in the output file, such as pipe character (\$1), a comma (,), or tab (/t). The default delimiter for CSV files is a comma character. If a value in your data contains the chosen delimiter, the delimiter will be quoted with a quote character. For instance, if the value in your data contains `Time,stream`, then this value will be quoted as `"Time,stream"` in the exported data. The quote character used by Timestream for LiveAnalytics is double quotes (").  
Avoid specifying the carriage return character (ASCII 13, hex `0D`, text '\$1r') or the line break character (ASCII 10, hex 0A, text '\$1n') as the `FIELD_DELIMITER` if you want to include headers in the CSV, since that will prevent many parsers from being able to parse the headers correctly in the resulting CSV output.

ESCAPED\$1BY  

```
escaped_by = '<character>', default: (\)
```
When exporting the data in CSV format, this field specifies the character that should be treated as an escape character in the data file written to S3 bucket. Escaping happens in the following scenarios:  

1. If the value itself contains the quote character (") then it will be escaped using an escape character. For example, if the value is `Time"stream`, where (\$1) is the configured escape character, then it will be escaped as `Time\"stream`. 

1. If the value contains the configured escape character, it will be escaped. For example, if the value is `Time\stream`, then it will be escaped as `Time\\stream`. 
If the exported output contains complex data type in the like Arrays, Rows or Timeseries, it will be serialized as a JSON string. Following is an example.    
[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/timestream/latest/developerguide/supported-sql-constructs.UNLOAD.html)

INCLUDE\$1HEADER  

```
include_header = 'true' , default: 'false'
```
When exporting the data in CSV format, this field lets you include column names as the first row of the exported CSV data files.  
The accepted values are 'true' and 'false' and the default value is 'false'. Text transformation options such as `escaped_by` and `field_delimiter` apply to headers as well.  
When including headers, it is important that you not select a carriage return character (ASCII 13, hex 0D, text '\$1r') or a line break character (ASCII 10, hex 0A, text '\$1n') as the `FIELD_DELIMITER`, since that will prevent many parsers from being able to parse the headers correctly in the resulting CSV output.

MAX\$1FILE\$1SIZE  

```
max_file_size = 'X[MB|GB]' , default: '78GB'
```
This field specifies the maximum size of the files that the `UNLOAD` statement creates in Amazon S3. The `UNLOAD` statement can create multiple files but the maximum size of each file written to Amazon S3 will be approximately what is specified in this field.  
The value of the field must be between 16 MB and 78 GB, inclusive. You can specify it in integer such as `12GB`, or in decimals such as `0.5GB` or `24.7MB`. The default value is 78 GB.  
The actual file size is approximated when the file is being written, so the actual maximum size may not be exactly equal to the number you specify.

# Logical operators
<a name="logical-operators"></a>

Timestream for LiveAnalytics supports the following logical operators.


| Operator | Description | Example | 
| --- | --- | --- | 
|  AND  |  True if both values are true  |  a AND b  | 
|  OR  |  True if either value is true  |  a OR b  | 
|  NOT  |  True if the value is false  |  NOT a  | 
+ The result of an `AND` comparison may be `NULL` if one or both sides of the expression are `NULL`. 
+ If at least one side of an `AND` operator is `FALSE` the expression evaluates to `FALSE`. 
+ The result of an `OR` comparison may be `NULL` if one or both sides of the expression are `NULL`. 
+ If at least one side of an `OR` operator is `TRUE` the expression evaluates to `TRUE`. 
+ The logical complement of `NULL` is `NULL`. 

The following truth table demonstrates the handling of `NULL` in `AND` and `OR`:


| A | B | A and b | A or b | 
| --- | --- | --- | --- | 
|  null  |  null  |  null  |  null  | 
|  false   |  null  |  false  |  null  | 
|  null  |  false  |  false  |  null  | 
|  true  |  null  |  null  |  true  | 
|  null  |  true  |  null  |  true  | 
|  false  |  false  |  false  |  false  | 
|  true  |  false  |  false  |  true  | 
|  false  |  true  |  false  |  true  | 
|  true  |  true  |  true  |  true  | 

The following truth table demonstrates the handling of NULL in NOT:


| A | Not a | 
| --- | --- | 
|  null  |  null  | 
|  true  |  false  | 
|  false  |  true  | 

# Comparison operators
<a name="comparison-operators"></a>

Timestream for LiveAnalytics supports the following comparison operators.


| Operator | Description | 
| --- | --- | 
|  <  |  Less than  | 
|  >  |  Greater than  | 
|  <=  |  Less than or equal to  | 
|  >=  |  Greater than or equal to  | 
|  =  |  Equal  | 
|  <>  |  Not equal  | 
|  \$1=  |  Not equal  | 

**Note**  
The `BETWEEN` operator tests if a value is within a specified range. The syntax is as follows:  

  ```
  BETWEEN min AND max
  ```
The presence of `NULL` in a `BETWEEN` or `NOT BETWEEN` statement will result in the statement evaluating to `NULL`.
`IS NULL `and `IS NOT NULL` operators test whether a value is null (undefined). Using `NULL` with `IS NULL` evaluates to true.
In SQL, a `NULL` value signifies an unknown value.

# Comparison functions
<a name="comparison-functions"></a>

Timestream for LiveAnalytics supports the following comparison functions.

**Topics**
+ [greatest()](comparison-functions.greatest.md)
+ [least()](comparison-functions.least.md)
+ [ALL(), ANY() and SOME()](comparison-functions.all-any-some.md)

# greatest()
<a name="comparison-functions.greatest"></a>

The **greatest()** function returns the largest of the provided values. It returns `NULL` if any of the provided values are `NULL`. The syntax is as follows.

```
greatest(value1, value2, ..., valueN) 
```

# least()
<a name="comparison-functions.least"></a>

The **least()** function returns the smallest of the provided values. It returns `NULL` if any of the provided values are `NULL`. The syntax is as follows.

```
least(value1, value2, ..., valueN) 
```

# ALL(), ANY() and SOME()
<a name="comparison-functions.all-any-some"></a>

The `ALL`, `ANY` and `SOME` quantifiers can be used together with comparison operators in the following way.


| Expression | Meaning | 
| --- | --- | 
|  A = ALL(...)  |  Evaluates to true when A is equal to all values.  | 
|  A <> ALL(...)  |  Evaluates to true when A does not match any value.  | 
|  A < ALL(...)  |  Evaluates to true when A is smaller than the smallest value.  | 
|  A = ANY(...)  |  Evaluates to true when A is equal to any of the values.   | 
|  A <> ANY(...)  |  Evaluates to true when A does not match one or more values.  | 
|  A < ANY(...)  |  Evaluates to true when A is smaller than the biggest value.  | 

## Examples and usage notes
<a name="comparison-functions.all-any-some.examples-usage"></a>

**Note**  
When using `ALL`, `ANY` or `SOME`, the keyword `VALUES` should be used if the comparison values are a list of literals. 

## Example: `ANY()`
<a name="w2aab7c59c21c11c11"></a>

An example of `ANY()` in a query statement as follows.

```
SELECT 11.7 = ANY (VALUES 12.0, 13.5, 11.7)
```

An alternative syntax for the same operation is as follows.

```
SELECT 11.7 = ANY (SELECT 12.0 UNION ALL SELECT 13.5 UNION ALL SELECT 11.7)
```

In this case, `ANY()` evaluates to `True`.

## Example: `ALL()`
<a name="w2aab7c59c21c11c13"></a>

An example of `ALL()` in a query statement as follows.

```
SELECT 17 < ALL (VALUES 19, 20, 15);
```

An alternative syntax for the same operation is as follows.

```
SELECT 17 < ALL (SELECT 19 UNION ALL SELECT 20 UNION ALL SELECT 15);
```

In this case, `ALL()` evaluates to `False`.

## Example: `SOME()`
<a name="w2aab7c59c21c11c15"></a>

An example of `SOME()` in a query statement as follows.

```
SELECT 50 >= SOME (VALUES 53, 77, 27);
```

An alternative syntax for the same operation is as follows.

```
SELECT 50 >= SOME (SELECT 53 UNION ALL SELECT 77 UNION ALL SELECT 27);
```

In this case, `SOME()` evaluates to `True`.

# Conditional expressions
<a name="conditional-expressions"></a>

Timestream for LiveAnalytics supports the following conditional expressions.

**Topics**
+ [The CASE statement](conditional-expressions.CASE.md)
+ [The IF statement](conditional-expressions.IF.md)
+ [The COALESCE statement](conditional-expressions.COALESCE.md)
+ [The NULLIF statement](conditional-expressions.NULLIF.md)
+ [The TRY statement](conditional-expressions.TRY.md)

# The CASE statement
<a name="conditional-expressions.CASE"></a>

The **CASE** statement searches each value expression from left to right until it finds one that equals `expression`. If it finds a match, the result for the matching value is returned. If no match is found, the result from the `ELSE` clause is returned if it exists; otherwise `null` is returned. The syntax is as follows:

```
CASE expression
    WHEN value THEN result
    [ WHEN ... ]
    [ ELSE result ]
END
```

 Timestream also supports the following syntax for **CASE** statements. In this syntax, the "searched" form evaluates each boolean condition from left to right until one is `true` and returns the matching result. If no conditions are `true`, the result from the `ELSE` clause is returned if it exists; otherwise `null` is returned. See below for the alternate syntax: 

```
CASE
    WHEN condition THEN result
    [ WHEN ... ]
    [ ELSE result ]
END
```

# The IF statement
<a name="conditional-expressions.IF"></a>

The **IF** statement evaluates a condition to be true or false and returns the appropriate value. Timestream supports the following two syntax representations for **IF**:

```
if(condition, true_value)
```

This syntax evaluates and returns `true_value` if condition is `true`; otherwise `null` is returned and `true_value` is not evaluated.

```
if(condition, true_value, false_value)
```

This syntax evaluates and returns `true_value` if condition is `true`, otherwise evaluates and returns `false_value`.

## Examples
<a name="conditional-expressions.IF.examples"></a>

```
SELECT
  if(true, 'example 1'),
  if(false, 'example 2'),
  if(true, 'example 3 true', 'example 3 false'),
  if(false, 'example 4 true', 'example 4 false')
```


| \$1col0 | \$1col1 | \$1col2 | \$1col3 | 
| --- | --- | --- | --- | 
|  `example 1`  |  `-` `null`  |  `example 3 true`  |  `example 4 false`  | 

# The COALESCE statement
<a name="conditional-expressions.COALESCE"></a>

 **COALESCE** returns the first non-null value in an argument list. The syntax is as follows:

```
coalesce(value1, value2[,...])
```

# The NULLIF statement
<a name="conditional-expressions.NULLIF"></a>

The **IF** statement evaluates a condition to be true or false and returns the appropriate value. Timestream supports the following two syntax representations for **IF**:

**NULLIF** returns null if `value1` equals `value2`; otherwise it returns `value1`. The syntax is as follows:

```
nullif(value1, value2)
```

# The TRY statement
<a name="conditional-expressions.TRY"></a>

The **TRY** function evaluates an expression and handles certain types of errors by returning `null`. The syntax is as follows:

```
try(expression)
```

# Conversion functions
<a name="conversion-functions"></a>

Timestream for LiveAnalytics supports the following conversion functions.

**Topics**
+ [cast()](#conversion-functions.cast)
+ [try\$1cast()](#conversion-functions.try-cast)

## cast()
<a name="conversion-functions.cast"></a>

 The syntax of the cast function to explicitly cast a value as a type is as follows.

```
cast(value AS type)
```

## try\$1cast()
<a name="conversion-functions.try-cast"></a>

Timestream for LiveAnalytics also supports the try\$1cast function that is similar to cast but returns null if cast fails. The syntax is as follows.

```
try_cast(value AS type)
```

# Mathematical operators
<a name="mathematical-operators"></a>

Timestream for LiveAnalytics supports the following mathematical operators.


| Operator | Description | 
| --- | --- | 
|  \$1  |  Addition  | 
|  -  |  Subtraction  | 
|  \$1  |  Multiplication  | 
|  /  |  Division (integer division performs truncation)  | 
|  %  |  Modulus (remainder)  | 

# Mathematical functions
<a name="mathematical-functions"></a>

Timestream for LiveAnalytics supports the following mathematical functions.


| Function | Output data type | Description | 
| --- | --- | --- | 
|  abs(x)  |  [same as input]  |  Returns the absolute value of x.  | 
|  cbrt(x)  |  double  |  Returns the cube root of x.  | 
|  ceiling(x) or ceil(x)  |  [same as input]  |  Returns x rounded up to the nearest integer.  | 
|  degrees(x)  |  double  |  Converts angle x in radians to degrees.  | 
|  e()  |  double  |  Returns the constant Euler's number.  | 
|  exp(x)  |  double  |  Returns Euler's number raised to the power of x.  | 
|  floor(x)  |  [same as input]  |  Returns x rounded down to the nearest integer.  | 
|  from\$1base(string,radix)  |  bigint  |  Returns the value of string interpreted as a base-radix number.  | 
|  ln(x)  |  double  |  Returns the natural logarithm of x.  | 
|  log2(x)  |  double  |  Returns the base 2 logarithm of x.  | 
|  log10(x)  |  double  |  Returns the base 10 logarithm of x.  | 
|  mod(n,m)   |  [same as input]  |  Returns the modulus (remainder) of n divided by m.  | 
|  pi()   |  double  |  Returns the constant Pi.  | 
|  pow(x, p) or power(x, p)  |  double  |  Returns x raised to the power of p.  | 
|  radians(x)  |  double  |  Converts angle x in degrees to radians.  | 
|  rand() or random()  |  double  |  Returns a pseudo-random value in the range 0.0 1.0.  | 
|  random(n)  |  [same as input]  |  Returns a pseudo-random number between 0 and n (exclusive).  | 
|  round(x)  |  [same as input]  |  Returns x rounded to the nearest integer.  | 
|  round(x,d)  |  [same as input]  |  Returns x rounded to d decimal places.  | 
|  sign(x)  |  [same as input]  |  Returns the signum function of x, that is: [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/timestream/latest/developerguide/mathematical-functions.html) For double arguments, the function additionally returns: [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/timestream/latest/developerguide/mathematical-functions.html)  | 
|  sqrt(x)   |  double  |  Returns the square root of x.  | 
|  to\$1base(x, radix)   |  varchar  |  Returns the base-radix representation of x.  | 
|  truncate(x)   |  double  |  Returns x rounded to integer by dropping digits after decimal point.  | 
|  acos(x)  |  double  |  Returns the arc cosine of x.  | 
|  asin(x)   |  double  |  Returns the arc sine of x.  | 
|  atan(x)   |  double  |  Returns the arc tangent of x.  | 
|  atan2(y, x)  |  double  |  Returns the arc tangent of y / x.  | 
|  cos(x)  |  double  |  Returns the cosine of x.  | 
|  cosh(x)  |  double  |  Returns the hyperbolic cosine of x.  | 
|  sin(x)   |  double  |  Returns the sine of x.  | 
|  tan(x)  |  double  |  Returns the tangent of x.  | 
|  tanh(x)  |  double  |  Returns the hyperbolic tangent of x.  | 
|  infinity()  |  double  |  Returns the constant representing positive infinity.  | 
|  is\$1finite(x)  |  boolean  |  Determine if x is finite.  | 
|  is\$1infinite(x)  |  boolean  |  Determine if x is infinite.  | 
|  is\$1nan(x)  |  boolean  |  Determine if x is not-a-number.  | 
|  nan()  |  double  |  Returns the constant representing not-a-number.  | 

# String operators
<a name="string-operators"></a>

Timestream for LiveAnalytics supports the `||` operator for concatenating one or more strings.

# String functions
<a name="string-functions"></a>

**Note**  
The input data type of these functions is assumed to be varchar unless otherwise specified.


| Function | Output data type | Description | 
| --- | --- | --- | 
|  chr(n)   |  varchar  |  Returns the Unicode code point n as a varchar.  | 
|  codepoint(x)   |  integer  |  Returns the Unicode code point of the only character of str.  | 
|  concat(x1, ..., xN)  |  varchar  |  Returns the concatenation of x1, x2, ..., xN.  | 
|  hamming\$1distance(x1,x2)   |  bigint  |  Returns the Hamming distance of x1 and x2, i.e. the number of positions at which the corresponding characters are different. Note that the two varchar inputs must have the same length.  | 
|  length(x)  |  bigint  |  Returns the length of x in characters.  | 
|  levenshtein\$1distance(x1, x2)   |  bigint  |  Returns the Levenshtein edit distance of x1 and x2, i.e. the minimum number of single-character edits (insertions, deletions or substitutions) needed to change x1 into x2.  | 
|  lower(x)  |  varchar  |  Converts x to lowercase.  | 
|  lpad(x1, bigint size, x2)  |  varchar  |  Left pads x1 to size characters with x2. If size is less than the length of x1, the result is truncated to size characters. size must not be negative and x2 must be non-empty.  | 
|  ltrim(x)  |  varchar  |  Removes leading whitespace from x.  | 
|  replace(x1, x2)  |  varchar  |  Removes all instances of x2 from x1.  | 
|  replace(x1, x2, x3)  |  varchar  |  Replaces all instances of x2 with x3 in x1.  | 
|  Reverse(x)   |  varchar  |  Returns x with the characters in reverse order.  | 
|  rpad(x1, bigint size, x2)  |  varchar  |  Right pads x1 to size characters with x2. If size is less than the length of x1, the result is truncated to size characters. size must not be negative and x2 must be non-empty.  | 
|  rtrim(x)  |  varchar  |  Removes trailing whitespace from x.  | 
|  split(x1, x2)  |  array(varchar)  |  Splits x1 on delimiter x2 and returns an array.  | 
|  split(x1, x2, bigint limit)  |  array(varchar)  |  Splits x1 on delimiter x2 and returns an array. The last element in the array always contain everything left in the x1. limit must be a positive number.  | 
|  split\$1part(x1, x2, bigint pos)   |  varchar  |  Splits x1 on delimiter x2 and returns the varchar field at pos. Field indexes start with 1. If pos is larger than the number of fields, then null is returned.  | 
|  strpos(x1, x2)   |  bigint  |  Returns the starting position of the first instance of x2 in x1. Positions start with 1. If not found, 0 is returned.  | 
|  strpos(x1, x2,bigint instance)   |  bigint  |  Returns the position of the Nth instance of x2 in x1. Instance must be a positive number. Positions start with 1. If not found, 0 is returned.  | 
|  strrpos(x1, x2)   |  bigint  |  Returns the starting position of the last instance of x2 in x1. Positions start with 1. If not found, 0 is returned.  | 
|  strrpos(x1, x2, bigint instance)   |  bigint  |  Returns the position of the Nth instance of x2 in x1 starting from the end of x1. instance must be a positive number. Positions start with 1. If not found, 0 is returned.  | 
|  position(x2 IN x1)   |  bigint  |  Returns the starting position of the first instance of x2 in x1. Positions start with 1. If not found, 0 is returned.  | 
|  substr(x, bigint start)   |  varchar  |  Returns the rest of x from the starting position start. Positions start with 1. A negative starting position is interpreted as being relative to the end of x.  | 
|  substr(x, bigint start, bigint len)   |  varchar  |  Returns a substring from x of length len from the starting position start. Positions start with 1. A negative starting position is interpreted as being relative to the end of x.  | 
|  trim(x)   |  varchar  |  Removes leading and trailing whitespace from x.  | 
|  upper(x)   |  varchar  |  Converts x to uppercase.  | 

# Array operators
<a name="array-operators"></a>

Timestream for LiveAnalytics supports the following array operators.


| Operator | Description | 
| --- | --- | 
|  []  |  Access an element of an array where the first index starts at 1.  | 
|  \$1\$1  |  Concatenate an array with another array or element of the same type.  | 

# Array functions
<a name="array-functions"></a>

Timestream for LiveAnalytics supports the following array functions.


| Function | Output data type | Description | 
| --- | --- | --- | 
|  array\$1distinct(x)  |  array  |  Remove duplicate values from the array x. <pre>SELECT array_distinct(ARRAY[1,2,2,3])</pre> Example result: `[ 1,2,3 ]`  | 
|  array\$1intersect(x, y)  |  array  |  Returns an array of the elements in the intersection of x and y, without duplicates. <pre>SELECT array_intersect(ARRAY[1,2,3], ARRAY[3,4,5])</pre> Example result: `[ 3 ]`  | 
|  array\$1union(x, y)  |  array  |  Returns an array of the elements in the union of x and y, without duplicates. <pre>SELECT array_union(ARRAY[1,2,3], ARRAY[3,4,5])</pre> Example result: `[ 1,2,3,4,5 ]`  | 
|  array\$1except(x, y)  |  array  |  Returns an array of elements in x but not in y, without duplicates. <pre>SELECT array_except(ARRAY[1,2,3], ARRAY[3,4,5])</pre> Example result: `[ 1,2 ]`  | 
|  array\$1join(x, delimiter, null\$1replacement)   |  varchar  |  Concatenates the elements of the given array using the delimiter and an optional string to replace nulls. <pre>SELECT array_join(ARRAY[1,2,3], ';', '')</pre> Example result: `1;2;3`  | 
|  array\$1max(x)  |  same as array elements  |  Returns the maximum value of input array. <pre>SELECT array_max(ARRAY[1,2,3])</pre> Example result: `3`  | 
|  array\$1min(x)  |  same as array elements  |  Returns the minimum value of input array. <pre>SELECT array_min(ARRAY[1,2,3])</pre> Example result: `1`  | 
|  array\$1position(x, element)  |  bigint  |  Returns the position of the first occurrence of the element in array x (or 0 if not found). <pre>SELECT array_position(ARRAY[3,4,5,9], 5)</pre> Example result: `3`  | 
|  array\$1remove(x, element)  |  array  |  Remove all elements that equal element from array x. <pre>SELECT array_remove(ARRAY[3,4,5,9], 4)</pre> Example result: `[ 3,5,9 ]`  | 
|  array\$1sort(x)  |  array  |  Sorts and returns the array x. The elements of x must be orderable. Null elements will be placed at the end of the returned array. <pre>SELECT array_sort(ARRAY[6,8,2,9,3])</pre> Example result: `[ 2,3,6,8,9 ]`  | 
|  arrays\$1overlap(x, y)   |  boolean  |  Tests if arrays x and y have any non-null elements in common. Returns null if there are no non-null elements in common but either array contains null. <pre>SELECT arrays_overlap(ARRAY[6,8,2,9,3], ARRAY[6,8])</pre> Example result: `true`  | 
|  cardinality(x)  |  bigint  |  Returns the size of the array x. <pre>SELECT cardinality(ARRAY[6,8,2,9,3])</pre> Example result: `5`  | 
|  concat(array1, array2, ..., arrayN)  |  array  |  Concatenates the arrays array1, array2, ..., arrayN. <pre>SELECT concat(ARRAY[6,8,2,9,3], ARRAY[11,32], ARRAY[6,8,2,0,14])</pre> Example result: `[ 6,8,2,9,3,11,32,6,8,2,0,14 ]`  | 
|  element\$1at(array(E), index)  |  E  |  Returns element of array at given index. If index < 0, element\$1at accesses elements from the last to the first. <pre>SELECT element_at(ARRAY[6,8,2,9,3], 1)</pre> Example result: `6`  | 
|  repeat(element, count)   |  array  |  Repeat element for count times. <pre>SELECT repeat(1, 3)</pre> Example result: `[ 1,1,1 ]`  | 
|  reverse(x)  |  array  |  Returns an array which has the reversed order of array x. <pre>SELECT reverse(ARRAY[6,8,2,9,3])</pre> Example result: `[ 3,9,2,8,6 ]`  | 
|  sequence(start, stop)  |  array(bigint)  |  Generate a sequence of integers from start to stop, incrementing by 1 if start is less than or equal to stop, otherwise -1. <pre>SELECT sequence(3, 8)</pre> Example result: `[ 3,4,5,6,7,8 ]`  | 
|  sequence(start, stop, step)   |  array(bigint)  |  Generate a sequence of integers from start to stop, incrementing by step. <pre>SELECT sequence(3, 15, 2)</pre> Example result: `[ 3,5,7,9,11,13,15 ]`  | 
|  sequence(start, stop)   |  array(timestamp)  |  Generate a sequence of timestamps from start date to stop date, incrementing by 1 day. <pre>SELECT sequence('2023-04-02 19:26:12.941000000', '2023-04-06 19:26:12.941000000', 1d)</pre> Example result: `[ 2023-04-02 19:26:12.941000000,2023-04-03 19:26:12.941000000,2023-04-04 19:26:12.941000000,2023-04-05 19:26:12.941000000,2023-04-06 19:26:12.941000000 ]`  | 
|  sequence(start, stop, step)   |  array(timestamp)  |  Generate a sequence of timestamps from start to stop, incrementing by step. The data type of step is interval. <pre>SELECT sequence('2023-04-02 19:26:12.941000000', '2023-04-10 19:26:12.941000000', 2d)</pre> Example result: `[ 2023-04-02 19:26:12.941000000,2023-04-04 19:26:12.941000000,2023-04-06 19:26:12.941000000,2023-04-08 19:26:12.941000000,2023-04-10 19:26:12.941000000 ]`  | 
|  shuffle(x)  |  array  |  Generate a random permutation of the given array x. <pre>SELECT shuffle(ARRAY[6,8,2,9,3])</pre> Example result: `[ 6,3,2,9,8 ]`  | 
|  slice(x, start, length)  |  array  |  Subsets array x starting from index start (or starting from the end if start is negative) with a length of length. <pre>SELECT slice(ARRAY[6,8,2,9,3], 1, 3)</pre> Example result: `[ 6,8,2 ]`  | 
|  zip(array1, array2[, ...])  |  array(row)  |  Merges the given arrays, element-wise, into a single array of rows. If the arguments have an uneven length, missing values are filled with NULL. <pre>SELECT zip(ARRAY[6,8,2,9,3], ARRAY[15,24])</pre> Example result: `[ ( 6, 15 ),( 8, 24 ),( 2, - ),( 9, - ),( 3, - ) ]`  | 

# Bitwise functions
<a name="bitwise-functions"></a>

Timestream for LiveAnalytics supports the following bitwise functions.


| Function | Output data type | Description | 
| --- | --- | --- | 
| bit\$1count(bigint, bigint) | bigint (two's complement) |  Returns the count of bits in the first bigint parameter where the second parameter is a bit signed integer such as 8 or 64. <pre>SELECT bit_count(19, 8)</pre> Example result: `3` <pre>SELECT bit_count(19, 2)</pre> Example result: `Number must be representable with the bits specified. 19 can not be represented with 2 bits`   | 
| bitwise\$1and(bigint, bigint) | bigint (two's complement) |  Returns the bitwise AND of the bigint parameters. <pre>SELECT bitwise_and(12, 7)</pre> Example result: `4`  | 
| bitwise\$1not(bigint) | bigint (two's complement) |  Returns the bitwise NOT of the bigint parameter. <pre>SELECT bitwise_not(12)</pre> Example result: `-13`  | 
| bitwise\$1or(bigint, bigint) | bigint (two's complement) |  Returns the bitwise OR of the bigint parameters. <pre>SELECT bitwise_or(12, 7)</pre> Example result: `15`  | 
| bitwise\$1xor(bigint, bigint) | bigint (two's complement) |  Returns the bitwise XOR of the bigint parameters. <pre>SELECT bitwise_xor(12, 7)</pre> Example result: `11`  | 

# Regular expression functions
<a name="regex-functions"></a>

The regular expression functions in Timestream for LiveAnalytics support the [Java pattern syntax](http://docs.oracle.com/javase/8/docs/api/java/util/regex/Pattern.html). Timestream for LiveAnalytics supports the following regular expression functions.


| Function | Output data type | Description | 
| --- | --- | --- | 
|  regexp\$1extract\$1all(string, pattern)  |  array(varchar)  |  Returns the substring(s) matched by the regular expression pattern in string. <pre>SELECT regexp_extract_all('example expect complex', 'ex\w')</pre> Example result: `[ exa,exp ]`  | 
|  regexp\$1extract\$1all(string, pattern, group)  |  array(varchar)  |  Finds all occurrences of the regular expression pattern in string and returns the [capturing group number](http://docs.oracle.com/javase/8/docs/api/java/util/regex/Pattern.html#gnumber) group. <pre>SELECT regexp_extract_all('example expect complex', '(ex)(\w)', 2)</pre> Example result: `[ a,p ]`  | 
|  regexp\$1extract(string, pattern)  |  varchar  |  Returns the first substring matched by the regular expression pattern in string. <pre>SELECT regexp_extract('example expect', 'ex\w')</pre> Example result: `exa`  | 
|  regexp\$1extract(string, pattern, group)   |  varchar  |  Finds the first occurrence of the regular expression pattern in string and returns the [capturing group number](http://docs.oracle.com/javase/8/docs/api/java/util/regex/Pattern.html#gnumber) group. <pre>SELECT regexp_extract('example expect', '(ex)(\w)', 2)</pre> Example result: `a`  | 
|  regexp\$1like(string, pattern)   |  boolean  |  Evaluates the regular expression pattern and determines if it is contained within string. This function is similar to the LIKE operator, except that the pattern only needs to be contained within string, rather than needing to match all of string. In other words, this performs a contains operation rather than a match operation. You can match the entire string by anchoring the pattern using ^ and \$1. <pre>SELECT regexp_like('example', 'ex')</pre> Example result: `true`  | 
|  regexp\$1replace(string, pattern)  |  varchar  |  Removes every instance of the substring matched by the regular expression pattern from string. <pre>SELECT regexp_replace('example expect', 'expect')</pre> Example result: `example`  | 
|  regexp\$1replace(string, pattern, replacement)   |  varchar  |  Replaces every instance of the substring matched by the regex pattern in string with replacement. Capturing groups can be referenced in replacement using \$1g for a numbered group or \$1\$1name\$1 for a named group. A dollar sign (\$1) may be included in the replacement by escaping it with a backslash (\$1\$1). <pre>SELECT regexp_replace('example expect', 'expect', 'surprise')</pre> Example result: `example surprise`  | 
|  regexp\$1replace(string, pattern, function)   |  varchar  |  Replaces every instance of the substring matched by the regular expression pattern in string using function. The [lambda expression](https://prestodb.io/docs/current/functions/lambda.html) function is invoked for each match with the capturing groups passed as an array. Capturing group numbers start at one; there is no group for the entire match (if you need this, surround the entire expression with parenthesis). <pre>SELECT regexp_replace('example', '(\w)', x -> upper(x[1]))</pre> Example result: `EXAMPLE`  | 
|  regexp\$1split(string, pattern)   |  array(varchar)  |  Splits string using the regular expression pattern and returns an array. Trailing empty strings are preserved. <pre>SELECT regexp_split('example', 'x')</pre> Example result: `[ e,ample ]`  | 

# Date / time operators
<a name="date-time-operators"></a>

**Note**  
Timestream for LiveAnalytics does not support negative time values. Any operation resulting in negative time results in error.

Timestream for LiveAnalytics supports the following operations on `timestamps`, `dates`, and `intervals`.


| Operator | Description | 
| --- | --- | 
|  \$1  |  Addition  | 
|  -  |  Subtraction  | 

**Topics**
+ [Operations](#date-time-operators-operations)
+ [Addition](#date-time-operators-addition)
+ [Subtraction](#date-time-operators-subtraction)

## Operations
<a name="date-time-operators-operations"></a>

The result type of an operation is based on the operands. Interval literals such as `1day` and `3s` can be used.

```
SELECT date '2022-05-21' + interval '2' day
```

```
SELECT date '2022-05-21' + 2d
```

```
SELECT date '2022-05-21' + 2day
```

Example result for each: `2022-05-23`

Interval units include `second`, `minute`, `hour`, `day`, `week`, `month`, and `year`. But in some cases not all are applicable. For example seconds, minutes, and hours can not be added to or subtracted from a date.

```
SELECT interval '4' year + interval '2' month
```

Example result: `4-2`

```
SELECT typeof(interval '4' year + interval '2' month)
```

Example result: `interval year to month`

Result type of interval operations may be `'interval year to month'` or `'interval day to second'` depending on the operands. Intervals can be added to or subtracted from `dates` and `timestamps`. But a `date` or `timestamp` cannot be added to or subtracted from a `date` or `timestamp`. To find intervals or durations related to dates or timestamps, see `date_diff` and related functions in [Interval and duration](date-time-functions.md#date-time-functions-interval-duration).

## Addition
<a name="date-time-operators-addition"></a>

**Example**  

```
SELECT date '2022-05-21' + interval '2' day
```
Example result: `2022-05-23`

**Example**  

```
SELECT typeof(date '2022-05-21' + interval '2' day)
```
Example result: `date`

**Example**  

```
SELECT interval '2' year + interval '4' month
```
Example result: `2-4`

**Example**  

```
SELECT typeof(interval '2' year + interval '4' month)
```
Example result: `interval year to month`

## Subtraction
<a name="date-time-operators-subtraction"></a>

**Example**  

```
SELECT timestamp '2022-06-17 01:00' - interval '7' hour
```
Example result: `2022-06-16 18:00:00.000000000`

**Example**  

```
SELECT typeof(timestamp '2022-06-17 01:00' - interval '7' hour)
```
Example result: `timestamp`

**Example**  

```
SELECT interval '6' day - interval '4' hour
```
Example result: `5 20:00:00.000000000`

**Example**  

```
SELECT typeof(interval '6' day - interval '4' hour)
```
Example result: `interval day to second`

# Date / time functions
<a name="date-time-functions"></a>

**Note**  
Timestream for LiveAnalytics does not support negative time values. Any operation resulting in negative time results in error.

Timestream for LiveAnalytics uses UTC timezone for date and time. Timestream supports the following functions for date and time.

**Topics**
+ [General and conversion](#date-time-functions-general)
+ [Interval and duration](#date-time-functions-interval-duration)
+ [Formatting and parsing](#date-time-functions-formatting-parsing)
+ [Extraction](#date-time-functions-extraction)

## General and conversion
<a name="date-time-functions-general"></a>

Timestream for LiveAnalytics supports the following general and conversion functions for date and time.


| Function | Output data type | Description | 
| --- | --- | --- | 
|  current\$1date  |  date  |  Returns current date in UTC. No parentheses used. <pre>SELECT current_date</pre> Example result: `2022-07-07`  This is also a reserved keyword. For a list of reserved keywords, see [Reserved keywords](ts-limits.md#limits.reserved).   | 
|  current\$1time  |  time  |  Returns current time in UTC. No parentheses used. <pre>SELECT current_time</pre> Example result: `17:41:52.827000000`  This is also a reserved keyword. For a list of reserved keywords, see [Reserved keywords](ts-limits.md#limits.reserved).   | 
|  current\$1timestamp or now()  |  timestamp  |  Returns current timestamp in UTC. <pre>SELECT current_timestamp</pre> Example result: `2022-07-07 17:42:32.939000000`  This is also a reserved keyword. For a list of reserved keywords, see [Reserved keywords](ts-limits.md#limits.reserved).   | 
|  current\$1timezone()  |  varchar The value will be 'UTC.'  |  Timestream uses UTC timezone for date and time. <pre>SELECT current_timezone()</pre> Example result: `UTC`  | 
|  date(varchar(x)), date(timestamp)  |  date  |  <pre>SELECT date(TIMESTAMP '2022-07-07 17:44:43.771000000')</pre> Example result: `2022-07-07`  | 
|  last\$1day\$1of\$1month(timestamp), last\$1day\$1of\$1month(date)  |  date  |  <pre>SELECT last_day_of_month(TIMESTAMP '2022-07-07 17:44:43.771000000')</pre> Example result: `2022-07-31`  | 
|  from\$1iso8601\$1timestamp(string)  |  timestamp  |  Parses the ISO 8601 timestamp into internal timestamp format. <pre>SELECT from_iso8601_timestamp('2022-06-17T08:04:05.000000000+05:00')</pre> Example result: `2022-06-17 03:04:05.000000000`  | 
|  from\$1iso8601\$1date(string)  |  date  |  Parses the ISO 8601 date string into internal timestamp format for UTC 00:00:00 of the specified date. <pre>SELECT from_iso8601_date('2022-07-17')</pre> Example result: `2022-07-17`  | 
|  to\$1iso8601(timestamp), to\$1iso8601(date)  |  varchar  |  Returns an ISO 8601 formatted string for the input. <pre>SELECT to_iso8601(from_iso8601_date('2022-06-17'))</pre> Example result: `2022-06-17`  | 
|  from\$1milliseconds(bigint)  |  timestamp  |  <pre>SELECT from_milliseconds(1)</pre> Example result: `1970-01-01 00:00:00.001000000`  | 
|  from\$1nanoseconds(bigint)  |  timestamp  |  <pre>select from_nanoseconds(300000001)</pre> Example result: `1970-01-01 00:00:00.300000001`  | 
|  from\$1unixtime(double)  |  timestamp  |  Returns a timestamp which corresponds to the provided unixtime. <pre>SELECT from_unixtime(1)</pre> Example result: `1970-01-01 00:00:01.000000000`  | 
|  localtime  |  time  |  Returns current time in UTC. No parentheses used. <pre>SELECT localtime</pre> Example result: `17:58:22.654000000`  This is also a reserved keyword. For a list of reserved keywords, see [Reserved keywords](ts-limits.md#limits.reserved).   | 
|  localtimestamp  |  timestamp  |  Returns current timestamp in UTC. No parentheses used. <pre>SELECT localtimestamp</pre> Example result: `2022-07-07 17:59:04.368000000`  This is also a reserved keyword. For a list of reserved keywords, see [Reserved keywords](ts-limits.md#limits.reserved).   | 
|  to\$1milliseconds(interval day to second), to\$1milliseconds(timestamp)  |  bigint  |  <pre>SELECT to_milliseconds(INTERVAL '2' DAY + INTERVAL '3' HOUR)</pre> Example result: `183600000` <pre>SELECT to_milliseconds(TIMESTAMP '2022-06-17 17:44:43.771000000')</pre> Example result: `1655487883771`  | 
|  to\$1nanoseconds(interval day to second), to\$1nanoseconds(timestamp)  |  bigint  |  <pre>SELECT to_nanoseconds(INTERVAL '2' DAY + INTERVAL '3' HOUR)</pre> Example result: `183600000000000` <pre>SELECT to_nanoseconds(TIMESTAMP '2022-06-17 17:44:43.771000678')</pre> Example result: `1655487883771000678`  | 
|  to\$1unixtime(timestamp)  |  double  |  Returns unixtime for the provided timestamp. <pre>SELECT to_unixtime('2022-06-17 17:44:43.771000000')</pre> Example result: `1.6554878837710001E9`  | 
|  date\$1trunc(unit, timestamp)  |  timestamp  |  Returns the timestamp truncated to unit, where unit is one of [second, minute, hour, day, week, month, quarter, or year]. <pre>SELECT date_trunc('minute', TIMESTAMP '2022-06-17 17:44:43.771000000')</pre> Example result: `2022-06-17 17:44:00.000000000`  | 

## Interval and duration
<a name="date-time-functions-interval-duration"></a>

Timestream for LiveAnalytics supports the following interval and duration functions for date and time.


| Function | Output data type | Description | 
| --- | --- | --- | 
|  date\$1add(unit, bigint, date), date\$1add(unit, bigint, time), date\$1add(varchar(x), bigint, timestamp)  |  timestamp  |  Adds a bigint of units, where unit is one of [second, minute, hour, day, week, month, quarter, or year]. <pre>SELECT date_add('hour', 9, TIMESTAMP '2022-06-17 00:00:00')</pre> Example result: `2022-06-17 09:00:00.000000000`  | 
|  date\$1diff(unit, date, date) , date\$1diff(unit, time, time) , date\$1diff(unit, timestamp, timestamp)  |  bigint  |  Returns a difference, where unit is one of [second, minute, hour, day, week, month, quarter, or year]. <pre>SELECT date_diff('day', DATE '2020-03-01', DATE '2020-03-02')</pre> Example result: `1`  | 
|  parse\$1duration(string)  |  interval  |  Parses the input string to return an `interval` equivalent. <pre>SELECT parse_duration('42.8ms')</pre> Example result: `0 00:00:00.042800000` <pre>SELECT typeof(parse_duration('42.8ms'))</pre> Example result: `interval day to second`  | 
| bin(timestamp, interval) | timestamp |  Rounds down the `timestamp` parameter's integer value to the nearest multiple of the `interval` parameter's integer value. The meaning of this return value may not be obvious. It is calculated using integer arithmetic first by dividing the timestamp integer by the interval integer and then by multiplying the result by the interval integer. Keeping in mind that a timestamp specifies a UTC point in time as a number of fractions of a second elapsed since the POSIX epoch (January 1, 1970), the return value will seldom align with calendar units. For example, if you specify an interval of 30 days, all the days since the epoch are divided into 30-day increments, and the start of the most recent 30-day increment is returned, which has no relationship to calendar months. Here are some examples: <pre>bin(TIMESTAMP '2022-06-17 10:15:20', 5m)     ==> 2022-06-17 10:15:00.000000000<br />bin(TIMESTAMP '2022-06-17 10:15:20', 1d)     ==> 2022-06-17 00:00:00.000000000<br />bin(TIMESTAMP '2022-06-17 10:15:20', 10day)  ==> 2022-06-17 00:00:00.000000000<br />bin(TIMESTAMP '2022-06-17 10:15:20', 30day)  ==> 2022-05-28 00:00:00.000000000</pre>  | 
|  ago(interval)  |  timestamp  |  Returns the value corresponding to current\$1timestamp `interval`. <pre>SELECT ago(1d)</pre> Example result: `2022-07-06 21:08:53.245000000`  | 
|  interval literals such as 1h, 1d, and 30m  |  interval  |  Interval literals are a convenience for parse\$1duration(string). For example, `1d` is the same as `parse_duration('1d')`. This allows the use of the literals wherever an interval is used. For example, `ago(1d)` and `bin(<timestamp>, 1m)`.  | 

Some interval literals act as shorthand for parse\$1duration. For example, `parse_duration('1day')`, `1day`, `parse_duration('1d')`, and `1d` each return `1 00:00:00.000000000` where the type is `interval day to second`. Space is allowed in the format provided to `parse_duration`. For example `parse_duration('1day')` also returns `00:00:00.000000000`. But `1 day` is not an interval literal.

The units related to `interval day to second` are ns, nanosecond, us, microsecond, ms, millisecond, s, second, m, minute, h, hour, d, and day.

There is also `interval year to month`. The units related to interval year to month are y, year, and month. For example, `SELECT 1year` returns `1-0`. `SELECT 12month` also returns `1-0`. `SELECT 8month` returns `0-8`.

Although the unit of `quarter` is also available for some functions such as `date_trunc` and `date_add`, `quarter` is not available as part of an interval literal.

## Formatting and parsing
<a name="date-time-functions-formatting-parsing"></a>

Timestream for LiveAnalytics supports the following formatting and parsing functions for date and time.


| Function | Output data type | Description | 
| --- | --- | --- | 
|  date\$1format(timestamp, varchar(x))  |  varchar  |  For more information about the format specifiers used by this function, see [https://trino.io/docs/current/functions/datetime.html\$1mysql-date-functions](https://trino.io/docs/current/functions/datetime.html#mysql-date-functions) <pre>SELECT date_format(TIMESTAMP '2019-10-20 10:20:20', '%Y-%m-%d %H:%i:%s')</pre> Example result: `2019-10-20 10:20:20`  | 
|  date\$1parse(varchar(x), varchar(y))  |  timestamp  |  For more information about the format specifiers used by this function, see [https://trino.io/docs/current/functions/datetime.html\$1mysql-date-functions](https://trino.io/docs/current/functions/datetime.html#mysql-date-functions) <pre>SELECT date_parse('2019-10-20 10:20:20', '%Y-%m-%d %H:%i:%s')</pre> Example result: `2019-10-20 10:20:20.000000000`  | 
|  format\$1datetime(timestamp, varchar(x))  |  varchar  |  For more information about the format string used by this function, see [http://joda-time.sourceforge.net/apidocs/org/joda/time/format/DateTimeFormat.html](http://joda-time.sourceforge.net/apidocs/org/joda/time/format/DateTimeFormat.html) <pre>SELECT format_datetime(parse_datetime('1968-01-13 12', 'yyyy-MM-dd HH'), 'yyyy-MM-dd HH')</pre> Example result: `1968-01-13 12`  | 
|  parse\$1datetime(varchar(x), varchar(y))  |  timestamp  |  For more information about the format string used by this function, see [http://joda-time.sourceforge.net/apidocs/org/joda/time/format/DateTimeFormat.html](http://joda-time.sourceforge.net/apidocs/org/joda/time/format/DateTimeFormat.html) <pre>SELECT parse_datetime('2019-12-29 10:10 PST', 'uuuu-LL-dd HH:mm z')</pre> Example result: `2019-12-29 18:10:00.000000000`  | 

## Extraction
<a name="date-time-functions-extraction"></a>

Timestream for LiveAnalytics supports the following extraction functions for date and time. The extract function is the basis for the remaining convenience functions.


| Function | Output data type | Description | 
| --- | --- | --- | 
|  extract  |  bigint  |  Extracts a field from a timestamp, where field is one of [YEAR, QUARTER, MONTH, WEEK, DAY, DAY\$1OF\$1MONTH, DAY\$1OF\$1WEEK, DOW, DAY\$1OF\$1YEAR, DOY, YEAR\$1OF\$1WEEK, YOW, HOUR, MINUTE, or SECOND]. <pre>SELECT extract(YEAR FROM '2019-10-12 23:10:34.000000000')</pre> Example result: `2019`  | 
|  day(timestamp), day(date), day(interval day to second)  |  bigint  |  <pre>SELECT day('2019-10-12 23:10:34.000000000')</pre> Example result: `12`  | 
|  day\$1of\$1month(timestamp), day\$1of\$1month(date), day\$1of\$1month(interval day to second)  |  bigint  |  <pre>SELECT day_of_month('2019-10-12 23:10:34.000000000')</pre> Example result: `12`  | 
|  day\$1of\$1week(timestamp), day\$1of\$1week(date)  |  bigint  |  <pre>SELECT day_of_week('2019-10-12 23:10:34.000000000')</pre> Example result: `6`  | 
|  day\$1of\$1year(timestamp), day\$1of\$1year(date)  |  bigint  |  <pre>SELECT day_of_year('2019-10-12 23:10:34.000000000')</pre> Example result: `285`  | 
|  dow(timestamp), dow(date)  |  bigint  |  Alias for day\$1of\$1week  | 
|  doy(timestamp), doy(date)  |  bigint  |  Alias for day\$1of\$1year  | 
|  hour(timestamp), hour(time), hour(interval day to second)  |  bigint  |  <pre>SELECT hour('2019-10-12 23:10:34.000000000')</pre> Example result: `23`  | 
|  millisecond(timestamp), millisecond(time), millisecond(interval day to second)  |  bigint  |  <pre>SELECT millisecond('2019-10-12 23:10:34.000000000')</pre> Example result: `0`  | 
|  minute(timestamp), minute(time), minute(interval day to second)  |  bigint  |  <pre>SELECT minute('2019-10-12 23:10:34.000000000')</pre> Example result: `10`  | 
|  month(timestamp), month(date), month(interval year to month)  |  bigint  |  <pre>SELECT month('2019-10-12 23:10:34.000000000')</pre> Example result: `10`  | 
|  nanosecond(timestamp), nanosecond(time), nanosecond(interval day to second)  |  bigint  |  <pre>SELECT nanosecond(current_timestamp)</pre> Example result: `162000000`  | 
|  quarter(timestamp), quarter(date)  |  bigint  |  <pre>SELECT quarter('2019-10-12 23:10:34.000000000')</pre> Example result: `4`  | 
|  second(timestamp), second(time), second(interval day to second)  |  bigint  |  <pre>SELECT second('2019-10-12 23:10:34.000000000')</pre> Example result: `34`  | 
|  week(timestamp), week(date)  |  bigint  |  <pre>SELECT week('2019-10-12 23:10:34.000000000')</pre> Example result: `41`  | 
|  week\$1of\$1year(timestamp), week\$1of\$1year(date)  |  bigint  |  Alias for week  | 
|  year(timestamp), year(date), year(interval year to month)  |  bigint  |  <pre>SELECT year('2019-10-12 23:10:34.000000000')</pre> Example result: `2019`  | 
|  year\$1of\$1week(timestamp), year\$1of\$1week(date)  |  bigint  |  <pre>SELECT year_of_week('2019-10-12 23:10:34.000000000')</pre> Example result: `2019`  | 
|  yow(timestamp), yow(date)  |  bigint  |  Alias for year\$1of\$1week  | 

# Aggregate functions
<a name="aggregate-functions"></a>

Timestream for LiveAnalytics supports the following aggregate functions.


| Function | Output data type | Description | 
| --- | --- | --- | 
|  arbitrary(x)  |  [same as input]  |  Returns an arbitrary non-null value of x, if one exists. <pre>SELECT arbitrary(t.c) FROM (VALUES 1, 2, 3, 4) AS t(c)</pre> Example result: `1`  | 
|  array\$1agg(x)  |  array<[same as input]  |  Returns an array created from the input x elements. <pre>SELECT array_agg(t.c) FROM (VALUES 1, 2, 3, 4) AS t(c)</pre> Example result: `[ 1,2,3,4 ]`  | 
|  avg(x)  |  double  |  Returns the average (arithmetic mean) of all input values. <pre>SELECT avg(t.c) FROM (VALUES 1, 2, 3, 4) AS t(c)</pre> Example result: `2.5`  | 
|  bool\$1and(boolean) every(boolean)   |  boolean  |  Returns TRUE if every input value is TRUE, otherwise FALSE. <pre>SELECT bool_and(t.c) FROM (VALUES true, true, false, true) AS t(c)</pre> Example result: `false`  | 
|  bool\$1or(boolean)  |  boolean  |  Returns TRUE if any input value is TRUE, otherwise FALSE. <pre>SELECT bool_or(t.c) FROM (VALUES true, true, false, true) AS t(c)</pre> Example result: `true`  | 
|  count(\$1) count(x)  |  bigint  |  count(\$1) returns the number of input rows. count(x) returns the number of non-null input values. <pre>SELECT count(t.c) FROM (VALUES true, true, false, true) AS t(c)</pre> Example result: `4`  | 
|  count\$1if(x)   |  bigint  |  Returns the number of TRUE input values.  <pre>SELECT count_if(t.c) FROM (VALUES true, true, false, true) AS t(c)</pre> Example result: `3`  | 
|  geometric\$1mean(x)  |  double  |  Returns the geometric mean of all input values. <pre>SELECT geometric_mean(t.c) FROM (VALUES 1, 2, 3, 4) AS t(c)</pre> Example result: `2.213363839400643`  | 
|  max\$1by(x, y)   |  [same as x]  |  Returns the value of x associated with the maximum value of y over all input values. <pre>SELECT max_by(t.c1, t.c2) FROM (VALUES (('a', 1)), (('b', 2)), (('c', 3)), (('d', 4))) AS t(c1, c2)</pre> Example result: `d`  | 
|  max\$1by(x, y, n)   |  array<[same as x]>  |  Returns n values of x associated with the n largest of all input values of y in descending order of y. <pre>SELECT max_by(t.c1, t.c2, 2) FROM (VALUES (('a', 1)), (('b', 2)), (('c', 3)), (('d', 4))) AS t(c1, c2)</pre> Example result: `[ d,c ]`  | 
|  min\$1by(x, y)  |  [same as x]  |  Returns the value of x associated with the minimum value of y over all input values. <pre>SELECT min_by(t.c1, t.c2) FROM (VALUES (('a', 1)), (('b', 2)), (('c', 3)), (('d', 4))) AS t(c1, c2)</pre> Example result: `a`  | 
|  min\$1by(x, y, n)  |  array<[same as x]>  |  Returns n values of x associated with the n smallest of all input values of y in ascending order of y. <pre>SELECT min_by(t.c1, t.c2, 2) FROM (VALUES (('a', 1)), (('b', 2)), (('c', 3)), (('d', 4))) AS t(c1, c2)</pre> Example result: `[ a,b ]`  | 
|  max(x)  |  [same as input]  |  Returns the maximum value of all input values. <pre>SELECT max(t.c) FROM (VALUES 1, 2, 3, 4) AS t(c)</pre> Example result: `4`  | 
|  max(x, n)  |  array<[same as x]>  |  Returns n largest values of all input values of x. <pre>SELECT max(t.c, 2) FROM (VALUES 1, 2, 3, 4) AS t(c)</pre> Example result: `[ 4,3 ]`  | 
|  min(x)  |  [same as input]  |  Returns the minimum value of all input values. <pre>SELECT min(t.c) FROM (VALUES 1, 2, 3, 4) AS t(c)</pre> Example result: `1`  | 
|  min(x, n)  |  array<[same as x]>  |  Returns n smallest values of all input values of x. <pre>SELECT min(t.c, 2) FROM (VALUES 1, 2, 3, 4) AS t(c)</pre> Example result: `[ 1,2 ]`  | 
|  sum(x)   |  [same as input]  |  Returns the sum of all input values. <pre>SELECT sum(t.c) FROM (VALUES 1, 2, 3, 4) AS t(c)</pre> Example result: `10`  | 
|  bitwise\$1and\$1agg(x)  |  bigint  |  Returns the bitwise AND of all input values in 2s complement representation. <pre>SELECT bitwise_and_agg(t.c) FROM (VALUES 1, -3) AS t(c)</pre> Example result: `1`  | 
|  bitwise\$1or\$1agg(x)  |  bigint  |  Returns the bitwise OR of all input values in 2s complement representation. <pre>SELECT bitwise_or_agg(t.c) FROM (VALUES 1, -3) AS t(c)</pre> Example result: `-3`  | 
|  approx\$1distinct(x)   |  bigint  |  Returns the approximate number of distinct input values. This function provides an approximation of count(DISTINCT x). Zero is returned if all input values are null. This function should produce a standard error of 2.3%, which is the standard deviation of the (approximately normal) error distribution over all possible sets. It does not guarantee an upper bound on the error for any specific input set. <pre>SELECT approx_distinct(t.c) FROM (VALUES 1, 2, 3, 4, 8) AS t(c)</pre> Example result: `5`  | 
|  approx\$1distinct(x, e)  |  bigint  |  Returns the approximate number of distinct input values. This function provides an approximation of count(DISTINCT x). Zero is returned if all input values are null. This function should produce a standard error of no more than e, which is the standard deviation of the (approximately normal) error distribution over all possible sets. It does not guarantee an upper bound on the error for any specific input set. The current implementation of this function requires that e be in the range of [0.0040625, 0.26000]. <pre>SELECT approx_distinct(t.c, 0.2) FROM (VALUES 1, 2, 3, 4, 8) AS t(c)</pre> Example result: `5`  | 
|  approx\$1percentile(x, percentage)   |  [same as x]  |  Returns the approximate percentile for all input values of x at the given percentage. The value of percentage must be between zero and one and must be constant for all input rows. <pre>SELECT approx_percentile(t.c, 0.4) FROM (VALUES 1, 2, 3, 4) AS t(c)</pre> Example result: `2`  | 
|  approx\$1percentile(x, percentages)   |  array<[same as x]>  |  Returns the approximate percentile for all input values of x at each of the specified percentages. Each element of the percentages array must be between zero and one, and the array must be constant for all input rows. <pre>SELECT approx_percentile(t.c, ARRAY[0.1, 0.8, 0.8]) FROM (VALUES 1, 2, 3, 4) AS t(c)</pre> Example result: `[ 1,4,4 ]`  | 
|  approx\$1percentile(x, w, percentage)   |  [same as x]  |  Returns the approximate weighed percentile for all input values of x using the per-item weight w at the percentage p. The weight must be an integer value of at least one. It is effectively a replication count for the value x in the percentile set. The value of p must be between zero and one and must be constant for all input rows. <pre>SELECT approx_percentile(t.c, 1, 0.1) FROM (VALUES 1, 2, 3, 4) AS t(c)</pre> Example result: `1`  | 
|  approx\$1percentile(x, w, percentages)   |  array<[same as x]>  |  Returns the approximate weighed percentile for all input values of x using the per-item weight w at each of the given percentages specified in the array. The weight must be an integer value of at least one. It is effectively a replication count for the value x in the percentile set. Each element of the array must be between zero and one, and the array must be constant for all input rows. <pre>SELECT approx_percentile(t.c, 1, ARRAY[0.1, 0.8, 0.8]) FROM (VALUES 1, 2, 3, 4) AS t(c)</pre> Example result: `[ 1,4,4 ]`  | 
|  approx\$1percentile(x, w, percentage, accuracy)  |  [same as x]  |  Returns the approximate weighed percentile for all input values of x using the per-item weight w at the percentage p, with a maximum rank error of accuracy. The weight must be an integer value of at least one. It is effectively a replication count for the value x in the percentile set. The value of p must be between zero and one and must be constant for all input rows. The accuracy must be a value greater than zero and less than one, and it must be constant for all input rows. <pre>SELECT approx_percentile(t.c, 1, 0.1, 0.5) FROM (VALUES 1, 2, 3, 4) AS t(c)</pre> Example result: `1`  | 
|  corr(y, x)  |  double  |  Returns correlation coefficient of input values. <pre>SELECT corr(t.c1, t.c2) FROM (VALUES ((1, 1)), ((2, 2)), ((3, 3)), ((4, 4))) AS t(c1, c2)</pre> Example result: `1.0`  | 
|  covar\$1pop(y, x)  |  double  |  Returns the population covariance of input values. <pre>SELECT covar_pop(t.c1, t.c2) FROM (VALUES ((1, 1)), ((2, 2)), ((3, 3)), ((4, 4))) AS t(c1, c2)</pre> Example result: `1.25`  | 
|  covar\$1samp(y, x)   |  double  |  Returns the sample covariance of input values. <pre>SELECT covar_samp(t.c1, t.c2) FROM (VALUES ((1, 1)), ((2, 2)), ((3, 3)), ((4, 4))) AS t(c1, c2)</pre> Example result: `1.6666666666666667`  | 
|  regr\$1intercept(y, x)  |  double  |  Returns linear regression intercept of input values. y is the dependent value. x is the independent value. <pre>SELECT regr_intercept(t.c1, t.c2) FROM (VALUES ((1, 1)), ((2, 2)), ((3, 3)), ((4, 4))) AS t(c1, c2)</pre> Example result: `0.0`  | 
|  regr\$1slope(y, x)  |  double  |  Returns linear regression slope of input values. y is the dependent value. x is the independent value. <pre>SELECT regr_slope(t.c1, t.c2) FROM (VALUES ((1, 1)), ((2, 2)), ((3, 3)), ((4, 4))) AS t(c1, c2)</pre> Example result: `1.0`  | 
|  skewness(x)  |  double  |  Returns the skewness of all input values. <pre>SELECT skewness(t.c1) FROM (VALUES 1, 2, 3, 4, 8) AS t(c1)</pre> Example result: `0.8978957037987335`  | 
|  stddev\$1pop(x)  |  double  |  Returns the population standard deviation of all input values. <pre>SELECT stddev_pop(t.c1) FROM (VALUES 1, 2, 3, 4, 8) AS t(c1)</pre> Example result: `2.4166091947189146`  | 
|  stddev\$1samp(x) stddev(x)  |  double  |  Returns the sample standard deviation of all input values. <pre>SELECT stddev_samp(t.c1) FROM (VALUES 1, 2, 3, 4, 8) AS t(c1)</pre> Example result: `2.701851217221259`  | 
|  var\$1pop(x)   |  double  |  Returns the population variance of all input values. <pre>SELECT var_pop(t.c1) FROM (VALUES 1, 2, 3, 4, 8) AS t(c1)</pre> Example result: `5.840000000000001`  | 
|  var\$1samp(x) variance(x)   |  double  |  Returns the sample variance of all input values. <pre>SELECT var_samp(t.c1) FROM (VALUES 1, 2, 3, 4, 8) AS t(c1)</pre> Example result: `7.300000000000001`  | 

# Window functions
<a name="window-functions"></a>

Window functions perform calculations across rows of the query result. They run after the HAVING clause but before the ORDER BY clause. Invoking a window function requires special syntax using the OVER clause to specify the window. A window has three components:
+ The partition specification, which separates the input rows into different partitions. This is analogous to how the GROUP BY clause separates rows into different groups for aggregate functions.
+ The ordering specification, which determines the order in which input rows will be processed by the window function.
+ The window frame, which specifies a sliding window of rows to be processed by the function for a given row. If the frame is not specified, it defaults to RANGE UNBOUNDED PRECEDING, which is the same as RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW. This frame contains all rows from the start of the partition up to the last peer of the current row.

All Aggregate Functions can be used as window functions by adding the OVER clause. The aggregate function is computed for each row over the rows within the current row's window frame. In addition to aggregate functions, Timestream for LiveAnalytics supports the following ranking and value functions.


| Function | Output data type | Description | 
| --- | --- | --- | 
|  cume\$1dist()  |  bigint  |  Returns the cumulative distribution of a value in a group of values. The result is the number of rows preceding or peer with the row in the window ordering of the window partition divided by the total number of rows in the window partition. Thus, any tie values in the ordering will evaluate to the same distribution value.  | 
|  dense\$1rank()  |  bigint  |  Returns the rank of a value in a group of values. This is similar to rank(), except that tie values do not produce gaps in the sequence.  | 
|  ntile(n)  |  bigint  |  Divides the rows for each window partition into n buckets ranging from 1 to at most n. Bucket values will differ by at most 1. If the number of rows in the partition does not divide evenly into the number of buckets, then the remainder values are distributed one per bucket, starting with the first bucket.  | 
|  percent\$1rank()  |  double  |  Returns the percentage ranking of a value in group of values. The result is (r - 1) / (n - 1) where r is the rank() of the row and n is the total number of rows in the window partition.  | 
|  rank()  |  bigint  |  Returns the rank of a value in a group of values. The rank is one plus the number of rows preceding the row that are not peer with the row. Thus, tie values in the ordering will produce gaps in the sequence. The ranking is performed for each window partition.  | 
|  row\$1number()  |  bigint  |  Returns a unique, sequential number for each row, starting with one, according to the ordering of rows within the window partition.  | 
|  first\$1value(x)  |  [same as input]  |  Returns the first value of the window. This function is scoped to the window frame. The function takes an expression or target as its parameter.  | 
|  last\$1value(x)  |  [same as input]  |  Returns the last value of the window. This function is scoped to the window frame. The function takes an expression or target as its parameter.  | 
|  nth\$1value(x, offset)  |  [same as input]  |  Returns the value at the specified offset from beginning the window. Offsets start at 1. The offset can be any scalar expression. If the offset is null or greater than the number of values in the window, null is returned. It is an error for the offset to be zero or negative. The function takes an expression or target as its first parameter.  | 
|  lead(x[, offset[, default\$1value]])  |  [same as input]  |  Returns the value at offset rows after the current row in the window. Offsets start at 0, which is the current row. The offset can be any scalar expression. The default offset is 1. If the offset is null or larger than the window, the default\$1value is returned, or if it is not specified null is returned. The function takes an expression or target as its first parameter.  | 
|  lag(x[, offset[, default\$1value]])  |  [same as input]  |  Returns the value at offset rows before the current row in the window Offsets start at 0, which is the current row. The offset can be any scalar expression. The default offset is 1. If the offset is null or larger than the window, the default\$1value is returned, or if it is not specified null is returned. The function takes an expression or target as its first parameter.  | 

# Sample queries
<a name="sample-queries"></a>

This section includes example use cases of Timestream for LiveAnalytics's query language.

**Topics**
+ [Simple queries](sample-queries.basic-scenarios.md)
+ [Queries with time series functions](sample-queries.devops-scenarios.md)
+ [Queries with aggregate functions](sample-queries.iot-scenarios.md)

# Simple queries
<a name="sample-queries.basic-scenarios"></a>

The following gets the 10 most recently added data points for a table.

```
SELECT * FROM <database_name>.<table_name>
ORDER BY time DESC
LIMIT 10
```

The following gets the 5 oldest data points for a specific measure.

```
SELECT * FROM <database_name>.<table_name>
WHERE measure_name = '<measure_name>'
ORDER BY time ASC
LIMIT 5
```

The following works with nanosecond granularity timestamps.

```
SELECT now() AS time_now
, now() - (INTERVAL '12' HOUR) AS twelve_hour_earlier -- Compatibility with ANSI SQL 
, now() - 12h AS also_twelve_hour_earlier -- Convenient time interval literals
, ago(12h) AS twelve_hours_ago -- More convenience with time functionality
, bin(now(), 10m) AS time_binned -- Convenient time binning support
, ago(50ns) AS fifty_ns_ago -- Nanosecond support
, now() + (1h + 50ns) AS hour_fifty_ns_future
```

Measure values for multi-measure records are identified by column name. Measure values for single-measure records are identified by `measure_value::<data_type>`, where `<data_type>` is one of `double`, `bigint`, `boolean`, or `varchar` as described in [Supported data types](supported-data-types.md). For more information about how measure values are modeled, see [Single table vs. multiple tables](https://docs.aws.amazon.com/timestream/latest/developerguide/data-modeling.html#data-modeling-multiVsinglerecords).

The following retrieves values for a measure called `speed` from multi-measure records with a `measure_name` of `IoTMulti-stats`.

```
SELECT speed FROM <database_name>.<table_name> where measure_name = 'IoTMulti-stats'
```

The following retrieves `double` values from single-measure records with a `measure_name` of `load`.

```
SELECT measure_value::double FROM <database_name>.<table_name> WHERE measure_name = 'load'
```

# Queries with time series functions
<a name="sample-queries.devops-scenarios"></a>

**Topics**
+ [Example dataset and queries](#sample-queries.devops-scenarios.example)

## Example dataset and queries
<a name="sample-queries.devops-scenarios.example"></a>

You can use Timestream for LiveAnalytics to understand and improve the performance and availability of your services and applications. Below is an example table and sample queries run on that table. 

The table `ec2_metrics` stores telemetry data, such as CPU utilization and other metrics from EC2 instances. You can view the table below.


| Time | region | az | Hostname | measure\$1name | measure\$1value::double | measure\$1value::bigint | 
| --- | --- | --- | --- | --- | --- | --- | 
|  2019-12-04 19:00:00.000000000  |  us-east-1  |  us-east-1a  |  frontend01  |  cpu\$1utilization  |  35.1  |  null  | 
|  2019-12-04 19:00:00.000000000  |  us-east-1  |  us-east-1a  |  frontend01  |  memory\$1utilization  |  55.3  |  null  | 
|  2019-12-04 19:00:00.000000000  |  us-east-1  |  us-east-1a  |  frontend01  |  network\$1bytes\$1in  |  null  |  1,500  | 
|  2019-12-04 19:00:00.000000000  |  us-east-1  |  us-east-1a  |  frontend01  |  network\$1bytes\$1out  |  null  |  6,700  | 
|  2019-12-04 19:00:00.000000000  |  us-east-1  |  us-east-1b  |  frontend02  |  cpu\$1utilization  |  38.5  |  null  | 
|  2019-12-04 19:00:00.000000000  |  us-east-1  |  us-east-1b  |  frontend02  |  memory\$1utilization  |  58.4  |  null  | 
|  2019-12-04 19:00:00.000000000  |  us-east-1  |  us-east-1b  |  frontend02  |  network\$1bytes\$1in  |  null  |  23,000  | 
|  2019-12-04 19:00:00.000000000  |  us-east-1  |  us-east-1b  |  frontend02  |  network\$1bytes\$1out  |  null  |  12,000  | 
|  2019-12-04 19:00:00.000000000  |  us-east-1  |  us-east-1c  |  frontend03  |  cpu\$1utilization  |  45.0  |  null  | 
|  2019-12-04 19:00:00.000000000  |  us-east-1  |  us-east-1c  |  frontend03  |  memory\$1utilization  |  65.8  |  null  | 
|  2019-12-04 19:00:00.000000000  |  us-east-1  |  us-east-1c  |  frontend03  |  network\$1bytes\$1in  |  null  |  15,000  | 
|  2019-12-04 19:00:00.000000000  |  us-east-1  |  us-east-1c  |  frontend03  |  network\$1bytes\$1out  |  null  |  836,000  | 
|  2019-12-04 19:00:05.000000000  |  us-east-1  |  us-east-1a  |  frontend01  |  cpu\$1utilization  |  55.2  |  null  | 
|  2019-12-04 19:00:05.000000000  |  us-east-1  |  us-east-1a  |  frontend01  |  memory\$1utilization  |  75.0  |  null  | 
|  2019-12-04 19:00:05.000000000  |  us-east-1  |  us-east-1a  |  frontend01  |  network\$1bytes\$1in  |  null  |  1,245  | 
|  2019-12-04 19:00:05.000000000  |  us-east-1  |  us-east-1a  |  frontend01  |  network\$1bytes\$1out  |  null  |  68,432  | 
|  2019-12-04 19:00:08.000000000  |  us-east-1  |  us-east-1b  |  frontend02  |  cpu\$1utilization  |  65.6  |  null  | 
|  2019-12-04 19:00:08.000000000  |  us-east-1  |  us-east-1b  |  frontend02  |  memory\$1utilization  |  85.3  |  null  | 
|  2019-12-04 19:00:08.000000000  |  us-east-1  |  us-east-1b  |  frontend02  |  network\$1bytes\$1in  |  null  |  1,245  | 
|  2019-12-04 19:00:08.000000000  |  us-east-1  |  us-east-1b  |  frontend02  |  network\$1bytes\$1out  |  null  |  68,432  | 
|  2019-12-04 19:00:20.000000000  |  us-east-1  |  us-east-1c  |  frontend03  |  cpu\$1utilization  |  12.1  |  null  | 
|  2019-12-04 19:00:20.000000000  |  us-east-1  |  us-east-1c  |  frontend03  |  memory\$1utilization  |  32.0  |  null  | 
|  2019-12-04 19:00:20.000000000  |  us-east-1  |  us-east-1c  |  frontend03  |  network\$1bytes\$1in  |  null  |  1,400  | 
|  2019-12-04 19:00:20.000000000  |  us-east-1  |  us-east-1c  |  frontend03  |  network\$1bytes\$1out  |  null  |  345  | 
|  2019-12-04 19:00:10.000000000  |  us-east-1  |  us-east-1a  |  frontend01  |  cpu\$1utilization  |  15.3  |  null  | 
|  2019-12-04 19:00:10.000000000  |  us-east-1  |  us-east-1a  |  frontend01  |  memory\$1utilization  |  35.4  |  null  | 
|  2019-12-04 19:00:10.000000000  |  us-east-1  |  us-east-1a  |  frontend01  |  network\$1bytes\$1in  |  null  |  23  | 
|  2019-12-04 19:00:10.000000000  |  us-east-1  |  us-east-1a  |  frontend01  |  network\$1bytes\$1out  |  null  |  0  | 
|  2019-12-04 19:00:16.000000000  |  us-east-1  |  us-east-1b  |  frontend02  |  cpu\$1utilization  |  44.0  |  null  | 
|  2019-12-04 19:00:16.000000000  |  us-east-1  |  us-east-1b  |  frontend02  |  memory\$1utilization  |  64.2  |  null  | 
|  2019-12-04 19:00:16.000000000  |  us-east-1  |  us-east-1b  |  frontend02  |  network\$1bytes\$1in  |  null  |  1,450  | 
|  2019-12-04 19:00:16.000000000  |  us-east-1  |  us-east-1b  |  frontend02  |  network\$1bytes\$1out  |  null  |  200  | 
|  2019-12-04 19:00:40.000000000  |  us-east-1  |  us-east-1c  |  frontend03  |  cpu\$1utilization  |  66.4  |  null  | 
|  2019-12-04 19:00:40.000000000  |  us-east-1  |  us-east-1c  |  frontend03  |  memory\$1utilization  |  86.3  |  null  | 
|  2019-12-04 19:00:40.000000000  |  us-east-1  |  us-east-1c  |  frontend03  |  network\$1bytes\$1in  |  null  |  300  | 
|  2019-12-04 19:00:40.000000000  |  us-east-1  |  us-east-1c  |  frontend03  |  network\$1bytes\$1out  |  null  |  423  | 

Find the average, p90, p95, and p99 CPU utilization for a specific EC2 host over the past 2 hours:

```
SELECT region, az, hostname, BIN(time, 15s) AS binned_timestamp,
    ROUND(AVG(measure_value::double), 2) AS avg_cpu_utilization,
    ROUND(APPROX_PERCENTILE(measure_value::double, 0.9), 2) AS p90_cpu_utilization,
    ROUND(APPROX_PERCENTILE(measure_value::double, 0.95), 2) AS p95_cpu_utilization,
    ROUND(APPROX_PERCENTILE(measure_value::double, 0.99), 2) AS p99_cpu_utilization
FROM "sampleDB".DevOps
WHERE measure_name = 'cpu_utilization'
    AND hostname = 'host-Hovjv'
    AND time > ago(2h)
GROUP BY region, hostname, az, BIN(time, 15s)
ORDER BY binned_timestamp ASC
```

Identify EC2 hosts with CPU utilization that is higher by 10 % or more compared to the average CPU utilization of the entire fleet for the past 2 hours:

```
WITH avg_fleet_utilization AS (
    SELECT COUNT(DISTINCT hostname) AS total_host_count, AVG(measure_value::double) AS fleet_avg_cpu_utilization
    FROM "sampleDB".DevOps
    WHERE measure_name = 'cpu_utilization'
        AND time > ago(2h)
), avg_per_host_cpu AS (
    SELECT region, az, hostname, AVG(measure_value::double) AS avg_cpu_utilization
    FROM "sampleDB".DevOps
    WHERE measure_name = 'cpu_utilization'
        AND time > ago(2h)
    GROUP BY region, az, hostname
)
SELECT region, az, hostname, avg_cpu_utilization, fleet_avg_cpu_utilization
FROM avg_fleet_utilization, avg_per_host_cpu
WHERE avg_cpu_utilization > 1.1 * fleet_avg_cpu_utilization
ORDER BY avg_cpu_utilization DESC
```

Find the average CPU utilization binned at 30 second intervals for a specific EC2 host over the past 2 hours:

```
SELECT BIN(time, 30s) AS binned_timestamp, ROUND(AVG(measure_value::double), 2) AS avg_cpu_utilization
FROM "sampleDB".DevOps
WHERE measure_name = 'cpu_utilization'
    AND hostname = 'host-Hovjv'
    AND time > ago(2h)
GROUP BY hostname, BIN(time, 30s)
ORDER BY binned_timestamp ASC
```

Find the average CPU utilization binned at 30 second intervals for a specific EC2 host over the past 2 hours, filling in the missing values using linear interpolation:

```
WITH binned_timeseries AS (
    SELECT hostname, BIN(time, 30s) AS binned_timestamp, ROUND(AVG(measure_value::double), 2) AS avg_cpu_utilization
    FROM "sampleDB".DevOps
    WHERE measure_name = 'cpu_utilization'
        AND hostname = 'host-Hovjv'
        AND time > ago(2h)
    GROUP BY hostname, BIN(time, 30s)
), interpolated_timeseries AS (
    SELECT hostname,
        INTERPOLATE_LINEAR(
            CREATE_TIME_SERIES(binned_timestamp, avg_cpu_utilization),
                SEQUENCE(min(binned_timestamp), max(binned_timestamp), 15s)) AS interpolated_avg_cpu_utilization
    FROM binned_timeseries
    GROUP BY hostname
)
SELECT time, ROUND(value, 2) AS interpolated_cpu
FROM interpolated_timeseries
CROSS JOIN UNNEST(interpolated_avg_cpu_utilization)
```

Find the average CPU utilization binned at 30 second intervals for a specific EC2 host over the past 2 hours, filling in the missing values using interpolation based on the last observation carried forward:

```
WITH binned_timeseries AS (
    SELECT hostname, BIN(time, 30s) AS binned_timestamp, ROUND(AVG(measure_value::double), 2) AS avg_cpu_utilization
    FROM "sampleDB".DevOps
    WHERE measure_name = 'cpu_utilization'
        AND hostname = 'host-Hovjv'
        AND time > ago(2h)
    GROUP BY hostname, BIN(time, 30s)
), interpolated_timeseries AS (
    SELECT hostname,
        INTERPOLATE_LOCF(
            CREATE_TIME_SERIES(binned_timestamp, avg_cpu_utilization),
                SEQUENCE(min(binned_timestamp), max(binned_timestamp), 15s)) AS interpolated_avg_cpu_utilization
    FROM binned_timeseries
    GROUP BY hostname
)
SELECT time, ROUND(value, 2) AS interpolated_cpu
FROM interpolated_timeseries
CROSS JOIN UNNEST(interpolated_avg_cpu_utilization)
```

# Queries with aggregate functions
<a name="sample-queries.iot-scenarios"></a>

Below is an example IoT scenario example data set to illustrate queries with aggregate functions.

**Topics**
+ [Example data](#sample-queries.iot-scenarios.example-data)
+ [Example queries](#sample-queries.iot-scenarios.example-queries)

## Example data
<a name="sample-queries.iot-scenarios.example-data"></a>

Timestream enables you to store and analyze IoT sensor data such as the location, fuel consumption, speed, and load capacity of one or more fleets of trucks to enable effective fleet management. Below is the schema and some of the data of a table iot\$1trucks that stores telemetry such as location, fuel consumption, speed, and load capacity of trucks.


| Time | truck\$1id | Make | Model | Fleet | fuel\$1capacity | load\$1capacity | measure\$1name | measure\$1value::double | measure\$1value::varchar | 
| --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | 
|  2019-12-04 19:00:00.000000000  |  123456781  |  GMC  |  Astro  |  Alpha  |  100  |  500  |  fuel\$1reading  |  65.2  |  null  | 
|  2019-12-04 19:00:00.000000000  |  123456781  |  GMC  |  Astro  |  Alpha  |  100  |  500  |  load  |  400.0  |  null  | 
|  2019-12-04 19:00:00.000000000  |  123456781  |  GMC  |  Astro  |  Alpha  |  100  |  500  |  speed  |  90.2  |  null  | 
|  2019-12-04 19:00:00.000000000  |  123456781  |  GMC  |  Astro  |  Alpha  |  100  |  500  |  location  |  null  |  47.6062 N, 122.3321 W  | 
|  2019-12-04 19:00:00.000000000  |  123456782  |  Kenworth  |  W900  |  Alpha  |  150  |  1000  |  fuel\$1reading  |  10.1  |  null  | 
|  2019-12-04 19:00:00.000000000  |  123456782  |  Kenworth  |  W900  |  Alpha  |  150  |  1000  |  load  |  950.3  |  null  | 
|  2019-12-04 19:00:00.000000000  |  123456782  |  Kenworth  |  W900  |  Alpha  |  150  |  1000  |  speed  |  50.8  |  null  | 
|  2019-12-04 19:00:00.000000000  |  123456782  |  Kenworth  |  W900  |  Alpha  |  150  |  1000  |  location  |  null  |  40.7128 degrees N, 74.0060 degrees W  | 

## Example queries
<a name="sample-queries.iot-scenarios.example-queries"></a>

Get a list of all the sensor attributes and values being monitored for each truck in the fleet.

```
SELECT
    truck_id,
    fleet,
    fuel_capacity,
    model,
    load_capacity,
    make,
    measure_name
FROM "sampleDB".IoT
GROUP BY truck_id, fleet, fuel_capacity, model, load_capacity, make, measure_name
```

Get the most recent fuel reading of each truck in the fleet in the past 24 hours.

```
WITH latest_recorded_time AS (
    SELECT
        truck_id,
        max(time) as latest_time
    FROM "sampleDB".IoT
    WHERE measure_name = 'fuel-reading'
    AND time >= ago(24h)
    GROUP BY truck_id
)
SELECT
    b.truck_id,
    b.fleet,
    b.make,
    b.model,
    b.time,
    b.measure_value::double as last_reported_fuel_reading
FROM
latest_recorded_time a INNER JOIN "sampleDB".IoT b
ON a.truck_id = b.truck_id AND b.time = a.latest_time
WHERE b.measure_name = 'fuel-reading'
AND b.time > ago(24h)
ORDER BY b.truck_id
```

Identify trucks that have been running on low fuel(less than 10 %) in the past 48 hours:

```
WITH low_fuel_trucks AS (
    SELECT time, truck_id, fleet, make, model, (measure_value::double/cast(fuel_capacity as double)*100) AS fuel_pct
    FROM "sampleDB".IoT
    WHERE time >= ago(48h)
    AND (measure_value::double/cast(fuel_capacity as double)*100) < 10
    AND measure_name = 'fuel-reading'
),
other_trucks AS (
SELECT time, truck_id, (measure_value::double/cast(fuel_capacity as double)*100) as remaining_fuel
    FROM "sampleDB".IoT
    WHERE time >= ago(48h)
    AND truck_id IN (SELECT truck_id FROM low_fuel_trucks)
    AND (measure_value::double/cast(fuel_capacity as double)*100) >= 10
    AND measure_name = 'fuel-reading'
),
trucks_that_refuelled AS (
    SELECT a.truck_id
    FROM low_fuel_trucks a JOIN other_trucks b
    ON a.truck_id = b.truck_id AND b.time >= a.time
)
SELECT DISTINCT truck_id, fleet, make, model, fuel_pct
FROM low_fuel_trucks
WHERE truck_id NOT IN (
    SELECT truck_id FROM trucks_that_refuelled
)
```

Find the average load and max speed for each truck for the past week:

```
SELECT
    bin(time, 1d) as binned_time,
    fleet,
    truck_id,
    make,
    model,
    AVG(
        CASE WHEN measure_name = 'load' THEN measure_value::double ELSE NULL END
    ) AS avg_load_tons,
    MAX(
        CASE WHEN measure_name = 'speed' THEN measure_value::double ELSE NULL END
    ) AS max_speed_mph
FROM "sampleDB".IoT
WHERE time >= ago(7d)
AND measure_name IN ('load', 'speed')
GROUP BY fleet, truck_id, make, model, bin(time, 1d)
ORDER BY truck_id
```

Get the load efficiency for each truck for the past week:

```
WITH average_load_per_truck AS (
    SELECT
        truck_id,
        avg(measure_value::double)  AS avg_load
    FROM "sampleDB".IoT
    WHERE measure_name = 'load'
    AND time >= ago(7d)
    GROUP BY truck_id, fleet, load_capacity, make, model
),
truck_load_efficiency AS (
    SELECT
        a.truck_id,
        fleet,
        load_capacity,
        make,
        model,
        avg_load,
        measure_value::double,
        time,
        (measure_value::double*100)/avg_load as load_efficiency -- , approx_percentile(avg_load_pct, DOUBLE '0.9')
    FROM "sampleDB".IoT a JOIN average_load_per_truck b
    ON a.truck_id = b.truck_id
    WHERE a.measure_name = 'load'
)
SELECT
    truck_id,
    time,
    load_efficiency
FROM truck_load_efficiency
ORDER BY truck_id, time
```