案例语句处理逻辑与预期不同

问题描述 投票:0回答:1

我试图根据ID的数量使用一个指标来分配一个状态。这是我写的查询(并且它工作了)。

select
    x.yyyy_mm_dd,
    x.prov_id,
    x.app,
    x.metric,
    x.is_100,
    case
        when ((x.is_100 = 'true') or size(collect_set(x.list)) >10) then 'implemented'
        when ((x.is_100 = 'false') and size(collect_set(x.list)) between 1 and 10) then 'first contact'
        else 'no contact'
    end as impl_status,
    size(collect_set(x.list)) as array_size,
    collect_set(x.list) as list
from(
    select
        yyyy_mm_dd,
        prov_id,
        app,
        metric,
        is_100,
        list
    from
        my_table
        lateral view explode(ids) e as list           
) x
group by
    1,2,3,4,5

然而,在case语句的第二个条件中, impl_status是不正确的。在结果集中,我可以看到is_100=false,array_size在1到10之间的行,然而,impl_status最终是 "no contact "而不是 "first contact"。我想也许between并不包括在内,但根据文档,似乎是这样的。

sql hive hiveql
1个回答
1
投票

我很好奇这是否可行。

(case when x.is_100 or count(distinct x.list) > 10
      then 'implemented'
      when (not x.is_100) and count(x.list) > 0 
      then 'first contact'
      else 'no contact'
 end) as impl_status,

这应该是相同的逻辑,没有字符串比较 -- 此处 是Hive中关于booleans的一个有趣的观点。 我还认为 COUNT() 比数组功能更清晰。


0
投票

确保你的字符串中没有一些隐藏的空间。

when (( trim(x.is_100) = 'false') and size(collect_set(x.list)) between 1 and 10) then 'first contact'
© www.soinside.com 2019 - 2024. All rights reserved.