首页 > 解决方案 > 选择代表组的 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 NumberType

这段代码是错误的,但它显示了我的想法

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 正确执行此操作?

标签: hivehiveql

解决方案


使用条件聚合:

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;

推荐阅读