Search code examples
htmlsqloracle-databasecasemarkup

Is it possible to have multiple CASE statements in a single sqlplus select statement?


I previously asked a question about formatting sqlplus output in HTML using a CASE statement.

I have been asked to amend the report to include a new column, which I have easily, but I would like to 'CASE' this column output so that it is colour coded within 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')

any ideas why this doesnt work?

output example - which strangely shows it works from sqlplus

sqlplus output for CASE in select statement


Solution

  • this should work, simple example:

    -- 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
    

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