我想寫入嵌套的情況下,當條件查詢中存儲的值將來自一個案例當條件和另一種情況時條件到相同的新column.to得到這種結果我寫的查詢爲:如何在postgresql查詢中嵌入case-when條件
(case when sq_name_new1 like format('%%%s%%',demo.name) THEN count(sq_name_new1) else (when demo.empcode is not null then count(demo.id) End) END) AS indivisual from res_scheduledjobs
在上面的查詢demo.name列
來自CTE.so我整個查詢的樣子:
with demo(empcode,id,name) as
(select hr_employee.emp_code,hr_employee.id,concat(resource_resource.name,' ',hr_employee.middle_name,' ',hr_employee.last_name) as name from hr_employee inner join resource_resource on resource_resource.id=hr_employee.resource_id)
select demo.empcode,demo.name,sq_name_new1,(case when sq_name_new1 like format('%%%s%%',demo.name) THEN count(sq_name_new1) else (when demo.empcode is not null then count(demo.id) End) END) AS indivisual from res_scheduledjobs LEFT JOIN demo on demo.id=res_scheduledjobs.assigned_technician group by res_scheduledjobs.assigned_technician,sq_name_new1,demo.empcode,demo.name ;
我只是想存儲的數(sq_name_new1)列轉換爲INDIVISUAL列,並將(demo.id)列的計數轉換爲同一列t如果第一個案例條件不匹配,則帽子在INDIVISUAL中。 但是當我執行我的查詢時,它會拋出一個錯誤。也就是說,條件語句的情況是錯誤的。
請幫我喲寫出正確的嵌套case-when條件。
只需使用'CASE WHEN THEN ...當 THEN ... ELSE '...但你應該格式化您的查詢,並有可能出現其他問題。 –