我可以插入特定值来替换每行中的第一个非NULL值

时间:2016-07-06 11:40:31

标签: sql sql-server sql-server-2014

enter image description here 我上面有一张桌子。 我想插入特定的值,称为I1,I2,I3,...,I10。为第一个非Null值。但是我要插入的值根据第一个非Null值的持续时间而变化。

例如,ID1的第一个非NULL值是持续时间3,然后我想插入" I3"进入那个特定的细胞。对于ID 2,我想将I2插入该单元格等

结果我希望看起来像 enter image description here

I1,I2,I3,...,I10是我手边的一些工业价值,它们是数字而不是字符。

有办法吗?谢谢。

1 个答案:

答案 0 :(得分:1)

您可以使用CASE表达式使用简单UPDATE mytable SET Duration1 = CASE WHEN Duration1 IS NOT NULL THEN I1 ELSE Duration1 END , Duration2 = CASE WHEN Duration1 IS NULL AND Duration2 IS NOT NULL THEN I2 ELSE Duration2 END , Duration3 = CASE WHEN Duration1 IS NULL AND Duration2 IS NULL AND Duration3 IS NOT NULL THEN I3 ELSE Duration3 END -- Continue in the same boring way , Duration10 = CASE WHEN Duration1 IS NULL AND Duration2 IS NULL AND Duration3 IS NULL AND Duration4 IS NULL AND Duration5 IS NULL AND Duration6 IS NULL AND Duration7 IS NULL AND Duration8 IS NULL AND Duration9 IS NULL AND Duration10 IS NOT NULL THEN I10 ELSE Duration10 END 来执行此操作:

react-native