CASE子句中的计数条件

时间:2015-01-06 15:18:01

标签: sql oracle case-statement

我试图根据错误代码的数量及其在SQL查询的特定时间段内出现的条件值,但我写的这个查询返回所有记录的第一个满足条件。

我做错了什么

SELECT ERROR_CODE,
       CASE
         WHEN (SELECT Count(ERROR_CODE)
               FROM   ERROR_LOG
               WHERE  CRET_DTIM > Trunc(SYSDATE - 2)
                      AND ERROR_CODE = '50001') > 50 THEN 'RED'
         WHEN (SELECT Count(ERROR_CODE)
               FROM   ERROR_LOG
               WHERE  CRET_DTIM > Trunc(SYSDATE - 2)
                      AND ERROR_CODE = '50001') < 50 THEN 'GREEN'
         WHEN (SELECT Count(ERROR_CODE)
               FROM   ERROR_LOG
               WHERE  CRET_DTIM > Trunc(SYSDATE - 1)
                      AND ERROR_CODE IS NULL) > 100 THEN 'RED'
         WHEN (SELECT Count(ERROR_CODE)
               FROM   ERROR_LOG
               WHERE  CRET_DTIM > Trunc(SYSDATE - 1)
                      AND ERROR_CODE IS NULL) < 100 THEN 'GREEN'
       END AS ALERT_COLOR
FROM   ERROR_LOG
GROUP  BY ERROR_CODE 

示例数据

50005   05-JAN-15 11.05.51.570000000 AM
50001   05-JAN-15 02.39.57.840000000 PM
50001   05-JAN-15 02.31.06.700000000 PM
50001   05-JAN-15 02.21.49.350000000 PM
50001   05-JAN-15 01.59.13.910000000 PM
50001   05-JAN-15 01.50.05.900000000 PM
50001   05-JAN-15 01.30.19.270000000 PM
50001   05-JAN-15 01.11.10.510000000 PM
50001   05-JAN-15 12.00.00.720000000 PM
        05-JAN-15 09.42.10.670000000 AM
        05-JAN-15 09.37.31.590000000 AM

DESIRED OUPUT(更改计数条件以查看所需的输出) 的 COUNT

        2
 50001  8
 50005  1

SQL

SELECT ERROR_CODE,
       CASE
         WHEN (SELECT Count(ERROR_CODE)
               FROM   ERROR_LOG
               WHERE  CRET_DTIM > Trunc(SYSDATE - 2)
                      AND ERROR_CODE = '50001') between 5 and 100 THEN 'RED'
         WHEN (SELECT Count(ERROR_CODE)
               FROM   ERROR_LOG
               WHERE  CRET_DTIM > Trunc(SYSDATE - 2)
                      AND ERROR_CODE = '50001') < 5 THEN 'GREEN'
         WHEN (SELECT Count(ERROR_CODE)
               FROM   ERROR_LOG
               WHERE  CRET_DTIM > Trunc(SYSDATE - 1)
                      AND ERROR_CODE IS NULL) > 100 THEN 'RED'
         WHEN (SELECT Count(ERROR_CODE)
               FROM   ERROR_LOG
               WHERE  CRET_DTIM > Trunc(SYSDATE - 1)
                      AND ERROR_CODE IS NULL) between 5 and 100 THEN 'GREEN'
       END AS ALERT_COLOR
FROM   ERROR_LOG
GROUP  BY ERROR_CODE

结果

Error_code   Alert_color
             GREEN
50005        ''
50001        RED

2 个答案:

答案 0 :(得分:3)

您正在为表中的每一行重复每个when子句中的子查询,没有相关性。子查询:

          (SELECT Count(ERROR_CODE)
           FROM   ERROR_LOG
           WHERE  CRET_DTIM > Trunc(SYSDATE - 2)
                  AND ERROR_CODE = '50001')
对于表格中的每一行,

...每次评估为8;因此,无论实际在该行中的错误代码如何,案例的第一个分支将始终匹配,因此您会看到每行RED

重复子查询效率很低,即使您将其关联起来也是如此。 Oracle会对它进行优化/缓存,但仍然......生成计数一次会更好:

select error_code,
  count(case when cret_dtim > trunc(sysdate - 2) then 1 end) as cnt_2_day,
  count(case when cret_dtim > trunc(sysdate - 1) then 1 end) as cnt_1_day
from error_log
where cret_dtim > trunc(sysdate - 2)
group by error_code
order by error_code nulls first;

ERROR_CODE CNT_2_DAY  CNT_1_DAY
---------- --------- ----------
(null)             2          2 
50001              8          8 
50005              1          1 

您的所有样本数据都不到一天,因此计数相同;如果你有两天前的数据,他们会有所不同。

然后您可以使用 - 一次 - 作为内部查询(如果您愿意,也可以使用CTE),并将结果用作另一级case的输入,以转换为您的颜色代码:

select error_code,
  case
    when error_code = '50001' and cnt_1_day between 5 and 100 then 'RED'
    when error_code = '50001' and cnt_2_day < 5 then 'GREEN'
    when error_code is null and cnt_1_day > 100 then 'RED'
    when error_code is null and cnt_1_day between 5 and 100 then 'GREEN'
    else null
  end as alert_colour
from 
(
  select error_code,
    count(case when cret_dtim > trunc(sysdate - 2) then 1 end) as cnt_2_day,
    count(case when cret_dtim > trunc(sysdate - 1) then 1 end) as cnt_1_day
  from error_log
  where cret_dtim > trunc(sysdate - 2)
  group by error_code
)
order by error_code nulls first;

ERROR_CODE ALERT_COLOUR
---------- ------------
(null)     (null)       
50001      RED          
50005      (null)       

这与你所说的预期结果不符,但正如我在评论中所说,我不确定你是如何从数字到你所展示的结果的。你的水桶排除了很多可能性;例如,您可能希望第一个条件为> 5。你完全忽略了你期望看到的50005。如果您的基础逻辑是任何代码集使用2天计数而空值使用1天计数 - 这是一个飞跃 - 那么您可以将它概括为:

select error_code,
  case
    when error_code is not null and cnt_not_null between 5 and 100 then 'RED'
    when error_code is not null and cnt_not_null < 5 then 'GREEN'
    when error_code is null and cnt_null > 100 then 'RED'
    when error_code is null and cnt_null between 5 and 100 then 'GREEN'
    else null
  end as alert_colour
from (
  select error_code,
    count(case when cret_dtim > trunc(sysdate - 2)
      and error_code is not null then 1 end) as cnt_not_null,
    count(case when cret_dtim > trunc(sysdate - 1)
      and error_code is null then 1 end) as cnt_null
  from error_log
  where cret_dtim > trunc(sysdate - 2)
  group by error_code
)
order by error_code nulls first;

ERROR_CODE ALERT_COLOUR
---------- ------------
(null)     (null)       
50001      RED          
50005      GREEN        

这仍然不是你预期的结果,但必须要做,直到解释逻辑(和我的错误),或者你的结果发生变化......

答案 1 :(得分:0)

case返回评估为result_expression的第一个input_expression = when_expression的{​​{1}}。试试这个。

TRUE