简体   繁体   中英

Hive query for a specific scenario

Suppose my data is like below:

在此处输入图片说明

And the output should be as below:

在此处输入图片说明

The Conditions are:

  1. If the cnsm_id consists Type_cd='P' then I need only that tuple
  2. If the cnsm_id with has either type_cd=P or type_cd=A then the record should have max(timestamp) .
  3. If the cnsm_id doesn't have the type_cd='P' then it should take the type_cd='A' record with the largest timestamp

Please who knew the answer could help me in solving this query?

SELECT
    CNSM_ID,
    MAX(t),
    TYPE_CD
FROM(
SELECT
    CNSM_ID,
    MAX(TIMESTAMP) AS t, 
    type
FROM
    TBL_NAME
GROUP BY
    CNSM_ID,
    TYPE_CD
ORDER BY t DESC) AS TBL_NAME1
GROUP BY CNSM_ID;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM