Here solution I working on
declare @table1 table(t1id int IDENTITY(1,1) NOT NULL,Batch int,
test1 decimal(3,1),Test2 decimal(3,1) ,
Test3 decimal(3,1),
DateOn datetime)
declare @table2 table(t2id int IDENTITY(1,1) NOT NULL,Batch int,
test1 varchar (200) ,Test2 varchar (200) ,
Test3 varchar (200)
)
declare @table3 table(t3id int IDENTITY(1,1) NOT NULL,Batch int,
test1 decimal(3,1),Test2 decimal(3,1) ,
Test3 decimal(3,1)
)
insert into @table1
select 2501 , 5.1 , Null , Null , '2021-11-11 13:23:44' union
select 2501 , Null , 6.2 , Null , '2021-11-11 14:23:44' union
select 2501 , 3.4 , Null , Null , '2021-10-11 15:23:44' union
select 2501 , Null , Null , 2.6 , '2021-11-11 18:23:44'
insert into @table2
select 2501,'3.4,(5.1)','6.2','2.6'
insert into @table3
select 2501,Null,Null,Null
select * From @table1
select * From @table2
select * From @table3
DECLARE @CursorID INT = 1;
DECLARE @RunningTotal BIGINT = 0;
DECLARE @RowCnt BIGINT = 0;
DECLARE @Batch varchar(50) = '';
DECLARE @Test1 AS varchar (50)
-- get a count of total rows to process
SELECT @RowCnt = COUNT(0) FROM @table2
Select @RowCnt as Rowc
WHILE @CursorID <= @RowCnt
BEGIN
-- get a PO and Coil I want to udate
Select @Batch = BATCH
FROM @table3
where t3id = @CursorID;
-- Select older test for PO and Coil I want to udate
Select top 1 @test1 = convert(varchar(50), test1)
From
@table1 Sgng
where Sgng.test1 IS NOT NULL
and
Sgng.Batch = @Batch
order by Sgng.DateOn
UPDATE @table3
Set
test1 = IsNull(Convert( numeric(38, 4), @test1), test1)
where Test1 is NULL
and
Batch = @Batch
SET @RunningTotal += @CursorID
SET @CursorID = @CursorID + 1
SET @test1 = NULL
END
select * From @table3