Skip to content

Commit c802349

Browse files
BohuTANGclaude
andauthored
Update ANY_VALUE aggregate function documentation (#2818)
* Update ANY_VALUE aggregate function documentation - Make ANY_VALUE the primary function with comprehensive documentation - Add redirect from old aggregate-any path to aggregate-any-value - Remove duplicate aggregate-any.md file - Include clear examples showing GROUP BY performance benefits - Add version info (v1.2.815) * Fix broken markdown link for ANY_VALUE function in aggregate index Update reference from aggregate-any.md to aggregate-any-value.md to match the updated filename, resolving build error. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]> --------- Co-authored-by: Claude <[email protected]>
1 parent 2f04812 commit c802349

File tree

4 files changed

+85
-55
lines changed

4 files changed

+85
-55
lines changed
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
---
2+
title: ANY_VALUE
3+
---
4+
import FunctionDescription from '@site/src/components/FunctionDescription';
5+
6+
<FunctionDescription description="Introduced or updated: v1.2.815"/>
7+
8+
Aggregate function.
9+
10+
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.
11+
12+
> **Alias:** `ANY()` returns the same result as `ANY_VALUE()` and remains available for compatibility.
13+
14+
## Syntax
15+
16+
```sql
17+
ANY_VALUE(<expr>)
18+
```
19+
20+
## Arguments
21+
22+
| Arguments | Description |
23+
|-----------|----------------|
24+
| `<expr>` | Any expression |
25+
26+
## Return Type
27+
28+
The type of `<expr>`. If all values are NULL, the return value is NULL.
29+
30+
:::note
31+
- `ANY_VALUE()` is non-deterministic and may return different values across executions.
32+
- For predictable results, use `MIN()` or `MAX()` instead.
33+
:::
34+
35+
## Example
36+
37+
**Sample Data:**
38+
```sql
39+
CREATE TABLE sales (
40+
region VARCHAR,
41+
manager VARCHAR,
42+
sales_amount DECIMAL(10, 2)
43+
);
44+
45+
INSERT INTO sales VALUES
46+
('North', 'Alice', 15000.00),
47+
('North', 'Alice', 12000.00),
48+
('South', 'Bob', 20000.00);
49+
```
50+
51+
**Problem:** This query fails because `manager` isn't in GROUP BY:
52+
```sql
53+
SELECT region, manager, SUM(sales_amount) -- ❌ Error
54+
FROM sales GROUP BY region;
55+
```
56+
57+
**Old approach:** Add `manager` to GROUP BY, but this creates more groups than needed and hurts performance:
58+
```sql
59+
SELECT region, manager, SUM(sales_amount)
60+
FROM sales GROUP BY region, manager; -- ❌ Poor performance due to extra grouping
61+
```
62+
63+
**Better solution:** Use `ANY_VALUE()` to select the manager:
64+
```sql
65+
SELECT
66+
region,
67+
ANY_VALUE(manager) AS manager, -- ✅ Works
68+
SUM(sales_amount) AS total_sales
69+
FROM sales
70+
GROUP BY region;
71+
```
72+
73+
**Result:**
74+
```text
75+
| region | manager | total_sales |
76+
|--------|---------|-------------|
77+
| North | Alice | 27000.00 |
78+
| South | Bob | 20000.00 |
79+
```

docs/en/sql-reference/20-sql-functions/07-aggregate-functions/aggregate-any.md

Lines changed: 0 additions & 54 deletions
This file was deleted.

docs/en/sql-reference/20-sql-functions/07-aggregate-functions/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ This page provides a comprehensive overview of aggregate functions in Databend,
1515
| [AVG](aggregate-avg.md) | Calculates the average of values | `AVG(temperature)``72.5` |
1616
| [MIN](aggregate-min.md) | Returns the minimum value | `MIN(price)``9.99` |
1717
| [MAX](aggregate-max.md) | Returns the maximum value | `MAX(price)``99.99` |
18-
| [ANY](aggregate-any.md) | Returns any value from the group | `ANY(status)``'active'` |
18+
| [ANY_VALUE](aggregate-any-value.md) | Returns any value from the group | `ANY_VALUE(status)``'active'` |
1919

2020
## Conditional Aggregation
2121

site-redirects.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,11 @@ const siteRedirects = [
429429
{
430430
from: '/guides/query/dictionary',
431431
to: '/guides/query/advanced/'
432+
},
433+
// ANY function redirect to ANY_VALUE
434+
{
435+
from: '/sql/sql-functions/aggregate-functions/aggregate-any',
436+
to: '/sql/sql-functions/aggregate-functions/aggregate-any-value'
432437
}
433438
];
434439
export default siteRedirects;

0 commit comments

Comments
 (0)