diff --git a/src/content/docs/analytics/analytics-engine/sql-reference/aggregate-functions.mdx b/src/content/docs/analytics/analytics-engine/sql-reference/aggregate-functions.mdx index 2535fc169e9c15..7795d3c3c70473 100644 --- a/src/content/docs/analytics/analytics-engine/sql-reference/aggregate-functions.mdx +++ b/src/content/docs/analytics/analytics-engine/sql-reference/aggregate-functions.mdx @@ -248,3 +248,57 @@ SELECT topKWeighted(double1, _sample_interval) FROM my_dataset -- find the 15 most common values of , weighted by `_sample_interval` SELECT topKWeighted(15)(blob1, _sample_interval) FROM my_dataset ``` + +## countIf + +Usage: + +```sql +countIf() +``` + +`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 + +Usage: + +```sql +sumIf(, ) +``` + +`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 + +Usage: + +```sql +avgIf(, ) +``` + +`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) +``` diff --git a/src/content/docs/analytics/analytics-engine/sql-reference/date-time-functions.mdx b/src/content/docs/analytics/analytics-engine/sql-reference/date-time-functions.mdx index 9f46f5ac2ebce0..802263ee66e496 100644 --- a/src/content/docs/analytics/analytics-engine/sql-reference/date-time-functions.mdx +++ b/src/content/docs/analytics/analytics-engine/sql-reference/date-time-functions.mdx @@ -9,6 +9,55 @@ head: --- +import { Badge } from "~/components" + +## formatDateTime + +Usage: + +```sql +formatDateTime(, [, ]) +``` + +`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(, '%Y-%m-%d') +formatDateTime(toDateTime('2022-12-01 16:17:00', 'America/New_York'), '%Y-%m-%d') + +-- prints YYYY-MM-DD in UTC +formatDateTime(, '%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 + +Usage: + +```sql +now() +``` + +Returns the current date as a `Date`. + ## toDateTime Usage: @@ -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 Usage: ```sql -now() +toYear() ``` -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 Usage: ```sql -toUnixTimestamp() +toMonth() ``` -`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 Usage: ```sql -formatDateTime(, [, ]) +toDayOfMonth() ``` -`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(, '%Y-%m-%d') -formatDateTime(toDateTime('2022-12-01 16:17:00', 'America/New_York'), '%Y-%m-%d') +## toHour --- prints YYYY-MM-DD in UTC -formatDateTime(, '%Y-%m-%d', 'Etc/UTC') -formatDateTime(toDateTime('2022-12-01 16:17:00', 'America/New_York'), '%Y-%m-%d', 'Etc/UTC') +Usage: + +```sql +toHour() +``` + +`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 + +Usage: + +```sql +toMinute() +``` + +`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 + +Usage: + +```sql +toSecond() +``` + +`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() +``` + +`toUnixTimestamp` converts a datetime into an integer unix timestamp. + +Examples: + +```sql +-- get the current unix timestamp +toUnixTimestamp(now()) ``` ## toStartOfInterval @@ -119,3 +233,111 @@ FROM your_dataset GROUP BY hour ORDER BY hour ASC ``` + +## toStartOfYear + +Usage: + +```sql +toStartOfYear() +``` + +`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 + +Usage: + +```sql +toStartOfMonth() +``` + +`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 + +Usage: + +```sql +toStartOfDay() +``` + +`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 + +Usage: + +```sql +toStartOfHour() +``` + +`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 + +Usage: + +```sql +toStartOfFifteenMinutes() +``` + +`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 + +Usage: + +```sql +toStartOfMinute() +``` + +`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')) +```