[英]SQL Server replace NULLs with the length of the column
There is a table这里有张桌子
foo bar id
foo1 bar1 NULL
foo2 bar2 NULL
foo2 bar2 NULL
foo3 bar3 NULL
foo4 bar4 NULL
foo4 bar4 NULL
I need to replace the NULL values with the length of the column like, 1,2,3 and so on我需要将 NULL 值替换为列的长度,例如 1、2、3 等
foo bar id
foo1 bar1 1
foo2 bar2 2
foo2 bar2 3
foo3 bar3 4
foo4 bar4 5
foo4 bar4 6
I have Googled but did not get how to make it我已经用谷歌搜索了,但没有得到如何制作它
As commented by jarlh, it looks like you are trying to assign incremental numbers to column id
, ordered by foo
, then bar
:正如 jarlh 所评论的那样,您似乎正在尝试将增量数字分配给列
id
,按foo
排序,然后是bar
:
select
foo,
bar,
case when id is null then row_number() over(order by foo, bar) else id end id
from mytable
If you are looking for an update:如果您正在寻找更新:
with cte as (
select
foo,
bar,
id,
row_number() over(order by foo, bar) rn
from mytable
)
update cte set id = rn
where id is null
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.