我需要根据CASE子句中使用的参数进行查询。
参数将包含以下值:contactSuccess,contactFail,notcontacted
我们有一个列:empCommunication,它是布尔值。它将为0,1,为空
基于Parameter值,我需要运行查询。
例如:如果参数为contactSuccess,则Where子句应为 empCommunication = 1
如果参数为contactFail,则Where子句应为 empCommunication = 0
以上两种情况均适用于以下查询,但如果失败,则
未联系参数,在这种情况下,Where子句应为 empCommunication是NULL。
请帮助我。
isinstance(value, self.type)
答案 0 :(得分:0)
试试这个!
SELECT
empCategory as name,
count(*) as value
from EmployeeRecords R WHERE empCommunication =
(CASE a_column_in_table
WHEN 'contactSuccess' THEN 1
WHEN 'contactFail' THEN 0
WHEN 'notcontacted' THEN NULL
else null
END)
group by empCategory;`
答案 1 :(得分:0)
假设您的参数是p_param,则可以将查询更改为
SELECT
empCategory AS name,
COUNT(*) AS value
FROM EmployeeRecords
WHERE
(empCommunication IS NULL AND p_param = 'notcontacted')
OR (empCommunication = 1 AND p_param = 'contactSuccess')
OR (empCommunication = 0 AND p_param = 'contactFail')
GROUP BY empCategory;
答案 2 :(得分:0)
使用coalesce()
来涵盖参数为'notcontacted'
且具有类似-1
之类的值的情况:
SELECT
empCategory as name,
count(*) as value
from EmployeeRecords R
WHERE coalesce(empCommunication, -1) =
CASE ?
WHEN 'contactSuccess' THEN 1
WHEN 'contactFail' THEN 0
WHEN 'notcontacted' THEN -1
END
group by empCategory