Skip to content

Commit bde1977

Browse files
linrrzqqqzclllyybb
andauthored
[Fix](variance) Fix sample variance/stddev null res for single value (#3765)
## Versions - [x] dev - [x] 4.x - [ ] 3.x - [ ] 2.1 or older (not covered by version/language sync gate) ## Languages - [x] Chinese - [x] English - [ ] Japanese candidate translation needed ## Docs Checklist - [ ] Checked by AI - [ ] Test Cases Built - [ ] Updated required version and language counterparts, or explained why not - [ ] If only one language changed, confirmed whether source/translation counterparts need sync --------- Co-authored-by: zclllyybb <zhaochangle@selectdb.com>
1 parent 61c1b44 commit bde1977

8 files changed

Lines changed: 242 additions & 10 deletions

File tree

docs/sql-manual/sql-functions/aggregate-functions/stddev-samp.md

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,14 @@
1010

1111
Returns the sample standard deviation of the expr expression
1212

13+
The calculation formula is:
14+
15+
$
16+
\mathrm{STDDEV\_SAMP}(x)=\sqrt{\mathrm{VAR\_SAMP}(x)}=\sqrt{\frac{\sum_{i=1}^{n}(x_i-\bar{x})^2}{n-1}}
17+
$
18+
19+
Where `n` is the number of valid values in the group.
20+
1321
## Syntax
1422

1523
```sql
@@ -25,11 +33,11 @@ STDDEV_SAMP(<expr>)
2533
## Return Value
2634

2735
Return the sample standard deviation of the expr expression as Double type.
28-
If there is no valid data in the group, returns NULL.
36+
If there is no valid data in the group, returns NULL. If the number of valid values in the group is 1, returns NaN.
2937

3038
### Examples
3139
```sql
32-
-- Create sample tables
40+
-- Create sample table
3341
CREATE TABLE score_table (
3442
student_id INT,
3543
score DOUBLE
@@ -58,3 +66,24 @@ FROM score_table;
5866
| 4.949747468305831 |
5967
+-------------------+
6068
```
69+
70+
When the number of valid values is 1, `STDDEV_SAMP` returns `NaN`.
71+
72+
```sql
73+
-- Create a single-column sample table
74+
CREATE TABLE sample_values (
75+
value INT
76+
) DISTRIBUTED BY HASH(value)
77+
PROPERTIES (
78+
"replication_num" = "1"
79+
);
80+
81+
INSERT INTO sample_values VALUES (10);
82+
83+
SELECT STDDEV_SAMP(value) AS sample_stddev FROM sample_values;
84+
+---------------+
85+
| sample_stddev |
86+
+---------------+
87+
| NaN |
88+
+---------------+
89+
```

docs/sql-manual/sql-functions/aggregate-functions/var-samp.md

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,14 @@
1010

1111
The VAR_SAMP function calculates the sample variance of a specified expression. Unlike VARIANCE (population variance), VAR_SAMP uses n-1 as the divisor, which is considered an unbiased estimate of the population variance in statistics.
1212

13+
The calculation formula is:
14+
15+
$
16+
\mathrm{VAR\_SAMP}(x)=\frac{\sum_{i=1}^{n}(x_i-\bar{x})^2}{n-1}
17+
$
18+
19+
Where `n` is the number of valid values in the group.
20+
1321
## Alias
1422

1523
- VARIANCE_SAMP
@@ -28,7 +36,7 @@ VAR_SAMP(<expr>)
2836

2937
## Return Value
3038
Returns a Double value representing the calculated sample variance.
31-
If there is no valid data in the group, returns NULL.
39+
If there is no valid data in the group, returns NULL. If the number of valid values in the group is 1, returns NaN.
3240

3341
## Examples
3442
```sql
@@ -66,3 +74,24 @@ FROM student_scores;
6674
| 29.4107142857143 | 25.73437500000001 |
6775
+------------------+---------------------+
6876
```
77+
78+
When the number of valid values is 1, `VAR_SAMP` returns `NaN`.
79+
80+
```sql
81+
-- Create a single-column sample table
82+
CREATE TABLE sample_values (
83+
value INT
84+
) DISTRIBUTED BY HASH(value)
85+
PROPERTIES (
86+
"replication_num" = "1"
87+
);
88+
89+
INSERT INTO sample_values VALUES (10);
90+
91+
SELECT VAR_SAMP(value) AS sample_variance FROM sample_values;
92+
+-----------------+
93+
| sample_variance |
94+
+-----------------+
95+
| NaN |
96+
+-----------------+
97+
```

i18n/zh-CN/docusaurus-plugin-content-docs/current/sql-manual/sql-functions/aggregate-functions/stddev-samp.md

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,14 @@
1010

1111
返回 expr 表达式的样本标准差
1212

13+
计算公式如下:
14+
15+
$
16+
\mathrm{STDDEV\_SAMP}(x)=\sqrt{\mathrm{VAR\_SAMP}(x)}=\sqrt{\frac{\sum_{i=1}^{n}(x_i-\bar{x})^2}{n-1}}
17+
$
18+
19+
其中 `n` 为组内合法数据的个数。
20+
1321
## 语法
1422

1523
```sql
@@ -25,7 +33,7 @@ STDDEV_SAMP(<expr>)
2533
## 返回值
2634

2735
返回 Double 类型的参数 expr 的样本标准差。
28-
当组内没有合法数据时,返回 NULL。
36+
当组内没有合法数据时,返回 NULL。组内合法数据个数为 1 时,返回 NaN。
2937

3038
## 举例
3139
```sql
@@ -58,3 +66,24 @@ FROM score_table;
5866
| 4.949747468305831 |
5967
+-------------------+
6068
```
69+
70+
当合法数据个数为 1 时,`STDDEV_SAMP` 返回 `NaN`
71+
72+
```sql
73+
-- 创建单列示例表
74+
CREATE TABLE sample_values (
75+
value INT
76+
) DISTRIBUTED BY HASH(value)
77+
PROPERTIES (
78+
"replication_num" = "1"
79+
);
80+
81+
INSERT INTO sample_values VALUES (10);
82+
83+
SELECT STDDEV_SAMP(value) AS sample_stddev FROM sample_values;
84+
+---------------+
85+
| sample_stddev |
86+
+---------------+
87+
| NaN |
88+
+---------------+
89+
```

i18n/zh-CN/docusaurus-plugin-content-docs/current/sql-manual/sql-functions/aggregate-functions/var-samp.md

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,14 @@
1010

1111
VAR_SAMP 函数计算指定表达式的样本方差。与 VARIANCE(总体方差)不同,VAR_SAMP 使用 n-1 作为除数,这在统计学上被认为是对总体方差的无偏估计。
1212

13+
计算公式如下:
14+
15+
$
16+
\mathrm{VAR\_SAMP}(x)=\frac{\sum_{i=1}^{n}(x_i-\bar{x})^2}{n-1}
17+
$
18+
19+
其中 `n` 为组内合法数据的个数。
20+
1321
## 别名
1422

1523
- VARIANCE_SAMP
@@ -28,7 +36,7 @@ VAR_SAMP(<expr>)
2836

2937
## 返回值
3038
返回一个 Double 类型的值,表示计算得到的样本方差。
31-
组内没有合法数据时,返回 NULL。
39+
组内没有合法数据时,返回 NULL。组内合法数据个数为 1 时,返回 NaN。
3240

3341
## 举例
3442
```sql
@@ -66,3 +74,24 @@ FROM student_scores;
6674
| 29.4107142857143 | 25.73437500000001 |
6775
+------------------+---------------------+
6876
```
77+
78+
当合法数据个数为 1 时,`VAR_SAMP` 返回 `NaN`
79+
80+
```sql
81+
-- 创建单列示例表
82+
CREATE TABLE sample_values (
83+
value INT
84+
) DISTRIBUTED BY HASH(value)
85+
PROPERTIES (
86+
"replication_num" = "1"
87+
);
88+
89+
INSERT INTO sample_values VALUES (10);
90+
91+
SELECT VAR_SAMP(value) AS sample_variance FROM sample_values;
92+
+-----------------+
93+
| sample_variance |
94+
+-----------------+
95+
| NaN |
96+
+-----------------+
97+
```

i18n/zh-CN/docusaurus-plugin-content-docs/version-4.x/sql-manual/sql-functions/aggregate-functions/stddev-samp.md

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,14 @@
1010

1111
返回 expr 表达式的样本标准差
1212

13+
计算公式如下:
14+
15+
$
16+
\mathrm{STDDEV\_SAMP}(x)=\sqrt{\mathrm{VAR\_SAMP}(x)}=\sqrt{\frac{\sum_{i=1}^{n}(x_i-\bar{x})^2}{n-1}}
17+
$
18+
19+
其中 `n` 为组内合法数据的个数。
20+
1321
## 语法
1422

1523
```sql
@@ -25,7 +33,7 @@ STDDEV_SAMP(<expr>)
2533
## 返回值
2634

2735
返回 Double 类型的参数 expr 的样本标准差。
28-
当组内没有合法数据时,返回 NULL。
36+
当组内没有合法数据时,返回 NULL。组内合法数据个数为 1 时,返回 NaN。
2937

3038
## 举例
3139
```sql
@@ -58,3 +66,24 @@ FROM score_table;
5866
| 4.949747468305831 |
5967
+-------------------+
6068
```
69+
70+
当合法数据个数为 1 时,`STDDEV_SAMP` 返回 `NaN`
71+
72+
```sql
73+
-- 创建单列示例表
74+
CREATE TABLE sample_values (
75+
value INT
76+
) DISTRIBUTED BY HASH(value)
77+
PROPERTIES (
78+
"replication_num" = "1"
79+
);
80+
81+
INSERT INTO sample_values VALUES (10);
82+
83+
SELECT STDDEV_SAMP(value) AS sample_stddev FROM sample_values;
84+
+---------------+
85+
| sample_stddev |
86+
+---------------+
87+
| NaN |
88+
+---------------+
89+
```

i18n/zh-CN/docusaurus-plugin-content-docs/version-4.x/sql-manual/sql-functions/aggregate-functions/var-samp.md

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,14 @@
1010

1111
VAR_SAMP 函数计算指定表达式的样本方差。与 VARIANCE(总体方差)不同,VAR_SAMP 使用 n-1 作为除数,这在统计学上被认为是对总体方差的无偏估计。
1212

13+
计算公式如下:
14+
15+
$
16+
\mathrm{VAR\_SAMP}(x)=\frac{\sum_{i=1}^{n}(x_i-\bar{x})^2}{n-1}
17+
$
18+
19+
其中 `n` 为组内合法数据的个数。
20+
1321
## 别名
1422

1523
- VARIANCE_SAMP
@@ -28,7 +36,7 @@ VAR_SAMP(<expr>)
2836

2937
## 返回值
3038
返回一个 Double 类型的值,表示计算得到的样本方差。
31-
组内没有合法数据时,返回 NULL。
39+
组内没有合法数据时,返回 NULL。组内合法数据个数为 1 时,返回 NaN。
3240

3341
## 举例
3442
```sql
@@ -66,3 +74,24 @@ FROM student_scores;
6674
| 29.4107142857143 | 25.73437500000001 |
6775
+------------------+---------------------+
6876
```
77+
78+
当合法数据个数为 1 时,`VAR_SAMP` 返回 `NaN`
79+
80+
```sql
81+
-- 创建单列示例表
82+
CREATE TABLE sample_values (
83+
value INT
84+
) DISTRIBUTED BY HASH(value)
85+
PROPERTIES (
86+
"replication_num" = "1"
87+
);
88+
89+
INSERT INTO sample_values VALUES (10);
90+
91+
SELECT VAR_SAMP(value) AS sample_variance FROM sample_values;
92+
+-----------------+
93+
| sample_variance |
94+
+-----------------+
95+
| NaN |
96+
+-----------------+
97+
```

versioned_docs/version-4.x/sql-manual/sql-functions/aggregate-functions/stddev-samp.md

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,14 @@
1010

1111
Returns the sample standard deviation of the expr expression
1212

13+
The calculation formula is:
14+
15+
$
16+
\mathrm{STDDEV\_SAMP}(x)=\sqrt{\mathrm{VAR\_SAMP}(x)}=\sqrt{\frac{\sum_{i=1}^{n}(x_i-\bar{x})^2}{n-1}}
17+
$
18+
19+
Where `n` is the number of valid values in the group.
20+
1321
## Syntax
1422

1523
```sql
@@ -25,11 +33,11 @@ STDDEV_SAMP(<expr>)
2533
## Return Value
2634

2735
Return the sample standard deviation of the expr expression as Double type.
28-
If there is no valid data in the group, returns NULL.
36+
If there is no valid data in the group, returns NULL. If the number of valid values in the group is 1, returns NaN.
2937

3038
### Examples
3139
```sql
32-
-- Create sample tables
40+
-- Create sample table
3341
CREATE TABLE score_table (
3442
student_id INT,
3543
score DOUBLE
@@ -58,3 +66,24 @@ FROM score_table;
5866
| 4.949747468305831 |
5967
+-------------------+
6068
```
69+
70+
When the number of valid values is 1, `STDDEV_SAMP` returns `NaN`.
71+
72+
```sql
73+
-- Create a single-column sample table
74+
CREATE TABLE sample_values (
75+
value INT
76+
) DISTRIBUTED BY HASH(value)
77+
PROPERTIES (
78+
"replication_num" = "1"
79+
);
80+
81+
INSERT INTO sample_values VALUES (10);
82+
83+
SELECT STDDEV_SAMP(value) AS sample_stddev FROM sample_values;
84+
+---------------+
85+
| sample_stddev |
86+
+---------------+
87+
| NaN |
88+
+---------------+
89+
```

0 commit comments

Comments
 (0)