gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 05:47:45 26 4
gpt4 key购买 nike

我正在尝试平均我的绘图级别数据。当我在绘图级别执行查询时,它会导致与某些行名称关联的所有行都为“NULL”。当我执行相同的查询尝试对特征进行 AVG 并按行名称分组时,结果显示“0”。我需要它显示“NULL”,这样它就不会影响我的总体意思。

选择地 block 级数据的示例代码:

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”,因此它不会影响我在下游执行的任何总均值计算。

最佳答案

我能想到的最简单的解决方法是使用 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;

关于MySQL Workbench,NULL 的平均值返回 "0",我需要它返回 NULL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58118138/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com