2015-10-06 138 views
0

我以前问过使用CASE语句在HTML中输出格式化sqlplus的问题。是否可以在单个sqlplus select语句中拥有多个CASE语句?

我一直要求修改报告中包括一个新的列,这是我很容易,但我想“案”这列输出,使其颜色编码的HTML内

select upper(lpad(country,6,' ')) cntry, max(timestamp) Timestamp,substr(LPAD(test_type,10,' '),0,10) Type, 
CASE 
     WHEN ((sysdate-max(timestamp))*1440) >=60 THEN '<span class="threshold-critical">'|| ' <======= ERROR over 60 minutes since last run'||'</span>' 
     WHEN ((sysdate-max(timestamp))*1440) >=30 THEN '<span class="threshold-warning">'|| '<===== WARNING over 30 minutes since last run'||'</span>' 
    ELSE '<span class="threshold-ok">'|| '<===== GOOD_____' ||'</span>' 
    end status, 
CASE 
    WHEN (ROUND(AVG((NVL(s2_time,0)+NVL(s3_time,0)+NVL(s4_time,0)+NVL(s5_time,0)+NVL(s6_time,0)+NVL(s7_time,0)+NVL(s8_time,0)+NVL(s9_time,0)+NVL(s10_time,0))/1000),1)) >=60 THEN '<span class="average-critical"</span>' 
    WHEN (ROUND(AVG((NVL(s2_time,0)+NVL(s3_time,0)+NVL(s4_time,0)+NVL(s5_time,0)+NVL(s6_time,0)+NVL(s7_time,0)+NVL(s8_time,0)+NVL(s9_time,0)+NVL(s10_time,0))/1000),1)) >=35 THEN '<span class="average-warning"</span>' 
    ELSE '<span class="average-ok"</span>' 
END Average 
from rfgdba.perf_test_results ptr, rfgdba.perf_tests pt 
where country is not null and test_id in ((select id from rfgdba.perf_tests where live='Y')) and test_type in ('ORACLE','SIEBEL') 
and timestamp > sysdate-(59/1440) and ptr.test_id=pt.ID 
group by country, test_type 
order by country, TRUNC(timestamp, 'HH24') 

任何想法为什么这不起作用?

输出的例子 - 这奇怪表明它的工作原理的sqlplus

sqlplus output for CASE in select statement

+3

“不工作”?你遇到了什么错误? – Ollie

+1

不应该在下面添加“rem新CASE语句”进行评论吗? – Utsav

+1

是的,你当然可以在'SELECT'中使用多个'CASE'表达式。你的意思是“不起作用”?如果你[创建一个SQLFiddle](http://sqlfiddle.com/#!4),填充表格和数据,这将是有帮助的,这样人们可以帮助你弄清楚发生了什么。谢谢。 –

回答

1

这应该工作,简单的例子:

-- some test data 
with data as 
(select 1 as id, 'A' as val 
    from dual 
    union 
    select 2, 'B' from dual) 
select case 
     when id = 1 then 
      '1' 
     else 
      'not 1' 
     end as col1, 
     case 
     when val = 'B' then 
      'B' 
     else 
      'not B' 
     end as col2 
    from data 

看到http://www.sqlfiddle.com/#!4/9eecb7d/7785

+0

非常感谢 - 原来是语法错误,因为sql代码在蟾蜍中成功运行,但不是在我的Windows脚本中,另外在我的set_markup配置文件中也出现了错误 - 现在按预期工作 – nyehus