首页 > 解决方案 > 案例语句处理逻辑与预期不同

问题描述

我正在尝试根据使用指标的 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 最终是“没有联系”而不是“第一次联系”。我在想也许 between 不包括在内,但它似乎是根据文档。

标签: sqlhivehiveql

解决方案


我很好奇这是否有效:

(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 中布尔值的有趣观点。我也认为这COUNT()比数组功能更清晰。


推荐阅读