我正在尝试填充指定列之间的所有NULL
列。
我的表:
如果更高级别的值中包含值,我想填充NULL
的每一列。
例如:
答案 0 :(得分:3)
这是我能想到的最好的:
update #Levels
set
level1 = CASE WHEN COALESCE(level2, level3, level4, level5, level6, level7, level8, level9, level10) IS NULL THEN level1 ELSE ISNULL(level1, 'def') END,
level2 = CASE WHEN COALESCE(level3, level4, level5, level6, level7, level8, level9, level10) IS NULL THEN level2 ELSE ISNULL(level2, 'def') END,
level3 = CASE WHEN COALESCE(level4, level5, level6, level7, level8, level9, level10) IS NULL THEN level3 ELSE ISNULL(level3, 'def') END,
level4 = CASE WHEN COALESCE(level5, level6, level7, level8, level9, level10) IS NULL THEN level4 ELSE ISNULL(level4, 'def') END,
level5 = CASE WHEN COALESCE(level6, level7, level8, level9, level10) IS NULL THEN level5 ELSE ISNULL(level5, 'def') END,
level6 = CASE WHEN COALESCE(level7, level8, level9, level10) IS NULL THEN level6 ELSE ISNULL(level6, 'def') END,
level7 = CASE WHEN COALESCE(level8, level9, level10) IS NULL THEN level7 ELSE ISNULL(level7, 'def') END,
level8 = CASE WHEN COALESCE(level9, level10) IS NULL THEN level8 ELSE ISNULL(level8, 'def') END,
level9 = CASE WHEN COALESCE(level10, null) IS NULL THEN level9 ELSE ISNULL(level9, 'def') END
看起来很乱,但是做了工作