gpt4 book ai didi

sql - Hive(查找连续 n 列的最小值)

转载 作者:可可西里 更新时间:2023-11-01 14:17:41 27 4
gpt4 key购买 nike

我在 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
美国广播公司,20140707,20140702,20140801,20140907
xyz,20140107,20140822,20140201,20141007

输出:

电子邮件,overall_first_date
美国广播公司,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

但这同样不是一个好方法。

谁能建议一个更好的方法来实现这一目标?

最佳答案

使用函数 least()。例如;选择 *, least(col1,col2,col3) 作为 minofcol来自表名;

关于sql - Hive(查找连续 n 列的最小值),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26276698/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com