带条件的命名列

wkftcu5l  于 2021-06-26  发布在  Impala
关注(0)|答案(1)|浏览(368)

我有一张这样的table:
我的任务是准备一个查询来列出KPI及其计算。
1) 首先,我尝试了这样一种情况,比如这样的陈述(简称):

select case  
        When PROTOCOLID = 61002 AND TRANS_STATS_TYPE = 3 THEN "S11 Session Successes"
        When PROTOCOLID = 61002 AND TRANS_STATS_TYPE = 3 AND (CAUSE_CODE is NULL OR CAUSE_CATEGORY="S") THEN "S11 Update Successes"  
        When PROTOCOLID = 61002 AND TRANS_STATS_TYPE = 7 THEN "Modify Access Bearer Successes"   
    End kpi_name, Sum(count) cnt

这给我带来了两个问题:
1) case when语句在满足第一个条件时停止,因此它将成功地确定行并将其标记为“s11 session successs”,但它不会识别“s11 update successes”,除非我创建一个并集并将其写入另一个select。
2) 如果没有具有相关条件的行,则结果将根本不显示kpi名称。我更希望显示带有kpi名称和null或0值的行。我试图使用if语句来实现这一点,但是我发现在一个大if语句中嵌套越来越多的条件是相当讨厌的,而且不容易修改。
有没有更优雅的方式来实现这两个我错过了?谢谢您
编辑:提供更多信息:此查询产生不正确的结果:

select case
    --When S1AP_SGS_PROTOCOL_ID = 36412 AND S1AP_SGS_TRANS_TYPE = 16 AND ( S1AP_SGS_TRANS_CAUSE_VALUE is NULL OR CAUSE_CATEGORY="S")                    THEN     "HO Preparation Successes"
    When S1AP_SGS_PROTOCOL_ID = 36412 AND S1AP_SGS_TRANS_TYPE = 16    THEN     "HO Preparation Attempts"
    End kpi_name,                       
    Sum(trans_count) cnt

 From  s1mme_agg_start_time_2017022811                      
    where time >= "2017-02-28 11:00:00" and time < "2017-02-28 12:00:00"
    group by kpi_name

ho准备尝试4560
ho准备成功28
虽然此查询提供了正确的结果:

select 
    When S1AP_SGS_PROTOCOL_ID = 36412 AND S1AP_SGS_TRANS_TYPE = 16 THEN "HO Preparation Attempts"
End kpi_name,                       
Sum(trans_count) cnt

From  s1mme_agg_start_time_2017022811                      
where time >= "2017-02-28 11:00:00" and time < "2017-02-28 12:00:00"
group by kpi_name

union

select case
    When S1AP_SGS_PROTOCOL_ID = 36412 AND S1AP_SGS_TRANS_TYPE = 16 AND ( S1AP_SGS_TRANS_CAUSE_VALUE is NULL OR CAUSE_CATEGORY="S")                    THEN     "HO Preparation Successes"

        End kpi_name,                       
    Sum(trans_count) cnt

From  s1mme_agg_start_time_2017022811                      
    where time >= "2017-02-28 11:00:00" and time < "2017-02-28 12:00:00"
    group by kpi_name

ho准备尝试4588
ho准备成功28

t1qtbnec

t1qtbnec1#

只需检查更具选择性的when condition first,并使用else来管理no value

select case
      When PROTOCOLID = 61002 AND TRANS_STATS_TYPE = 3 
              AND (CAUSE_CODE is NULL OR CAUSE_CATEGORY="S") THEN "S11 Update Successes"
      When PROTOCOLID = 61002 AND TRANS_STATS_TYPE = 3 THEN "S11 Session Successes"
      When PROTOCOLID = 61002 AND TRANS_STATS_TYPE = 7 THEN "Modify Access Bearer Successes"
      ELSE 'no value'
    End kpi_name
    , Sum(count) cnt
 .......

相关问题