Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
120 changes: 120 additions & 0 deletions content/shared/sql-reference/functions/window.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,12 @@ ORDER BY
- [rank](#rank)
- [row_number](#row_number)
- [Analytical Functions](#analytical-functions)
{{% show-in "cloud-serverless,cloud-dedicated" %}}- [difference](#difference){{% /show-in %}}
- [first_value](#first_value)
- [lag](#lag)
- [last_value](#last_value)
- [lead](#lead)
{{% show-in "cloud-serverless,cloud-dedicated" %}}- [non_negative_difference](#non_negative_difference){{% /show-in %}}
- [nth_value](#nth_value)

## Window frames
Expand Down Expand Up @@ -796,12 +798,71 @@ Key differences:

## Analytical Functions

{{% show-in "cloud-serverless,cloud-dedicated" %}}- [difference](#difference){{% /show-in %}}
- [first_value](#first_value)
- [lag](#lag)
- [last_value](#last_value)
- [lead](#lead)
{{% show-in "cloud-serverless,cloud-dedicated" %}}- [non_negative_difference](#non_negative_difference){{% /show-in %}}
- [nth_value](#nth_value)

{{% show-in "cloud-serverless,cloud-dedicated" %}}
### difference

Returns the result of subtraction between subsequent values.

```sql
difference(expression)
```

#### Arguments

- **expression**: Expression to operate on. Can be a numeric column or
function, and any combination of arithmetic operators.

##### Related functions

[non_negative_difference](#non_negative_difference)

{{< expand-wrapper >}}
{{% expand "View `difference` query example" %}}

The following example uses the {{< influxdb3/home-sample-link >}}.

{{% influxdb/custom-timestamps %}}

```sql
SELECT
time,
room,
temp,
difference(temp) OVER (
PARTITION BY room
ORDER BY time
) AS difference
FROM home
WHERE
time >= '2022-01-01T08:00:00Z'
AND time < '2022-01-01T11:00:00Z'
ORDER BY room, time
```

| time | room | temp | difference |
| :------------------ | :---------- | ---: | ---------: |
| 2022-01-01T08:00:00 | Kitchen | 21.0 | |
| 2022-01-01T09:00:00 | Kitchen | 23.0 | 2.0 |
| 2022-01-01T10:00:00 | Kitchen | 22.7 | -0.3 |
| 2022-01-01T08:00:00 | Living Room | 21.1 | |
| 2022-01-01T09:00:00 | Living Room | 21.4 | 0.3 |
| 2022-01-01T10:00:00 | Living Room | 21.8 | 0.4 |

{{% /influxdb/custom-timestamps %}}

{{% /expand %}}
{{< /expand-wrapper >}}

{{% /show-in %}}

### first_value

Returns the value from the first row of the window frame.
Expand Down Expand Up @@ -1120,6 +1181,65 @@ ORDER BY room, time
{{% /expand %}}
{{< /expand-wrapper >}}

{{% show-in "cloud-serverless,cloud-dedicated" %}}

### non_negative_difference

Returns only non-negative results of subtraction between subsequent values.
Negative differences return _NULL_.

```sql
non_negative_difference(expression)
```

#### Arguments

- **expression**: Expression to operate on. Can be a numeric column or
function, and any combination of arithmetic operators.

##### Related functions

[difference](#difference)

{{< expand-wrapper >}}
{{% expand "View `non_negative_difference` query example" %}}

The following example uses the {{< influxdb3/home-sample-link >}}.

{{% influxdb/custom-timestamps %}}

```sql
SELECT
time,
room,
temp,
non_negative_difference(temp) OVER (
PARTITION BY room
ORDER BY time
) AS non_negative_difference
FROM home
WHERE
time >= '2022-01-01T08:00:00Z'
AND time < '2022-01-01T11:00:00Z'
ORDER BY room, time
```

| time | room | temp | non_negative_difference |
| :------------------ | :---------- | ---: | ----------------------: |
| 2022-01-01T08:00:00 | Kitchen | 21.0 | |
| 2022-01-01T09:00:00 | Kitchen | 23.0 | 2.0 |
| 2022-01-01T10:00:00 | Kitchen | 22.7 | |
| 2022-01-01T08:00:00 | Living Room | 21.1 | |
| 2022-01-01T09:00:00 | Living Room | 21.4 | 0.3 |
| 2022-01-01T10:00:00 | Living Room | 21.8 | 0.4 |

{{% /influxdb/custom-timestamps %}}

{{% /expand %}}
{{< /expand-wrapper >}}

{{% /show-in %}}

### nth_value

Returns the value from the row that is the nth row of the window frame
Expand Down
Loading