Fill Null Value with Descending Sequence


-----SQL Server 2012, 2014
create table test3 (SEQUENCE int,  SEQ_No int)
insert into test3 values(24,300),(null,301),(null,302),(null,303),(null,304),(null,305)

;with mycte as (
select Sequence,  mySum - sum(cnt) Over (Order by SEQ_NO)+ 1 as [SequenceNew]  , SEQ_No  from 
(
select * , sum(SEQUENCE) Over (Order by SEQ_NO)   mySum
, count(SEQUENCE) Over(Order by SEQ_NO) cnt from test3) t )

Update mycte 
Set Sequence=[SequenceNew] 



select * from test3

drop table test3



 
Advertisements


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s