Skip to content
Open
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -248,3 +248,57 @@ SELECT topKWeighted(double1, _sample_interval) FROM my_dataset
-- find the 15 most common values of <blob1>, weighted by `_sample_interval`
SELECT topKWeighted(15)(blob1, _sample_interval) FROM my_dataset
```

## countIf <Badge text="New" variant="tip" size="small" />

Usage:

```sql
countIf(<expr>)
```

`countIf` is an aggregation function that returns the number of rows in the results set,
but only counting rows where a provided expression evaluates to true.

Example:

```sql
-- return the number of rows where `double1` is greater than 5
countIf(double1 > 5)
```

## sumIf <Badge text="New" variant="tip" size="small" />

Usage:

```sql
sumIf(<expr>, <expr>)
```

`sumIf` is an aggregation function that returns the sum of a first expression across all rows in the results set,
but only including rows where a second expression evaluates to true.

Example:

```sql
-- return the sum of column `item_cost` of all items where another column `in_stock` is not zero
sumIf(item_cost, in_stock > 0)
```

## avgIf <Badge text="New" variant="tip" size="small" />

Usage:

```sql
avgIf(<expr>, <expr>)
```

`avgIf` is an aggregation function that returns the mean of an expression across all rows in the results set,
but only including rows where a second expression evaluates to true.

Example:

```sql
-- return the mean of column `item_cost` where another column `in_stock` is not zero
avgIf(item_cost, in_stock > 0)
```
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,55 @@ head:

---

import { Badge } from "~/components"

## formatDateTime

Usage:

```sql
formatDateTime(<datetime expression>, <format string>[, <timezone string>])
```

`formatDateTime` prints a datetime as a string according to a provided format string. Refer to
[ClickHouse's documentation](https://clickhouse.com/docs/en/sql-reference/functions/date-time-functions/#formatdatetime)
for a list of supported formatting options.

Examples:

```sql
-- prints the current YYYY-MM-DD in UTC
formatDateTime(now(), '%Y-%m-%d')

-- prints YYYY-MM-DD in the datetime's timezone
formatDateTime(<a datetime with a timezone>, '%Y-%m-%d')
formatDateTime(toDateTime('2022-12-01 16:17:00', 'America/New_York'), '%Y-%m-%d')

-- prints YYYY-MM-DD in UTC
formatDateTime(<a datetime with a timezone>, '%Y-%m-%d', 'Etc/UTC')
formatDateTime(toDateTime('2022-12-01 16:17:00', 'America/New_York'), '%Y-%m-%d', 'Etc/UTC')
```

## now

Usage:

```sql
now()
```

Returns the current time as a DateTime.

## today <Badge text="New" variant="tip" size="small" />

Usage:

```sql
now()
```

Returns the current date as a `Date`.

## toDateTime

Usage:
Expand Down Expand Up @@ -37,58 +86,123 @@ toDateTime('1981-04-12 12:00:04') -- string with datetime in 'YYYY-MM-DD hh:mm:s
toDateTime('2022-12-01 16:17:00', 'America/New_York')
```

## now
## toYear <Badge text="New" variant="tip" size="small" />

Usage:

```sql
now()
toYear(<datetime>)
```

Returns the current time as a DateTime.
`toYear` returns the year of a datetime.

## toUnixTimestamp
Examples:

```sql
-- returns the number 2025
toYear(toDateTime('2025-10-27 00:00:00'))
```

## toMonth <Badge text="New" variant="tip" size="small" />

Usage:

```sql
toUnixTimestamp(<datetime>)
toMonth(<datetime>)
```

`toUnixTimestamp` converts a datetime into an integer unix timestamp.
`toMonth` returns the year of a datetime.

Examples:

```sql
-- get the current unix timestamp
toUnixTimestamp(now())
-- returns the number 10
toMonth(toDateTime('2025-10-27 00:00:00'))
```

## formatDateTime
## toDayOfMonth <Badge text="New" variant="tip" size="small" />

Usage:

```sql
formatDateTime(<datetime expression>, <format string>[, <timezone string>])
toDayOfMonth(<datetime>)
```

