CASE类型字符变化且布尔值不能匹配

时间:2018-07-24 14:48:56

标签: sql

我写的代码是

case 
    when employee_today = employee_today THEN employee_today
    when employee_today = null THEN employee_today = employee_yesterday
        else 'Null'
        END AS source,
count(*) AS total_count

我尝试了:: varchar,但它似乎不想合作,有什么建议吗?

3 个答案:

答案 0 :(得分:1)

因此,当Employee_today出现为空值时,我试图用employee_yesterday值替换空值。

这实际上是我可以做到的

COALESCE(employee_today, employee_yesterday, 'Employee Unknown') source,

因此,这使我可以将employee_today中的所有空值替换为employee_yesterday中的值,如果它们都为空,那么我将其替换为“ Employee Unknown”

答案 1 :(得分:0)

我想这就是你想要的:

source =
case 
    when employee_today is null THEN employee_yesterday
    else employee_today 
END,
count(*) AS total_count

您的陈述有误:

case 
    --this would always be true unless it was NULL
    when employee_today = employee_today THEN employee_today 
    -- NULL != NULL, you need to use IS NULL unless NULL is a text value and then it
    --should be quoted with = 'NULL'. Also, this isn't how you set the value to the column
    when employee_today = null THEN employee_today = employee_yesterday
        --else NULL is automatic in CASE expressions, but here you are setting it to the string 'NULL' 
        --which may or may not be intended... usually it is not
        else 'Null'
        END AS source,
count(*) AS total_count

答案 2 :(得分:0)

尝试一下

case 
    when employee_today = 'employee_today' THEN employee_today
    when employee_today is null THEN 'employee_yesterday'
        else 'Null'
        END AS source,
count(*) AS total_count

如果这不起作用,请通知我,我将进行相应的修改。