我在Hive中有一个包含5列的表,即email,a_first_date,b_first_date,c_first_date,d_first_date.
a,b,c,d是用户可以采取的4种不同动作,上表中的4列表示用户进行第一次相应动作的日期.例如,'a_first_date'中的值具有用户执行操作a的日期.
输出:我想要的是2列电子邮件,overall_first_date,即用户第一次行动的日期?
示例表:(假设所有值都是除电子邮件之外的BIGINT类型)
email,a_first_date,b_first_date,c_first_date,d_first_date
abc,20140707,20140702,20140801,20140907
xyz,20140107,20140822,20140201,20141007
输出:
email,overall_first_date
abc,20140702
xyz,20140107
可能的几个解决方案是编写UDF或使用IF ELSE将这些值相互比较,然后找到最小值,但这将涉及大量的比较.
或者我可以做一个:
select email, min(action) as overall_first_date from
(
select email, a_first_date as action from mytable
UNION ALL
select email, b_first_date as action from mytable
UNION ALL
select email, c_first_date as action from mytable
UNION ALL
select email, d_first_date as action from mytable
) q1
GROUP BY email
Run Code Online (Sandbox Code Playgroud)
但这又不是一个好方法.
有人可以建议一个更好的方法来实现这一目标吗?
小智 6
你可以使用Hive的数组函数:
select email,
sort_array(array(a_first_date, b_first_date, c_first_date, d_first_date))[0] as overall_first_date
from table;
Run Code Online (Sandbox Code Playgroud)
我不确定这与CASE语句在性能方面的比较.由于您没有很多列,因此两者都同样简单.
为什么不使用 case 语句呢?这似乎已经在 SQL 中进行了彻底的讨论: https: //dba.stackexchange.com/questions/21542/what-is-the-most-efficient-way-to-get-the-minimum-of-multiple-columns -on-sql-ser