命名列有条件

时间:2017-03-20 20:22:40

标签: sql if-statement case impala

我有一张像this这样的表格:
我的任务是准备一个查询列表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 Sucesses" 除非我建立联盟并在另一个选择中写入。

2)如果没有具有相关条件的行,则结果将根本不显示KPI名称。 我希望显示的行包含KPI名称和null或0值。我试图使用if语句实现这一点,但是我发现将越来越多的condidtions嵌套到一个大的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

1 个答案:

答案 0 :(得分:0)

首先检查条件越有选择性,然后使用else管理无值

<html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
    <title></title>
    <script src="js/jquery-3.1.1.min.js"></script>    
    <link href="css/w3.css" rel="stylesheet" />
    <link href="css/jquery-ui.css" rel="stylesheet" />

    <asp:ContentPlaceHolder ID="head" runat="server">

    </asp:ContentPlaceHolder>
</head>
    <body>
        <form id="form1" runat="server">
            <div class="w3-sidebar w3-bar-block w3-collapse w3-card-2 w3-animate-left" style="width: 200px;" id="mySidebar">
            <button class="w3-bar-item w3-button w3-large w3-hide-large" onclick="w3_close()">Close &times;</button>
            <a href="#" class="w3-bar-item w3-button">Link 1</a>
            <a href="#" class="w3-bar-item w3-button">Link 2</a>
            <a href="#" class="w3-bar-item w3-button">Link 3</a>
        </div>

        <div class="w3-main" style="margin-left: 200px">
            <div class="w3-teal">
                <button class="w3-button w3-teal w3-xlarge w3-hide-large" onclick="w3_open()">&#9776;</button>
                <div class="w3-container">
                    <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
                    </asp:ContentPlaceHolder>
                    <%--<h1>My Page</h1>--%>
                </div>
            </div>
        </div>
        <script>
            function w3_open() {
                document.getElementById("mySidebar").style.display = "block";
            }
            function w3_close() {
                document.getElementById("mySidebar").style.display = "none";

            }
        </script>
    </form>


</body>