[英]Case expression in where clause in Oracle
I have a pull some data out of Oracle tables based on some condition. 我根据某些条件从Oracle表中提取了一些数据。
If a person with JobFunctionCode = 'ADMIN' then when he resign the notified_termination_date populate and then his data should be picked up. 如果JobFunctionCode ='ADMIN'的人辞职,则在填充notify_termination_date时,应该提取他的数据。
The person from other JobFunctionCode like Manager, Finance etc when they resign the accepted_termination_date populates then their data should be picked up. 来自其他JobFunctionCode(例如Manager,Finance等)的人员在辞职accept_termination_date时将填充他们的数据。
So pull out data based on these conditions I wrote a case statement in my WHERE
clause, but its giving missing parenthesis error. 因此,根据这些条件提取数据,我在
WHERE
子句中编写了一个case语句,但是它给出了缺失的括号错误。 This SQL is a scheduled run so I have set the termination dates to sysdate
and this will run on a daily basis. 该SQL是计划的运行,因此我将终止日期设置为
sysdate
并且它将每天运行。
select ppos.person_id, paam.position_id, pjf.job_function_code
from
per_all_assignments_m paam, per_periods_of_service ppos, per_jobs_f pjf
where
case
when pjf.job_function_code = 'ADMIN' then (TO_CHAR(ppos.notified_termination_date, 'YYYY-MM-DD') = TO_CHAR(SYSDATE, 'YYYY-MM-DD'))
else (TO_CHAR(ppos.accepted_termination_date, 'YYYY-MM-DD') = TO_CHAR(SYSDATE, 'YYYY-MM-DD'))
end
and paam.person_id = ppos.person_id
and paam.job_id = pjf.job_id
and paam.primary_flag = 'Y'
and TO_CHAR(paam.effective_end_date, 'YYYY-MM-DD') = '4712-12-31'
and TO_CHAR(pjf.effective_end_date, 'YYYY-MM-DD') = '4712-12-31'
Also, there can be the condition when Person from both JobFunctionCode terminates then both person data should be pulled out, so I don't think in this case a CASE
expression can be used. 另外,可能存在这样一个条件,当两个JobFunctionCode中的Person都终止时,则应同时提取两个person数据,因此我认为在这种情况下不能使用
CASE
表达式。
Please let me know how to write the logic here? 请让我知道如何在这里写逻辑?
The where clause should be: where子句应为:
where
(
(pjf.job_function_code = 'ADMIN' and TO_CHAR(ppos.notified_termination_date, 'YYYY-MM-DD') = TO_CHAR(SYSDATE, 'YYYY-MM-DD'))
or (pjf.job_function_code <> 'ADMIN' and TO_CHAR(ppos.accepted_termination_date, 'YYYY-MM-DD') = TO_CHAR(SYSDATE, 'YYYY-MM-DD'))
)
and <rest of clauses>
I havent looked at the rest of it, whether the TO-CHAR is required, etc, but this will fix your where clause. 我没有看其余的内容,是否需要TO-CHAR,等等,但这将解决您的where子句。
You can try : 你可以试试 :
((case
when pjf.job_function_code = 'ADMIN'
case
when (TO_CHAR(ppos.notified_termination_date, 'YYYY-MM-DD') = TO_CHAR(SYSDATE, 'YYYY-MM-DD')) then 'Y'
else 'N'
end
else
case
when (TO_CHAR(ppos.accepted_termination_date, 'YYYY-MM-DD') = TO_CHAR(SYSDATE, 'YYYY-MM-DD')) then 'Y'
else 'N'
end
end )='Y'
or exists (select * from <<tablename>> pjf.job_function_code = 'ADMIN'
and (TO_CHAR(ppos.notified_termination_date, 'YYYY-MM-DD')) = (TO_CHAR(ppos.accepted_termination_date, 'YYYY-MM-DD'))));
PS: Just check the brackets closure, and write the query in exists clause as per your table design PS:只需检查方括号的关闭情况,然后根据您的表设计将查询写到exist子句
In where
clause case when is not allowed, you can write your condition below way 在
where
子句不允许的情况下,您可以通过以下方式编写条件
where
( pjf.job_function_code = 'ADMIN' AND
(TO_CHAR(ppos.notified_termination_date, 'YYYY-MM-DD') = TO_CHAR(SYSDATE, 'YYYY-MM-DD'))
Or (TO_CHAR(ppos.accepted_termination_date, 'YYYY-MM-DD') = TO_CHAR(SYSDATE, 'YYYY-MM-DD'))
)
and paam.person_id = ppos.person_id
and paam.job_id = pjf.job_id
and paam.primary_flag = 'Y'
and TO_CHAR(paam.effective_end_date, 'YYYY-MM-DD') = '4712-12-31'
and TO_CHAR(pjf.effective_end_date, 'YYYY-MM-DD') = '4712-12-31'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.