2017-08-10 85 views
0

我正在嘗試爲以代碼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. 

回答

0

你可以嘗試這樣的:

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 
+0

我無法應用代碼'AC'。我將不得不爲代碼「AC」應用相應的ID。 – user2926497

+0

你可以使用子查詢進行查找嗎?編輯於回答 – Andomar

+0

謝謝。 Wil試着回來。 – user2926497