简体   繁体   English

CASE WHEN THEN 如何处理 NULL 值

[英]How does CASE WHEN THEN treat NULL values

Assuming that I have the following piece of code in the SELECT clause which is being executed on Spark:假设我在 Spark 上执行的 SELECT 子句中有以下代码段:

...
MEAN(CASE
         WHEN (col1 = 'A'
               AND (col3 = 'A' OR col4 = 'B')) THEN col2
     END) AS testing,
...

What would be the output of this query when col2 is NULL ?col2NULL时,此查询的输出是什么? Are the rows containing col2=NULL be ignored by the MEAN function? MEAN函数是否会忽略包含col2=NULL的行?

The result will be NULL . 结果将为NULL It will have the type of col2 -- this might matter in some databases (or if you are saving the result to a table). 它将具有col2类型-在某些数据库中(或者将结果保存到表中),这可能很重要。

What is the MEAN() function? 什么是MEAN()函数? To calculate the average, use AVG() . 要计算平均值,请使用AVG() This is the standard function for calculating averages in SQL. 这是用于在SQL中计算平均值的标准函数。

Disclaimer - don't know Apache Spark! 免责声明-不知道Apache Spark!

I've created a SQL Fiddle - http://sqlfiddle.com/#!9/6f7d5e/3 . 我创建了一个SQL Fiddle- http: //sqlfiddle.com/#!9/6f7d5e/3。

If col2 is null, it is not included in the average, unless all the matching records are null. 如果col2为null,则它不包括在平均值中,除非所有匹配记录均为null。

I believe you meant AVG . 我相信您的意思是AVG It will ignore NULL values. 忽略NULL值。 So if the result of case expression is: 因此,如果case表达式的结果是:

100
200
300
NULL

Then the result would be (100 + 200 + 300) / 3 = 200 instead of (100 + 200 + 300) / 4 . 然后,结果将是(100 + 200 + 300) / 3 = 200而不是(100 + 200 + 300) / 4 And in case the result of the case expression is: 如果case表达式的结果是:

NULL
NULL
NULL
NULL

Then the result will be NULL instead of 0. 然后结果将为NULL而不是0。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM