有没有办法在 db2 中的选择查询中为某个值选择别名



我正在从以下查询中查询数据

select count(*) as Total, 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

上面的查询返回以下结果

TOTAL  SLABREACHED
 93            0
  7            1

SLABREACHED仅包含 0 s 和 1 s。

但是我想在我的选择查询中用SLABREACHED替换0,用WithInSLA替换1,因为我无法更改数据集。

任何帮助将不胜感激。

您可以使用

CASE WHEN表达式尝试以下操作

select count(*) as Total,case when SLABREACHED=0 then 'SLABREACHED' 
when SLABREACHED=1 then 'WithInSLA' 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

我很好奇你为什么不希望结果在一行中:

select sum(SLABREACHED) as WithInSLA,
       sum(1 - SLABREACHED) as SLABREACHED
from mwp_main
where problemsince >= current timestamp - 7 days and
      problemsince < current timestamp and
      status not in ('OPEN', 'REASSIGNED',' REASSIGNED RESPONSE', 'REOPEN') ;

最新更新