【问题标题】:how to use case with count function in oracle plsql如何在oracle plsql中使用带有计数功能的案例
【发布时间】:2019-02-18 21:11:27
【问题描述】:

这是查询,我想在 oracle 中使用带有 count 函数的 case 语句。

  Select case when count(*) > 0 then 'doSomething' else 'doSomething'  
    from student where student_name='faizan ahmed' and student_father='ahmed' and UPPER(student_dob)=UPPER('01-FEB-19');

请帮助我,使用 plsql 代码。

ORA-00905: 缺少关键字 00905. 00000 - “缺少关键字”

【问题讨论】:

  • 您在CASE 表达式的末尾缺少END
  • case when count(*) > 0 then 'doSomething' else 'doSomething' end

标签: sql oracle oracle11g


【解决方案1】:

为此,请改用exists

Select (case when exists (select 1
                          from student
                          where student_name = 'faizan ahmed' and
                                student_father = 'ahmed' and
                                upper(student_dob) = upper('01-FEB-19');
             then 'doSomething'
             else 'doSomethingElse' 
       end)
from dual;

EXISTS 通常比计数更有效,因为它可以在第一个匹配行处停止,而不是聚合整个表。

【讨论】:

    【解决方案2】:

    CASE 缺少 END

    SELECT CASE WHEN COUNT (*) > 0 THEN 
                     'doSomething' 
                ELSE 'doSomething' 
           END                                  --> This
      FROM student
     WHERE     student_name = 'faizan ahmed'
           AND student_father = 'ahmed'
           AND UPPER (student_dob) = date '2019-02-01' -- No! UPPER ('01-FEB-19');
    

    如果您格式化您编写的代码,则更容易发现。

    除此之外,STUDENT_DOB 似乎是一个日期。如果是这样,则不要将其与字符串(因为'01-feb-19' 是字符串)进行比较,而是将其与日期(date '2019-02-01' - 它是一个日期文字,由date 关键字组成和yyyy-mm-dd 值)。

    另外,奇怪的是你使用UPPER 和那个“日期”字符串,但是你所有的名字都是小写的。嗯?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-04-21
      • 2013-09-15
      • 2015-06-10
      • 1970-01-01
      • 1970-01-01
      • 2017-08-30
      • 2014-04-29
      • 1970-01-01
      相关资源
      最近更新 更多