[英]How to update only first NULL column with a value in Snowflake sql?
How to update a table first Null column with value and other Null columns with the text 'Available'?如何使用值更新表的第一个空列,并使用文本“可用”更新其他空列?
My attempt: I tried using Case statements but it is affecting the performance of a query.我的尝试:我尝试使用 Case 语句,但它影响了查询的性能。
Update Emp
SET Level1 = (CASE WHEN Level1 IS NOT NULL THEN Level1 ELSE PermissionCode END),
Level2 = (CASE WHEN Level1 IS NOT NULL AND Level2 IS NULL THEN PermissionCode ELSE Level2 END),
..and so on
Is there any efficient way to update a table as mentioned below the expected format?有没有什么有效的方法来更新下面提到的预期格式的表格?
Current Table structure:
========================
| EmpID | Level1 | Level2 | Level3 | Level4 |....| Level256 | PermissionCode |
|--------|--------|--------|--------|---------|....|----------|-----------------|
| 124RY7 | abc | wsg | NULL | NULL |....| NULL | RT12345 |
| 5T7YTR | efg | NULL | NULL | NULL |....| NULL | 654GTY |
Expected Output:预期输出:
| EmpID | Level1 | Level2 | Level3 | Level4 |....| Level256 | PermissionCode |
|--------|--------|--------|---------|---------|....|----------|-----------------|
| 124RY7 | abc | wsg |RT12345 |Available|....|Available | RT12345 |
| 5T7YTR | efg | 654GTY |Available|Available|....|Available | 654GTY |
Try using a combination of nvl2 and coalesce尝试使用 nvl2 和 coalesce 的组合
select empid, lvl1, lvl2, lvl3, lvl4, PC
from temp
union all
select empid, coalesce(lvl1,lvl2,lvl3,lvl4,PC), nvl2(lvl1, coalesce(lvl2,lvl3,lvl4,PC), 'Available'), nvl2(lvl2, coalesce(lvl3,lvl4,PC), 'Available'), nvl2(lvl3, coalesce(lvl4,PC), 'Available')
, PC
from temp;
the question is what the actual usecase is.问题是实际用例是什么。 What you can do is try a bulk update (just will have to get autocommit off and commit manually) like "set levelX=Available where levelX-1 is null" for each level for all the columns, which will eventually leave you with just 1 null in every row which you can then fill with the code.
您可以做的是尝试对所有列的每个级别进行批量更新(只需要关闭自动提交并手动提交),例如“set levelX=Available where levelX-1 is null”,最终只剩下 1 null 在每一行中,然后您可以用代码填充。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.