CASE声明条件postgres
问题描述:
我正在尝试为以代码10开头的员工创建报告....并为他们应用特定的工作代码。他们在表格中有一个糟糕的工作代码。如何在 声明中检查条件?CASE声明条件postgres
Example condition :
Apply job_code 'AC' for the employees that start with the code 10.
表1:customer_test
employee_code
Job_code_id
company_code
join_year
表2:C ompany_store
Job_code_id
JOB_CODE
Company_code
Join_year
SELECT
ct.employee_code,ct.job_code_id old,ct.company_code ,cs.job_Code_id new ,cs.job_code new code,
CASE
WHEN (ct.employee_code LIKE '10%') THEN CAST(cs.job_Code_id AS varchar) //I need to apply the condition here? apply the specific job code id based on the conditions.
ELSE CAST(cs.job_Code_id AS varchar)
END AS REPORT
FROM customer_test ct,company_store cs
WHERE ct.company_code=cs.company_code
and ct.job_code_id =cs.job_code_id
and ct.join_year=cs.join_year;
Thanks in advance.
答
你可以尝试这样的:
case
when employee_code like '10%' then
(
select job_code
from customer_test
where employee_code ='AC'
)
else cast(job_Code as varchar(50))
end
我无法应用代码'AC'。我将不得不为代码“AC”应用相应的ID。 – user2926497
你可以使用子查询进行查找吗?编辑于回答 – Andomar
谢谢。 Wil试着回来。 – user2926497