如何计算 sql 服务器中多列的方差?

How to calculate variance over multiple columns in sql server?

使用包含许多字段的计算结果的临时 table,我想计算 table 中每一列的方差。 问题是查询总是 returns null 如果我不使用 Coalesce 将空值替换为 0。我不理解文档状态 var 和 [= 会忽略空值13=] 函数。

table 的外观如下:

当运行以下查询时,它给了我null

WITH cte
AS (SELECT
  *
FROM [IPA_10])
SELECT
  var("Cell valide" + "Cell invalide" + "Compagnie valide" + "Compagnie invalide" + "Email 1 Invalide" + "Email 2 Invalide"
  + "Adresse Valide" + "Adresse à Valider" + "Adresse Retournée" + "Homme" + "Femme" + "Aucun Genre" +
  "APS Fort" + "APS Moyen" + "APS Faible" + "APS Très Faible" + "Pas d'APS" + "Français" + "Anglais" + "Aucune Langue" +
  "Home Phone Valide" + "Home Phone Invalide" + "MFR" + "Non MFR" + "NAS Valide" + "NAS Invalide" + "Other Phone 1 Valide" +
  "Other Phone 1 Invalide" + "Other Phone 2 Valide" + "Other Phone 2 Invalide" + "Other Phone 3 Valide" + "Other Phone 3 Invalide" +
  "Other Phone 4 Valide" + "Other Phone 4 Invalide" + "Phone Search 1 Valide" + "Phone Search 1 Invalide" + "Phone Search 2 Valide" +
  "Phone Search 2 Invalide" + "Capital - < 100" + "Capital - 100-200" + "Capital - 200-300" + "Capital - 300-400" +
  "Capital - 400-500" + "Capital - 500-600" + "Capital - 600-700" + "Capital - 700-800" + "Capital - 800-900" +
  "Capital - 900-1000" + "Capital - 1000-1500" + "Capital - 1500-2500" + "Capital - > 2500" + "Délinquance – [0-3[" +
  "Délinquance - [3-6[" + "Délinquance - [6-9[" + "Délinquance – [9-12[" + "Délinquance – [12-24[" +
  "Délinquance – [25-36[" + "Délinquance – [36-84[" + "Délinquance – 84 &+" + "DDN - Aucune Valeur" + "Région Saguenay" +
  "Région Outaouais" + "Région Ontario (qc)" + "Région Abitibi-Témiscamingue" + "Région Autres Provinces - États (qc)" +
  "Région Bas Saint-Laurent" + "Région Capitale-Nationale" + "Région Centre-du-Québec" + "Région Chaudière-Appalache" +
  "Région Côte-Nord" + "Région Estrie" + "Région Gaspésie" + "Région Lanaudière" + "Région Laurentides" + "Région Laval" +
  "Région Mauricie" + "Région Montérégie" + "Région Montréal" + "Région Nord-du-Québec" + "Aucune région (qc)")
FROM cte

使用 coalsce 会得到一个结果,但根据我的理解,它是不准确的,因为它会被包含在方差计算中。 如何获得多列的方差?

在 Larnu 的评论之后,我像这样重组了查询并且它起作用了。 我添加了一个步骤来对每一行的所有列求和,然后计算这些结果的方差。

