有没有办法在db2的select查询中为某个值选择ALIAS?

时间:2018-12-26 10:50:16

标签: sql 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仅包含01 s。

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

任何帮助将不胜感激。

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

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

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') ;