MySQL Workbench,NULL的平均值返回“ 0”,我需要它返回NULL

时间:2019-09-26 13:38:59

标签: mysql mysql-workbench sql-null

我正在尝试平均地块水平数据。当我在绘图级别上执行查询时,与某些行名关联的所有行的结果均为“ NULL”。当我尝试对AVG特征和按行名分组执行相同的查询时,结果显示为“ 0”。我需要它显示“ NULL”,所以它不会影响我的平均水平。

选择绘图级数据的示例代码:

SELECT experiment_name, `year`, location_name, line_name, head_dap, height_cm, yield_bua
FROM data_amba
WHERE experiment_name = 'matspr' AND `year` = 2018 AND location_name = 'Stephen'
GROUP BY line_name, head_dap, height_cm, yield_bua;

结果:

+------------+----------+-----------+-----------+
| line_name  | head_dap | height_cm | yield_bua |
+------------+----------+-----------+-----------+
| ND_GENESIS | NULL     | NULL      | 130.9     |
+------------+----------+-----------+-----------+
| ND_GENESIS | NULL     | NULL      | 131.3     |
+------------+----------+-----------+-----------+
| ND_GENESIS | NULL     | NULL      | 134.3     |
+------------+----------+-----------+-----------+
| PINNACLE   | NULL     | NULL      | 144.9     |
+------------+----------+-----------+-----------+
| PINNACLE   | NULL     | NULL      | 146.6     |
+------------+----------+-----------+-----------+
| PINNACLE   | NULL     | NULL      | 163.9     |
+------------+----------+-----------+-----------+
| S2M177     | NULL     | NULL      | 107.6     |
+------------+----------+-----------+-----------+
| S2M177     | NULL     | NULL      | 142.6     |
+------------+----------+-----------+-----------+
| S2M177     | NULL     | NULL      | 168.6     |
+------------+----------+-----------+-----------+
| S2M178     | NULL     | NULL      | 114.6     |
+------------+----------+-----------+-----------+
| S2M178     | NULL     | NULL      | 132.9     |
+------------+----------+-----------+-----------+
| S2M178     | NULL     | NULL      | 154.6     |
+------------+----------+-----------+-----------+
| S2M179     | NULL     | NULL      | 132.6     |
+------------+----------+-----------+-----------+
| S2M179     | NULL     | NULL      | 137.9     |
+------------+----------+-----------+-----------+
| S2M179     | NULL     | NULL      | 140.6     |
+------------+----------+-----------+-----------+
| S2M180     | NULL     | NULL      | 101.9     |
+------------+----------+-----------+-----------+
| S2M180     | NULL     | NULL      | 147.3     |
+------------+----------+-----------+-----------+
| S2M180     | NULL     | NULL      | 169.6     |
+------------+----------+-----------+-----------+
| S2M181     | NULL     | NULL      | 111.9     |
+------------+----------+-----------+-----------+
| S2M181     | NULL     | NULL      | 125.3     |
+------------+----------+-----------+-----------+
| S2M181     | NULL     | NULL      | 138.9     |
+------------+----------+-----------+-----------+

选择平均值的示例代码:

SELECT experiment_name, `year`, location_name, line_name, AVG(head_dap), AVG(height_cm), AVG(yield_bua)
FROM data_amba
WHERE experiment_name = 'matspr' AND `year` = 2018 AND location_name = 'Stephen'
GROUP BY line_name;

结果:

+------------+---------------+----------------+----------------+
| line_name  | AVG(head_dap) | AVG(height_cm) | AVG(yield_bua) |
+------------+---------------+----------------+----------------+
| ND_GENESIS | 0             | 0              | 132.1667       |
+------------+---------------+----------------+----------------+
| PINNACLE   | 0             | 0              | 151.8          |
+------------+---------------+----------------+----------------+
| S2M177     | 0             | 0              | 139.6          |
+------------+---------------+----------------+----------------+
| S2M178     | 0             | 0              | 134.0333       |
+------------+---------------+----------------+----------------+
| S2M179     | 0             | 0              | 137.0333       |
+------------+---------------+----------------+----------------+
| S2M180     | 0             | 0              | 139.6          |
+------------+---------------+----------------+----------------+
| S2M181     | 0             | 0              | 125.3667       |
+------------+---------------+----------------+----------------+

平均3个“ NULL”值时,我希望结果中看到的是“ NULL”,而不是“ 0”,因此它不会影响我在下游执行的任何均值计算。

2 个答案:

答案 0 :(得分:2)

我能想到的最简单的解决方法是使用NULLIF函数-

SELECT experiment_name
      ,`year`
      ,location_name
      ,line_name
      ,NULLIF(AVG(head_dap), 0)
      ,NULLIF(AVG(height_cm), 0)
      ,NULLIF(AVG(yield_bua), 0)
FROM data_amba
WHERE experiment_name = 'matspr'
AND `year` = 2018
AND location_name = 'Stephen'
GROUP BY line_name;

答案 1 :(得分:0)

它可能是MySQL WorkBench的行为,它对整数字段显示零。当我在SqlFiddle中运行相同的代码时,如果出现以下情况,则会显示 null 所有行均为空属于该类别。