簡體   English   中英

如何克服 concat Output Hive 中的 null 數據?

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

我的查詢如下:

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. 如果數據不是null,就會到output:

change_in_percentage

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

  1. 如果數據是null,就會到output:

NULL

我的問題:如何處理 null 數據以產生所需的 output 如下:

change_in_percentage

| 20220807 | DB_shop | NULL | 未找到數據 |

謝謝

包裝您的案例以與默認值合並。

  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