嵌套CASE无法正常工作

时间:2018-04-16 03:17:18

标签: sql case

以下SQL查询可让我找到Office 365更新频道。但是当Registry表没有计算机条目时,它不处理这种情况。 我尝试了许多不同的嵌套CASES,都失败了。我只是一个SQL新手。有人能指出正确的方向吗?

很好,但如果缺少tblRegistry则无法处理。

SELECT
tblAssets.AssetName,
CASE 
    WHEN tblRegistry.Value = 'http://officecdn.microsoft.com/pr/492350f6-3a01-4f97-b9c0-c7c6ddf67d60' THEN 'Monthly' 
    WHEN tblRegistry.Value = 'http://officecdn.microsoft.com/pr/7ffbc6bf-bc32-4f92-8982-f9dd17fd3114' THEN 'Semi-Annual' 
    WHEN tblRegistry.Value = 'http://officecdn.microsoft.com/pr/64256afe-f5d9-4f86-8936-8840a6a4f5be' THEN 'Monthly Targeted' 
    WHEN tblRegistry.Value = 'http://officecdn.microsoft.com/pr/b8f9b850-328d-4355-9145-c59439a0c4cf' THEN 'Semi-Annual Targeted'
    ELSE 'Bad-Registry'+'_'+tblRegistry.Value END AS o365channel
FROM tblAssets
INNER JOIN tblRegistry ON tblRegistry.AssetID = tblAssets.AssetID
WHERE (tblRegistry.Regkey = 'HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Office\ClickToRun\Configuration') AND (tblRegistry.Valuename = 'UpdateChannel')

非常糟糕

SELECT
tblAssets.AssetName,
CASE WHEN tblAssets.AssetName in 
    (
    select tblAssets.AssetName
    FROM tblAssets
    INNER JOIN tblRegistry ON tblRegistry.AssetID = tblAssets.AssetID
    WHERE (tblRegistry.Regkey = 'HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Office\ClickToRun\Configuration') AND (tblRegistry.Valuename = 'UpdateChannel')
    )
    select tblAssets.AssetName,
    CASE
        WHEN tblRegistry.Value = 'http://officecdn.microsoft.com/pr/492350f6-3a01-4f97-b9c0-c7c6ddf67d60' THEN 'Monthly' 
        WHEN tblRegistry.Value = 'http://officecdn.microsoft.com/pr/7ffbc6bf-bc32-4f92-8982-f9dd17fd3114' THEN 'Semi-Annual' 
        WHEN tblRegistry.Value = 'http://officecdn.microsoft.com/pr/64256afe-f5d9-4f86-8936-8840a6a4f5be' THEN 'Monthly Targeted' 
        WHEN tblRegistry.Value = 'http://officecdn.microsoft.com/pr/b8f9b850-328d-4355-9145-c59439a0c4cf' THEN 'Semi-Annual Targeted'
        ELSE 'Bad-RegKey'+'_'+tblRegistry.Value
    END
ELSE 'Bad-RegMissing'
END AS o365channel
FROM tblAssets

提前致谢, ccutler

1 个答案:

答案 0 :(得分:0)

试试这个:

SELECT tblAssets.AssetName,
       CASE 
            WHEN tblRegistry.Value = 'http://officecdn.microsoft.com/pr/492350f6-3a01-4f97-b9c0-c7c6ddf67d60' THEN 'Monthly' 
            WHEN tblRegistry.Value = 'http://officecdn.microsoft.com/pr/7ffbc6bf-bc32-4f92-8982-f9dd17fd3114' THEN 'Semi-Annual' 
            WHEN tblRegistry.Value = 'http://officecdn.microsoft.com/pr/64256afe-f5d9-4f86-8936-8840a6a4f5be' THEN 'Monthly Targeted' 
            WHEN tblRegistry.Value = 'http://officecdn.microsoft.com/pr/b8f9b850-328d-4355-9145-c59439a0c4cf' THEN 'Semi-Annual Targeted'
            ELSE 'Bad-Registry'+'_'+tblRegistry.Value
       END AS o365channel
FROM tblAssets
LEFT JOIN tblRegistry
    ON tblRegistry.AssetID = tblAssets.AssetID
    AND (tblRegistry.Regkey = 'HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Office\ClickToRun\Configuration')
    AND (tblRegistry.Valuename = 'UpdateChannel')