简体   繁体   English

如何克服 concat Output Hive 中的 null 数据?

[英]How to overcome null data in concat Output Hive?

I have my query below:我的查询如下:

SELECT
    day PERIOD,
    'DB_shop' TB_NAME,
    CONCAT(substring(cast(percentage AS STRING),1,5),'%') AS PERCENTAGE,
    CASE
        WHEN percentage >= 20 THEN concat('Data increase ', cast(percentage AS STRING),'% from last day')
        WHEN percentage <= -20 THEN concat('Data drop ', cast(percentage AS STRING),'% from last day')
        WHEN percentage IS NULL THEN "Data Not Found"
        ELSE 'Normal'
    END AS STATUS
FROM
(
    SELECT
        day,
        count(1) count_all,
        lag(count(1)) OVER(ORDER BY day) as PrevCount,
        round(((count(1) - lag(count(1)) OVER(ORDER BY day))/lag(count(1)) OVER(ORDER BY day))*100,2) percentage
    FROM DB_shop
    WHERE day BETWEEN cast(substring(regexp_replace(cast(date_add(to_date(from_unixtime(unix_timestamp(cast(${PERIOD} as string), 'yyyyMMdd'))),-1)  as string),'-',''),1,8) as int) AND cast(substring(regexp_replace(cast(to_date(from_unixtime(unix_timestamp(cast(${PERIOD} as string), 'yyyyMMdd')))  as string),'-',''),1,8) as int)
    GROUP BY day
    ORDER BY day DESC
    LIMIT 1
)x
ORDER BY PERIOD DESC)

SELECT concat("| ", PERIOD, " | ", TB_NAME, " | ", PERCENTAGE, " | ", STATUS, " |") change_in_percentage FROM trend;
  1. If the data is not null, it will come output:如果数据不是null,就会到output:

change_in_percentage change_in_percentage

| | 20220805 | 20220805 | DB_shop | DB_shop | -5.7% | -5.7% | Normal |正常 |

  1. If the data is null, it will come output:如果数据是null,就会到output:

NULL NULL

My question: How to handle null data to produce the desired output as follows:我的问题:如何处理 null 数据以产生所需的 output 如下:

change_in_percentage change_in_percentage

| | 20220807 | 20220807 | DB_shop | DB_shop | NULL | NULL | Data Not Found |未找到数据 |

Thank you谢谢

wrap your case to coalesce with default value.包装您的案例以与默认值合并。

  coalesce(CASE
        WHEN percentage >= 20 THEN concat('Data increase ', cast(percentage AS STRING),'% from last day')
        WHEN percentage <= -20 THEN concat('Data drop ', cast(percentage AS STRING),'% from last day')
        WHEN percentage IS NULL THEN "Data Not Found"
        ELSE 'Normal'
    END, 'Data Not Found' )

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

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