在DB2中的选择查询中计算百分比

时间:2018-12-31 10:20:31

标签: sql db2

我有一个b / m查询,该查询根据其状态(SLABREACHED,WithInSLA)来计算计数和分组记录。

select count(*) as Total,case when SLABREACHED=0 then 'WithInSLA' 
when SLABREACHED=1 then 'SLABREACHED' end as SLABREACHED
from mwp_main
where problemsince >= current timestamp - 7 days and problemsince < current timestamp
and status not in ('OPEN','REASSIGNED','REASSIGNED RESPONSE','REOPEN') 
group by slabreached

我需要计算SLABREACHED和WithINSLA记录的总百分比以及它们的总数。我已经为此花了很长时间,但无法弄清楚如何在上述查询中计算百分比。

当前查询结果是:

     Total                                   SLABREACHED

      68                                     WithInSLA

      10                                     SLABREACHED

我需要:

列::

       Total         Percentage              SLABREACHED

       68             80%                        WithInSLA

       10             20%                        SLABREACHED

任何帮助将不胜感激

3 个答案:

答案 0 :(得分:0)

您可以在查询和没有分组依据的查询之间使用交叉联接

val list = mutableListOf("1", "2", "3")

val listOfLists = mutableListOf(
    list.toMutableList(),
    list.toMutableList(),
    list.toMutableList()
)

listOfLists.first()[0] = "Hello"

print(listOfLists)

答案 1 :(得分:0)

我不确定100%是否要计算-80%/ 20%不太合理。但是,如果要除以总数,请使用窗口函数:

<dl>
<dt>Last Name:</dt><dd>Josh</dd>
<dt>Time:</dt><dd>12:12:01</dd>
</dl>

答案 2 :(得分:0)

请参阅OLAP specification中的ratio_to_report olap函数的描述。 应该是:

select 
  count(*) as Total
, int(ratio_to_report(count(*)) over() * 100) || '%' as Percentage
, case when SLABREACHED=0 then 'WithInSLA'
  when SLABREACHED=1 then 'SLABREACHED' end as SLABREACHED
from mwp_main
where problemsince >= current timestamp - 7 days and problemsince < current timestamp
and status not in ('OPEN','REASSIGNED','REASSIGNED RESPONSE','REOPEN') 
group by slabreached;