我在 Hive 中有一个表,有 5 列,即电子邮件、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 类型)
电子邮件、a_first_date、b_first_date、c_first_date、d_first_date
abc,20140707,20140702,20140801,20140907
xyz,20140107,20140822,20140201,20141007
Output:
电子邮件、总体首次日期
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
但这又不是一个好方法。
谁能建议一个更好的方法来实现这一目标?