sql - 案例语句处理逻辑与预期不同
问题描述
我正在尝试根据使用指标的 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 不包括在内,但它似乎是根据文档。
解决方案
我很好奇这是否有效:
(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()
比数组功能更清晰。
推荐阅读
- c - 是否可以在不违反严格别名的情况下将字符数组用作内存池?
- c++ - 可以创建一个函数来一般设置传入的结构成员吗?
- json - 如何访问地图列表中的数据?
- python - 在 Python 中评分 R 模型
- scala - 如何在 Spark 读取时捕获 spark.read FileNotFoundException?
- ruby-on-rails - 重命名 Rails 6.1 应用程序后的 Webpack / Yarn 错误
- python - 在基于函数的视图中将多个值解析为 user_passes_test
- python - 从 mpl 平行坐标图中删除图例?
- angular - Angular 的 Kendo UI 不显示任何数据
- c# - 如何将字符串从 httpcontet.current.session.getstring 转换为某个给定类型