Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
423 views
in Technique[技术] by (71.8m points)

sql - Impala return null values after 'is not equal' condition

I am trying to print the top 5 sports according to the duration. I ran the below query:

with t1
AS
(
 select cast(duration as int),race,lap,sport
 from db.table1
 where exchange_code in ("tennis", "golf", "football")
 and table1_date = 20201010
 and duration is not null
 and race is not null 
 and lap is not null 
 and sport is not null
)
select sum(duration_int) ,race,lap,sport
from t1
group by race,lap,sport
order by sum(duration_int) desc
limit 5;

And the results are these:

 sum(duration_int) race   lap      sport
 [null]            first  second   golf
 408439363026         
 65886284          fourth third    football
 33687102          fifth  first    american-football    
 22642805          tenth  fifth    english-football 

As you can see I have null values after the condition IS NOT NULL.

question from:https://stackoverflow.com/questions/65918288/impala-return-null-values-after-is-not-equal-condition

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

As the Impala documentation explains, CAST() does not return an error if it cannot cast the value, it returns NULL:

If the expression value is of a type that cannot be converted to the target type, the result is NULL.

Hence, the condition duration is not null is not sufficient. Instead:

cast(duration as int) is not null

I am not sure why race, lap, and sport would have NULL values. I suspect the values may be the string '[null]'.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...