[英]How to get min and max from 7 columns in Hive Hue excluding zeros
您可以使用least
和greatest
来获取最小值和最大值,并使用when
删除0
。
select *,
least(
case when col1 != 0 then col1 else 99999999 end,
case when col2 != 0 then col2 else 99999999 end,
case when col3 != 0 then col3 else 99999999 end,
case when col4 != 0 then col4 else 99999999 end,
case when col5 != 0 then col5 else 99999999 end,
case when col6 != 0 then col6 else 99999999 end,
case when col7 != 0 then col7 else 99999999 end,
) as `Min`
greatest(
case when col1 != 0 then col1 else -99999999 end,
case when col2 != 0 then col2 else -99999999 end,
case when col3 != 0 then col3 else -99999999 end,
case when col4 != 0 then col4 else -99999999 end,
case when col5 != 0 then col5 else -99999999 end,
case when col6 != 0 then col6 else -99999999 end,
case when col7 != 0 then col7 else -99999999 end
) as `Max`
from mytable
您可以使用显式case
表达式:
select (case when col1 <> 0 and col1 >= col2 and col1 >= col3 and col1 > =col4 and col1 >= col5 and col1 >= col6 and col1 >= col7
then col1
. . .
end)
这是很多打字。 这可能表明您的数据存储不正确——您应该为每列设置单独的行。 您可以通过扩展数据并重新聚合来模拟这一点:
select name, state, col1, col2, col3, col4, col5, col6, col7,
min(case when col1 <> 0 then col1 end) as min_value,
max(case when col1 <> 0 then col1 end) as max_value,
from ((select t.*, col1 as col from t) union all
(select t.*, col2 as col from t) union all
(select t.*, col3 as col from t) union all
(select t.*, col4 as col from t) union all
(select t.*, col5 as col from t) union all
(select t.*, col6 as col from t) union all
(select t.*, col7 as col from t)
) x
group by name, state, col1, col2, col3, col4, col5, col6, col7;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.