sql - Hive 查询 - 从每个组中获取多个字段的最大值和总和

标签 sql hadoop hiveql

我有一张如下表:

id | most_recent_run  | flag1 | flag2
---+------------------+-------+------
1  | 2017-01-01 10:40 | 0     | 1
1  | 2017-01-01 18:30 | 1     | 1 
2  | 2017-02-28 04:30 | 1     | 0

我想查询这个表,这样对于每个 id ,我得到 max(most_recent_run) , sum(flag1)sum(flag2) .这应该是查询结果:
id | max_most_recent_run | flag1_count | flag2_count
---+---------------------+-------------+------------
1  | 2017-01-01 18:30    | 1           | 2
2  | 2017-02-28 04:30    | 1           | 0

我尝试使用收集和排名函数的组合编写此查询,但我没有得到预期的结果。

在这方面的任何解释或方向将不胜感激。

谢谢!

最佳答案

您应该使用 SQL group by 语句,然后使用 maxsum功能,即:

select id,
    max(most_recent_run) as max_most_recent_run,
    sum(flag1) as flag1_count,
    sum(flag2) as flag2_count
from my_table
group by id

关于sql - Hive 查询 - 从每个组中获取多个字段的最大值和总和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48715431/

相关文章:

sql - Oracle SQLPlus : How to display the output of a sqlplus command without having to first issue the spool off command?

hadoop - Hive:执行错误,从org.apache.hadoop.hive.ql.exec.FunctionTask返回代码-101

hive - 在配置单元中添加一个具有常量值的新列

hadoop - HiveServer2-使用 “hive”用户执行所有查询-错误?

sql - Oracle Top-N 查询 : are results guaranteed to be ordered?

mysql - SQL 存储过程不工作

java - 为什么检查文件是否存在于 hadoop 中会导致 NullPointerException?

database - 我应该提交作业以激发 Spark ,还是可以从客户端库运行它们?

hadoop - 使用项目相似度 hadoop 作业具有预先计算的项目相似度的基于可扩展实时项目的 mahout 推荐器?

sql - 具有大量或未定义类别的交叉表