我有一个像这样的 hive 表:
create external table test(
test_id string,
test_name string,
description string,
clicks int,
last_referred_click_date string
)
ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t'
STORED AS TEXTFILE LOCATION '{some_location}';
我需要找出 test_id 的总点击次数和最后一次点击日期(该组 test_id 中的最大日期)
我正在做这样的事情
insert overwrite table test partition(weekending='{input_date}')
select s.test_id,s.test_name,s.description,max(click_date),
sum(t.click) as clicks
group by s.test_id,s.test_name,s.description order by clicks desc;
max() 函数是否适用于字符串?我的 click_date 是 'yyyy-mm-dd' 格式并且是字符串数据类型?如果没有,我可以在这里做什么? UDF ?
最佳答案
SELECT s.test_id,
s.test_name,
s.description,
MAX(CAST(last_referred_click_date as DateTime)),
sum(t.clicks) as Total_Clicks
FROM test s
WHERE s.test_id=1
GROUP BY s.test_id,s.test_name,s.description
ORDER BY clicks desc;
关于sql - Hive:通过查找组中的最大值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9923587/