将HIVE查询结果中的空值或NULL值替换为特定值 [英] Replace the empty or NULL value with specific value in HIVE query result
问题描述
当查询未返回任何选定列的结果时,我试图显示一个默认值Others。我会告诉你这个例子。
这个查询返回一个空值 os(agent)SO
(在第一行):
select country,os(agent)SO,counts(*)from clicks_data
where country is not null and os(agent)不是零
按国家划分,os(代理);
输出:
ZA 4
ZA Android 4
ZA Mac 8
ZA Windows 5
相反,我想得到这个结果:
ZA其他4
ZA Android 4
ZA Mac 8
ZA Windows 5
我的下一个尝试是这个查询,但它并没有真正起作用:
select country,regexp_replace(os(agent),'','Others') SO,counts(*)来自clicks_data
,其中country不为null并且os(代理)不为空
按国家分组,os(代理);
这是结果:
ZA其他4
ZA OthersAOthersnOthersdOthersrOthersoOthersiOthersdOthers 4
ZA OthersMOthersaOtherscOthers 8
ZA OthersWOthersiOthersnOthersdOthersoOtherswOtherssOthers 5
LENGTH()
来检查列值的长度。它返回> 0,如果有某个值,则返回0为空值或空值。 同时在 CASE WHEN .. 。END
block
最终查询可能如下所示:
SELECT country,CASE WHEN LENGTH(os(agent))> 0 THEN os(agent)ELSE'Others'END as,COUNT(*)
FROM clicks_data
WHERE国家不是NULL并且os(代理)不是NULL
GROUP BY country,os (剂);
希望这可以帮到你!!!
I'm trying to show a default value, "Others", when the query does not return any result for one of the selected columns. I'll show you the example.
This query returns an empty value for os(agent) SO
(in the first row):
select country, os(agent) SO, count(*) from clicks_data
where country is not null and os(agent) is not null
group by country, os(agent);
Output:
ZA 4
ZA Android 4
ZA Mac 8
ZA Windows 5
Instead, I would like to get this result:
ZA Others 4
ZA Android 4
ZA Mac 8
ZA Windows 5
My next attempt was this query, but it's not really working, either:
select country, regexp_replace(os(agent),'','Others') SO, count(*) from clicks_data
where country is not null and os(agent) is not null
group by country, os(agent);
This is the result:
ZA Others 4
ZA OthersAOthersnOthersdOthersrOthersoOthersiOthersdOthers 4
ZA OthersMOthersaOtherscOthers 8
ZA OthersWOthersiOthersnOthersdOthersoOtherswOtherssOthers 5
Use LENGTH()
to check the length of the column value. It returns > 0, if there is some value else return 0 for empty or NULL value.
Also frame the column value in CASE WHEN ... END
block
The final query may look like:
SELECT country, CASE WHEN LENGTH(os(agent)) > 0 THEN os(agent) ELSE 'Others' END AS SO, COUNT(*)
FROM clicks_data
WHERE country IS NOT NULL AND os(agent) IS NOT NULL
GROUP BY country, os(agent);
Hope this help you!!!
这篇关于将HIVE查询结果中的空值或NULL值替换为特定值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!