diff --git a/docs/en/sql-reference/20-sql-functions/07-aggregate-functions/aggregate-any-value.md b/docs/en/sql-reference/20-sql-functions/07-aggregate-functions/aggregate-any-value.md new file mode 100644 index 0000000000..24445aa027 --- /dev/null +++ b/docs/en/sql-reference/20-sql-functions/07-aggregate-functions/aggregate-any-value.md @@ -0,0 +1,79 @@ +--- +title: ANY_VALUE +--- +import FunctionDescription from '@site/src/components/FunctionDescription'; + + + +Aggregate function. + +The `ANY_VALUE()` function returns an arbitrary non-NULL value from the input expression. It's used in `GROUP BY` queries when you need to select a column that isn't grouped or aggregated. + +> **Alias:** `ANY()` returns the same result as `ANY_VALUE()` and remains available for compatibility. + +## Syntax + +```sql +ANY_VALUE() +``` + +## Arguments + +| Arguments | Description | +|-----------|----------------| +| `` | Any expression | + +## Return Type + +The type of ``. If all values are NULL, the return value is NULL. + +:::note +- `ANY_VALUE()` is non-deterministic and may return different values across executions. +- For predictable results, use `MIN()` or `MAX()` instead. +::: + +## Example + +**Sample Data:** +```sql +CREATE TABLE sales ( + region VARCHAR, + manager VARCHAR, + sales_amount DECIMAL(10, 2) +); + +INSERT INTO sales VALUES + ('North', 'Alice', 15000.00), + ('North', 'Alice', 12000.00), + ('South', 'Bob', 20000.00); +``` + +**Problem:** This query fails because `manager` isn't in GROUP BY: +```sql +SELECT region, manager, SUM(sales_amount) -- ❌ Error +FROM sales GROUP BY region; +``` + +**Old approach:** Add `manager` to GROUP BY, but this creates more groups than needed and hurts performance: +```sql +SELECT region, manager, SUM(sales_amount) +FROM sales GROUP BY region, manager; -- ❌ Poor performance due to extra grouping +``` + +**Better solution:** Use `ANY_VALUE()` to select the manager: +```sql +SELECT + region, + ANY_VALUE(manager) AS manager, -- ✅ Works + SUM(sales_amount) AS total_sales +FROM sales +GROUP BY region; +``` + +**Result:** +```text +| region | manager | total_sales | +|--------|---------|-------------| +| North | Alice | 27000.00 | +| South | Bob | 20000.00 | +``` diff --git a/docs/en/sql-reference/20-sql-functions/07-aggregate-functions/aggregate-any.md b/docs/en/sql-reference/20-sql-functions/07-aggregate-functions/aggregate-any.md deleted file mode 100644 index cf2ef8c97e..0000000000 --- a/docs/en/sql-reference/20-sql-functions/07-aggregate-functions/aggregate-any.md +++ /dev/null @@ -1,54 +0,0 @@ ---- -title: ANY ---- - -Aggregate function. - -The ANY() function selects the first encountered (non-NULL) value, unless all rows have NULL values in that column. The query can be executed in any order and even in a different order each time, so the result of this function is indeterminate. To get a determinate result, you can use the ‘min’ or ‘max’ function instead of ‘any’. - -## Syntax - -```sql -ANY() -``` - -## Arguments - -| Arguments | Description | -|-----------|----------------| -| `` | Any expression | - -## Return Type - -The first encountered (non-NULL) value, in the type of the value. If all values are NULL, the return value is NULL. - -## Example - -**Create a Table and Insert Sample Data* -```sql -CREATE TABLE product_data ( - id INT, - product_name VARCHAR NULL, - price FLOAT NULL -); - -INSERT INTO product_data (id, product_name, price) -VALUES (1, 'Laptop', 1000), - (2, NULL, 800), - (3, 'Keyboard', NULL), - (4, 'Mouse', 25), - (5, 'Monitor', 150); -``` - -**Query Demo: Retrieve the First Encountered Non-NULL Product Name** -```sql -SELECT ANY(product_name) AS any_product_name -FROM product_data; -``` - -**Result** -```sql -| any_product_name | -|------------------| -| Laptop | -``` \ No newline at end of file diff --git a/docs/en/sql-reference/20-sql-functions/07-aggregate-functions/index.md b/docs/en/sql-reference/20-sql-functions/07-aggregate-functions/index.md index f98a68eed7..5fcbdd3d71 100644 --- a/docs/en/sql-reference/20-sql-functions/07-aggregate-functions/index.md +++ b/docs/en/sql-reference/20-sql-functions/07-aggregate-functions/index.md @@ -15,7 +15,7 @@ This page provides a comprehensive overview of aggregate functions in Databend, | [AVG](aggregate-avg.md) | Calculates the average of values | `AVG(temperature)` → `72.5` | | [MIN](aggregate-min.md) | Returns the minimum value | `MIN(price)` → `9.99` | | [MAX](aggregate-max.md) | Returns the maximum value | `MAX(price)` → `99.99` | -| [ANY](aggregate-any.md) | Returns any value from the group | `ANY(status)` → `'active'` | +| [ANY_VALUE](aggregate-any-value.md) | Returns any value from the group | `ANY_VALUE(status)` → `'active'` | ## Conditional Aggregation diff --git a/site-redirects.ts b/site-redirects.ts index 5ca031ee4f..ff9ad3e95c 100644 --- a/site-redirects.ts +++ b/site-redirects.ts @@ -429,6 +429,11 @@ const siteRedirects = [ { from: '/guides/query/dictionary', to: '/guides/query/advanced/' + }, + // ANY function redirect to ANY_VALUE + { + from: '/sql/sql-functions/aggregate-functions/aggregate-any', + to: '/sql/sql-functions/aggregate-functions/aggregate-any-value' } ]; export default siteRedirects; \ No newline at end of file