使用 CASE 和 COALESCE

Use CASE & COALESCE

这个语法只会给我一个无用的错误

Incorrect syntax near mentorname

我要更新什么才能强制显示结果?

SELECT COALESCE(case when
            mentorname LIKE '%Med%' THEN 'MedTronics' end
            mentorname LIKE '%Zi%' THEN 'Zinamice' end
            , 'Total') As [Prov Source]

来自数据库 1

试试这个:

SELECT COALESCE(CASE 
                    WHEN mentorname LIKE '%Med%' 
                        THEN 'MedTronics'
                    WHEN mentorname LIKE '%Zi%' 
                        THEN 'Zinamice' 
               END
            , 'Total') As [Prov Source]

虽然您可以简化查询并完全摆脱 COALESCE 并只留下 CASE:

SELECT
    CASE 
        WHEN mentorname LIKE '%Med%'
            THEN 'MedTronics'
        WHEN mentorname LIKE '%Zi%'
            THEN 'Zinamice'
        ELSE 'Total'
    END AS [Prov Source]

为了简单起见,您可以使用嵌套的 CASE 语句完成同样的事情并完全避免 COALESCE...

SELECT 
    [Prov Source] = 
        CASE WHEN mentorname LIKE '%Med%' THEN 
            'MedTronics' 
        ELSE
            CASE WHEN mentorname LIKE '%Zi%' THEN 
                'Zinamice' 
            ELSE
                'Total'
            END
        END