【问题标题】:Is it possible to have multiple CASE statements in a single sqlplus select statement?单个 sqlplus select 语句中是否可以有多个 CASE 语句?
【发布时间】:2016-01-08 23:46:11
【问题描述】:

我之前问过一个关于使用 CASE 语句在 HTML 中格式化 sqlplus 输出的问题。

我被要求修改报告以包含一个新列,我很容易做到,但我想“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 中工作

【问题讨论】:

  • “不起作用”?你遇到了什么错误?
  • 不应该评论rem new CASE statement added below吗?
  • 是的,您当然可以在SELECT 中使用多个CASE 表达式。 “不起作用”是什么意思?如果您将create an SQLFiddle 填入表格和数据,这将很有帮助,这样人们就可以帮助您弄清楚发生了什么。谢谢。
  • @nyehus:请在您的问题中发布确切的异常消息
  • 我刚刚输入的“rem new CASE”行以显示新行,关于错误-我什么也没得到-我找不到

标签: html sql oracle case markup


【解决方案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

【讨论】:

  • 非常感谢 - 原来是语法错误,因为 sql 代码在 toad 中成功运行,但在我的 windows 脚本中没有,另外我的 set_markup 配置文件中也有错误 - 现在一切都按预期工作
猜你喜欢
  • 2017-01-16
  • 1970-01-01
  • 1970-01-01
  • 2023-04-07
  • 2022-11-11
  • 1970-01-01
  • 2014-12-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多