[英]Case Statement with different data type
I am trying to return SLA days for particular conditions.我正在尝试针对特定条件返回 SLA 天数。 However for a specific condition I want it to return a different data type.但是对于特定条件,我希望它返回不同的数据类型。 Current code is as follows:当前代码如下:
SELECT CASE
WHEN fourthlevel.case_type IN
('Complaint')
THEN
(SELECT COUNT (*)
FROM work_days1
WHERE work_days1.business_date >
fourthlevel.cdate
AND work_days1.business_date <=
COALESCE (fourthlevel.close_date,
SYSDATE))
WHEN fourthlevel.case_type IN ('Enquiry')
THEN
(SELECT COUNT (*)
FROM work_days1
WHERE work_days1.business_date >
fourthlevel.create_date
AND work_days1.business_date <=
COALESCE (fourthlevel.close_date,
SYSDATE))
END
AS sla_days
FROM fourthlevel
I want it to return for where case_status = 'Cancelled' return 'N/A'.我希望它返回 where case_status = 'Cancelled' return 'N/A'。 I know i cant do it like this but i will include the code so it is easier to understand:我知道我不能这样做,但我将包含代码以便更容易理解:
SELECT CASE
WHEN fourthlevel.case_type IN ('Complaint')
THEN
(SELECT COUNT (*)
FROM work_days1
WHERE work_days1.business_date > fourthlevel.cdate
AND work_days1.business_date <=
COALESCE (fourthlevel.close_date, SYSDATE))
WHEN fourthlevel.case_type IN ('Enquiry')
THEN
(SELECT COUNT (*)
FROM work_days1
WHERE work_days1.business_date > fourthlevel.create_date
AND work_days1.business_date <=
COALESCE (fourthlevel.close_date, SYSDATE))
WHEN fourthlevel.case_status = 'Cancelled'
THEN
'N/A'
END
AS sla_days
FROM fourthlevel
How do i do this?我该怎么做呢?
A case
statement can only return one data type. case
语句只能返回一种数据类型。 So convert the numbers to strings:因此,将数字转换为字符串:
SELECT CASE
WHEN fourthlevel.case_type IN ('Complaint')
THEN
(SELECT cast(COUNT(*) as varchar2(255))
FROM work_days1
WHERE work_days1.business_date > fourthlevel.cdate
AND work_days1.business_date <=
COALESCE (fourthlevel.close_date, SYSDATE))
WHEN fourthlevel.case_type IN ('Enquiry')
THEN
(SELECT cast(COUNT(*) as varchar2(255))
FROM work_days1
WHERE work_days1.business_date > fourthlevel.create_date
AND work_days1.business_date <=
COALESCE (fourthlevel.close_date, SYSDATE))
WHEN fourthlevel.case_status = 'Cancelled'
THEN
'N/A'
END AS sla_days
FROM fourthlevel
Alternatively, you could return NULL
when the two conditions do not match.或者,您可以在两个条件不匹配时返回NULL
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.