我正在尝试运行以下代码:
SELECT a.job_name
, coalesce(b.target_time, cast('08:00:00' as time(2))) sla_time
FROM ud812.slarpt_job_level_info a
left outer join (
select job_name, target_time
from ud812.slarpt_job_target_times
qualify row_number() over (partition by job_name
order by established_date desc) = 1
) b
on (a.job_name = b.job_name)
where a.display_on_sla_report = 'Y'
and a.job_type = 'LD'
and a.decom_date is null
这样做,我收到错误“选择失败3800:THEN / ELSE表达式中的数据类型不匹配。 这与我对合并的使用有关。
当我检查我的数据类型时:从ud812.slarpt_job_target_times qualify中选择type(target_time)...我得到时间(2)。 我已经尝试了几件事来调整我的代码。
这些包括:使用不同的数据类型。时间(6)。 我甚至尝试了以下内容,但sla_time的类型以整数形式返回。
SELECT a.job_name
, cast(coalesce(b.target_time, '08:00:00') as time(2)) sla_time
FROM ud812.slarpt_job_level_info a
left outer join (
select job_name, cast(target_time as char(8)) as target_time
from ud812.slarpt_job_target_times
qualify row_number() over (partition by job_name
order by established_date desc) = 1
) b
on (a.job_name = b.job_name)
where a.display_on_sla_report = 'Y'
and a.job_type = 'LD'
and a.decom_date is null
最后,我试图建立一个默认时间,每个工作名称为早上8点。 想法?
我也尝试过以下操作,但是当我键入()列时,它返回一个整数,而不是时间(2)。
SELECT a.job_name
, cast(coalesce(b.target_time, default_time) as time(2)) sla_time
FROM (
select job_name, '08:00:00' as default_time
from ud812.slarpt_job_level_info
where a.display_on_sla_report = 'Y'
and a.job_type = 'LD'
and a.decom_date is null
) a
left outer join (
select job_name, target_time
from ud812.slarpt_job_target_times
qualify row_number() over (partition by job_name
order by established_date desc) = 1
) b
on (a.job_name = b.job_name)
答案 0 :(得分:0)
在下面的测试用例中,COALESCE()
使用格式TIME '00:00:01'
作为NULL的替换值时可以正常工作。
CREATE VOLATILE TABLE MyTable, NO FALLBACK
( MyID SMALLINT NOT NULL,
MyTime TIME(2)
)
PRIMARY INDEX (MyID)
ON COMMIT PRESERVE ROWS;
INSERT INTO MyTable VALUES (1, TIME '01:23:45');
INSERT INTO MyTable VALUES (2, NULL);
SELECT *
FROM MyTable;
SELECT MyID
, COALESCE(MyTime, TIME '23:45:01')
FROM MyTable;