SelectDB Enterprise
参考手册
SQL 函数
聚合函数
VAR_SAMP,VARIANCE_SAMP

VAR_SAMP,VARIANCE_SAMP

描述

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

别名

  • VARIANCE_SAMP

语法

VAR_SAMP(<expr>)

参数

参数描述
<expr>要计算样本方差的列或表达式

返回值

返回一个 DOUBLE 类型的值,表示计算得到的样本方差。

举例

-- 创建示例表
CREATE TABLE student_scores (
    student_id INT,
    score DECIMAL(4,1)
) DISTRIBUTED BY HASH(student_id)
PROPERTIES (
    "replication_num" = "1"
);
 
-- 插入测试数据
INSERT INTO student_scores VALUES
(1, 85.5),
(2, 92.0),
(3, 78.5),
(4, 88.0),
(5, 95.5),
(6, 82.0),
(7, 90.0),
(8, 87.5);
 
-- 计算学生成绩的样本方差
SELECT 
    VAR_SAMP(score) as sample_variance,
    VARIANCE(score) as population_variance
FROM student_scores;
+------------------+---------------------+
| sample_variance  | population_variance |
+------------------+---------------------+
| 29.4107142857143 |   25.73437500000001 |
+------------------+---------------------+
© 2025 北京飞轮数据科技有限公司 京ICP备2022004029号 | Apache、Apache Doris 以及相关开源项目名称均为 Apache 基金会商标