COVAR_SAMP
返回两个数据列的样本协方差 (Σ((x - x̅)(y - y̅)) / (n - 1))。
语法
COVAR_SAMP(<expr1>, <expr2>)
参数
别名
返回类型
float64,当 n <= 1 时,返回 +∞。
示例
创建表并插入示例数据
CREATE TABLE store_sales (
id INT,
store_id INT,
items_sold INT,
profit FLOAT
);
INSERT INTO store_sales (id, store_id, items_sold, profit)
VALUES (1, 1, 100, 1000),
(2, 2, 200, 2000),
(3, 3, 300, 3000),
(4, 4, 400, 4000),
(5, 5, 500, 5000);
查询示例:计算 items_sold 与 profit 之间的样本协方差
SELECT COVAR_SAMP(items_sold, profit) AS covar_samp_items_profit
FROM store_sales;
结果
| covar_samp_items_profit |
|-------------------------|
| 250000.0 |