WITH cte
AS (SELECT
  *
FROM [IPA_10]), cte2 as (
SELECT
  id, coalesce(sum("Cell valide"),0) + coalesce(sum("Cell invalide"),0) + coalesce(sum("Compagnie valide"),0) + coalesce(sum("Compagnie invalide"),0) + coalesce(sum("Email 1 Invalide"),0) + coalesce(sum("Email 2 Invalide"),0) + coalesce(sum("Adresse Valide"),0) + coalesce(sum("Adresse à Valider"),0) + coalesce(sum("Adresse Retournée"),0) + coalesce(sum("Homme"),0) + coalesce(sum("Femme"),0) + coalesce(sum("Aucun Genre"),0) + coalesce(sum("APS Fort"),0) + coalesce(sum("APS Moyen"),0) + coalesce(sum("APS Faible"),0) + coalesce(sum("APS Très Faible"),0) + coalesce(sum("Pas d'APS"),0) + coalesce(sum("Français"),0) + coalesce(sum("Anglais"),0) + coalesce(sum("Aucune Langue"),0) + coalesce(sum("Home Phone Valide"),0) + coalesce(sum("Home Phone Invalide"),0) + coalesce(sum("MFR"),0) + coalesce(sum("Non MFR"),0) + coalesce(sum("NAS Valide"),0) + coalesce(sum("NAS Invalide"),0) + coalesce(sum("Other Phone 1 Valide"),0) + coalesce(sum("Other Phone 1 Invalide"),0) + coalesce(sum("Other Phone 2 Valide"),0) + coalesce(sum("Other Phone 2 Invalide"),0) + coalesce(sum("Other Phone 3 Valide"),0) + coalesce(sum("Other Phone 3 Invalide"),0) + coalesce(sum("Other Phone 4 Valide"),0) + coalesce(sum("Other Phone 4 Invalide"),0) + coalesce(sum("Phone Search 1 Valide"),0) + coalesce(sum("Phone Search 1 Invalide"),0) + coalesce(sum("Phone Search 2 Valide"),0) + coalesce(sum("Phone Search 2 Invalide"),0) + coalesce(sum("Capital - < 100"),0) + coalesce(sum("Capital - 100-200"),0) + coalesce(sum("Capital - 200-300"),0) + coalesce(sum("Capital - 300-400"),0) + coalesce(sum("Capital - 400-500"),0) + coalesce(sum("Capital - 500-600"),0) + coalesce(sum("Capital - 600-700"),0) + coalesce(sum("Capital - 700-800"),0) + coalesce(sum("Capital - 800-900"),0) + coalesce(sum("Capital - 900-1000"),0) + coalesce(sum("Capital - 1000-1500"),0) + coalesce(sum("Capital - 1500-2500"),0) + coalesce(sum("Capital - > 2500"),0) + coalesce(sum("Délinquance – [0-3["),0) + coalesce(sum("Délinquance - [3-6["),0) + coalesce(sum("Délinquance - [6-9["),0) + coalesce(sum("Délinquance – [9-12["),0) + coalesce(sum("Délinquance – [12-24["),0) + coalesce(sum("Délinquance – [25-36["),0) + coalesce(sum("Délinquance – [36-84["),0) + coalesce(sum("Délinquance – 84 &+"),0) + coalesce(sum("DDN - Aucune Valeur"),0) + coalesce(sum("Région Saguenay"),0) + coalesce(sum("Région Outaouais"),0) + coalesce(sum("Région Ontario (qc)"),0) + coalesce(sum("Région Abitibi-Témiscamingue"),0) + coalesce(sum("Région Autres Provinces - États (qc)"),0) + coalesce(sum("Région Bas Saint-Laurent"),0) + coalesce(sum("Région Capitale-Nationale"),0) + coalesce(sum("Région Centre-du-Québec"),0) + coalesce(sum("Région Chaudière-Appalache"),0) + coalesce(sum("Région Côte-Nord"),0) + coalesce(sum("Région Estrie"),0) + coalesce(sum("Région Gaspésie"),0) + coalesce(sum("Région Lanaudière"),0) + coalesce(sum("Région Laurentides"),0) + coalesce(sum("Région Laval"),0) + coalesce(sum("Région Mauricie"),0) + coalesce(sum("Région Montérégie"),0) + coalesce(sum("Région Montréal"),0) + coalesce(sum("Région Nord-du-Québec"),0) + coalesce(sum("Aucune région (qc)"),0) as somme
FROM cte
group by id)
select var(somme) from cte2