Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

Replace Nulls with next row values for the same Id

$
0
0

Hi All,

I have a table with 4 columns, let say ID,   Note1 , Note2  and  Type

ID   Note1  Note2    Type
1        A       null      Good
1        A       null      Good
1        B       null      Good
1       null      X        Bad
1       null      Y        Bad
1       null      Z        Bad
2        D      null      Good
2      null       E        Bad
3      null       F        Bad
3       G        null     Good

4       H         null     Good

Type is based on Note1 values. If Note1  is not null then Type= Good, If Note1 is null then Type =Bad
I want output to be like this..

ID   Note1  Note2  
1         A       X
1         A       Y
1         B       Z
2         D       E
3         G       F

4          H      null

Create Statement:

Create table TestTable
( ID int Not Null,
Note1  varchar(10) null,
Note2  varchar(10) null,
Type varchar(10) null
)

Insert Statement:

Insert into TestTable (ID ,Note1,Note2,Type) Values (1, 'A', null,'Good')
Insert into TestTable (ID ,Note1,Note2,Type) Values (1 ,'A' ,null,'Good')
Insert into TestTable (ID ,Note1,Note2,Type) Values (1 ,'B',null,'Good')
Insert into TestTable (ID ,Note1,Note2,Type) Values (1,null, 'X' ,'Bad')
Insert into TestTable (ID ,Note1,Note2,Type) Values (1, null,'Y' , 'Bad')
Insert into TestTable (ID ,Note1,Note2,Type) Values (1,null, 'Z' ,'Bad')
Insert into TestTable (ID ,Note1,Note2,Type) Values (2, 'D',null,'Good')
Insert into TestTable (ID ,Note1,Note2,Type) Values (2,null,'E' ,'Bad')
Insert into TestTable (ID ,Note1,Note2,Type) Values (3,null,'F','Bad')
Insert into TestTable (ID ,Note1,Note2,Type) Values (3,'G', null,'Good')

Insert into TestTable (ID ,Note1,Note2,Type) Values (4,'H', null,'Good')


Thanks,
RH


sql





Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>