hive - 选择代表组的 MAX(DATE) 的字符串列?[蜂巢]
问题描述
我有一个产品表
Product Number | Type | Code | Date
P1-A D D1 2020-03-18
P1-A D D2 2020-03-20
P1-A D D4 2020-03-29
P1-A P P1 2020-01-20
P1-A P P3A 2020-01-22
P2-A D D1 2020-04-10
P3-A ... ... ...
我想了解每组代码的最大日期是 [D2,D4] 还是 [P3A,P4] 以及哪个代码代表最大日期Product Number
。Type
这段代码是错误的,但它显示了我的想法
SELECT Product Number, MAX(D Code), MAX(D Date), MAX(P Code), MAX(P Date)
FROM product_table WHERE code = 'D2' OR code = 'D4' or code = 'P3A' or code = 'P4'
GROUP BY Product Number, Type
决赛桌看起来像
Product Number | D Code | D Date | P Code | P Date |
P1-A D4 2020-03-29 P3A 2020-01-22
... ... ... ... ...
.... ... ... ... ...
如何使用 Hive 正确执行此操作?
解决方案
使用条件聚合:
SELECT Product_Number,
max(case when type = 'D' then code end) as D_code,
max(case when type = 'D' then date end) as D_date,
max(case when type = 'P' then code end) as P_code,
max(case when type = 'P' then date end) as P_date
FROM product_table
WHERE code in('D2','D4','P3A','P4')
GROUP BY Product_Number;
推荐阅读
- android - 以编程方式连接到热点(无互联网)的 android 设备切换回带互联网的 wifi
- python - Python 2.7 无法从 cmd.exe Windows 10 加载 DLL(VC2017 内置)
- c# - 23 kB XML 文件的解析速度超过半秒
- jenkins - 如何获取当前的 Jenkins 流水线 StepContext
- regex - 如何在pycharm中获取分页符
- wpf - 将样式应用于 wpf 自定义日历
- mongodb - Mongo db.stats 显示集合存在,但显示集合返回空
- python - 如何将基本脚本中的代码转换为具有实例的类?
- elixir - dialyzer 无法识别 elixir 函数并出现错误:0:unknown_function
- c# - 返回一个对象的实例