如何解决加入另一个表时如何在每个案例的案例语句中给出where条件
我想将 t.archer_fact_metrics_results 表与 b.lkp_nonpercentagekri 表连接起来,并派生出一个列derived_january。当 t,January 值介于这些阈值之间时,将 b.colour_indicator 对应的 b.proxy_percentage_value 值分配给 派生_january 否则将 t.january 值保持为导出_january 列
select
(case
when (Format = 'Monetary Amount' or Format = 'Number')
then case when (t.January >= green_threshold_min and t.January <=green_threshold_max) then b.proxy_percentage_value and b.colour_indicator='Green'
when (t.January >= amber_threshold_min and t.January <= amber_threshold_max) then b.proxy_percentage_value and b.colour_indicator = 'Amber'
when (t.January >= red_threshold_min and t.January <= red_threshold_max)then b.proxy_percentage_value and b.colour_indicator = 'Red'
else t.January end
end) as derived_january
from kri.archer_fact_metrics_results t
join kri.lkp_nonpercentagekri b on t.p2g_status = b.colour_indicator
获取错误:SQL 错误 [40000] [42000]:编译语句时出错:FAILED:ClassCastException org.apache.hadoop.hive.serde2.objectinspector.primitive.WritableDoubleObjectInspector 无法转换为 org.apache.hadoop.hive.serde2.objectinspector.primitive.BooleanObjectInspector
输入值
b.proxy_percentage_value b.colour_indicator t.January t.green_threshold_min t.green_threshold_max
55.0 Green 34 25 60
34.0 Amber 25 20 50
45.0 Red 25 35 45
amber_threshold_min 和 amber_threshold_max 以及 red_threshold_min 和 red_threshold_max 的类似数据
预期:
derived_january
55.0
34.0
25
解决方法
如果我正确理解你的逻辑,你需要在 case when 中使用像 and b.colour_indicator = 'Red'
这样的逻辑。因此它的抛出错误。
您可以使用以下 SQL -
select
(case
when (Format = 'Monetary Amount' or Format = 'Number')
then case when (t.January >= green_threshold_min and t.January <=green_threshold_max and b.colour_indicator='Green' ) then b.proxy_percentage_value
when (t.January >= amber_threshold_min and t.January <= amber_threshold_max and b.colour_indicator = 'Amber') then b.proxy_percentage_value
when (t.January >= red_threshold_min and t.January <= red_threshold_max and b.colour_indicator = 'Red' )then b.proxy_percentage_value
else t.January end
end) as derived_january
from kri.archer_fact_metrics_results t
join kri.lkp_nonpercentagekri b on t.p2g_status = b.colour_indicator
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。