[英]how to use case with count function in oracle plsql
here is the query, i want to use case statement with count function in oracle. 这是查询,我想在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');
please help me out, using plsql code. 请使用plsql代码帮助我。
ORA-00905: missing keyword 00905. 00000 - "missing keyword" ORA-00905:缺少关键字00905。00000-“缺少关键字”
For this purpose, use exists
instead: 为此,请改用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
is usually more efficient than a count, because it can stop at the first matching row instead of aggregating the whole table. EXISTS
通常比计数更有效,因为它可以停在匹配的第一行,而不是汇总整个表。
You're missing an END
for CASE
: 您缺少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');
It is easier to spot if you format code you write. 如果您格式化编写的代码,则更容易发现。
Apart from that, STUDENT_DOB
seems to be a date . 除此之外, STUDENT_DOB
似乎是一个date 。 If so, then don't compare it to a string (because '01-feb-19'
IS a string) but to a date ( date '2019-02-01'
- it is a date literal , consists of the date
keyword and yyyy-mm-dd
value). 如果是这样,则不要将其与字符串(因为'01-feb-19'
是字符串)进行比较,而不能与日期( date '2019-02-01'
它是日期文字 ,由date
关键字组成)进行比较和yyyy-mm-dd
值)。
Also, it is strange that you used UPPER
with that "date" string, but all your names are lowercase. 同样,奇怪的是,您在该“日期”字符串中使用了UPPER
,但是您所有的名字都是小写的。 Hm? 嗯?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.