简体   繁体   English

针对特定场景的 Hive 查询

[英]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如果cnsm_id包含Type_cd='P'那么我只需要那个元组
  2. If the cnsm_id with has either type_cd=P or type_cd=A then the record should have max(timestamp) .如果cnsm_id具有type_cd=Ptype_cd=A则记录应该有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如果cnsm_id没有type_cd='P'那么它应该采用具有最大时间戳的type_cd='A'记录

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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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