`formatDateTime` prints a datetime as a string according to a provided format string. Refer to
[ClickHouse's documentation](https://clickhouse.com/docs/en/sql-reference/functions/date-time-functions/#formatdatetime)
for a list of supported formatting options.
`toDayOfMonth` returns the day of the month from a datetime.

Examples:

```sql
-- prints the current YYYY-MM-DD in UTC
formatDateTime(now(), '%Y-%m-%d')
-- returns the number 27
toDayOfMonth(toDateTime('2025-10-27 00:00:00'))
```

-- prints YYYY-MM-DD in the datetime's timezone
formatDateTime(<a datetime with a timezone>, '%Y-%m-%d')
formatDateTime(toDateTime('2022-12-01 16:17:00', 'America/New_York'), '%Y-%m-%d')
## toHour <Badge text="New" variant="tip" size="small" />

-- prints YYYY-MM-DD in UTC
formatDateTime(<a datetime with a timezone>, '%Y-%m-%d', 'Etc/UTC')
formatDateTime(toDateTime('2022-12-01 16:17:00', 'America/New_York'), '%Y-%m-%d', 'Etc/UTC')
Usage:

```sql
toHour(<datetime>)
```

`toHour` returns the hour of the day from a datetime.

Examples:

```sql
-- returns the number 9
toHour(toDateTime('2025-10-27 09:11:13'))
```

## toMinute <Badge text="New" variant="tip" size="small" />

Usage:

```sql
toMinute(<datetime>)
```

`toMinute` returns the minute of the hour from a datetime.

Examples:

```sql
-- returns the number 11
toMinute(toDateTime('2025-10-27 09:11:13'))
```

## toSecond <Badge text="New" variant="tip" size="small" />

Usage:

```sql
toSecond(<datetime>)
```

`toSecond` returns the second of the minute from a datetime.

Examples:

```sql
-- returns the number 13
toSecond(toDateTime('2025-10-27 09:11:13'))
```

## toUnixTimestamp

Usage:

```sql
toUnixTimestamp(<datetime>)
```

`toUnixTimestamp` converts a datetime into an integer unix timestamp.

Examples:

```sql
-- get the current unix timestamp
toUnixTimestamp(now())
```

## toStartOfInterval
Expand Down Expand Up @@ -119,3 +233,111 @@ FROM your_dataset
GROUP BY hour
ORDER BY hour ASC
```

## toStartOfYear <Badge text="New" variant="tip" size="small" />

Usage:

```sql
toStartOfYear(<datetime>)
```

`toStartOfYear` rounds down a datetime to the nearest start of year. This can be useful
for grouping data into equal-sized time ranges.

Examples:

```sql
-- round a timestamp down to 2025-01-01 00:00:00
toStartOfYear(toDateTime('2025-10-27 00:00:00'))
```

## toStartOfMonth <Badge text="New" variant="tip" size="small" />

Usage:

```sql
toStartOfMonth(<datetime>)
```

`toStartOfMonth` rounds down a datetime to the nearest start of month. This can be useful
for grouping data into equal-sized time ranges.

Examples:

```sql
-- round a timestamp down to 2025-10-01 00:00:00
toStartOfMonth(toDateTime('2025-10-27 00:00:00'))
```

## toStartOfDay <Badge text="New" variant="tip" size="small" />

Usage:

```sql
toStartOfDay(<datetime>)
```

`toStartOfDay` rounds down a datetime to the nearest start of day. This can be useful
for grouping data into equal-sized time ranges.

Examples:

```sql
-- round a timestamp down to 2025-10-27 00:00:00
toStartOfDay(toDateTime('2025-10-27 00:00:00'))
```

## toStartOfHour <Badge text="New" variant="tip" size="small" />

Usage:

```sql
toStartOfHour(<datetime>)
```

`toStartOfHour` rounds down a datetime to the nearest start of hour. This can be useful
for grouping data into equal-sized time ranges.

Examples:

```sql
-- round a timestamp down to 2025-10-27 16:00:00
toStartOfHour(toDateTime('2025-10-27 16:55:25'))
```

## toStartOfFifteenMinutes <Badge text="New" variant="tip" size="small" />

Usage:

```sql
toStartOfFifteenMinutes(<datetime>)
```

`toStartOfFifteenMinutes` rounds down a datetime to the nearest fifteen minutes. This can be useful
for grouping data into equal-sized time ranges.

Examples:

```sql
-- round a timestamp down to 2025-10-27 16:45:00
toStartOfFifteenMinutes(toDateTime('2025-10-27 16:55:25'))
```

## toStartOfMinute <Badge text="New" variant="tip" size="small" />

Usage:

```sql
toStartOfMinute(<datetime>)
```

`toStartOfMinute` rounds down a datetime to the nearest minute. This can be useful
for grouping data into equal-sized time ranges.

Examples:

```sql
-- round a timestamp down to 2025-10-27 16:55:00
toStartOfMinute(toDateTime('2025-10-27 16:55:25'))
```