I want to set the doprocess flag to 0 if the below data is empty between any of the targets mentioned
Example:
create table TestSAMP (
id int identity(1,1),
modelid navrchar(max),
target1 nvarchar(max),
target2 nvarchar(max),
target3 nvarchar(max),
target4 nvarchar(max),
doprcoess int default(1)
)
--VALID SET DOPROCESS FLAG TO 1
INSERT INTO TestSAMP(modelid,target1,target2,target3,target4)
VALUES('1','T1','T2','T3','T4')
--NOTVALID SET DOPROCESS FLAG TO 0 DUE TO THE DATA IS MISSING IN SEQUENCE
INSERT INTO TestSAMP(modelid,target1,target2,target3,target4)
VALUES('2','TR','','T3','T4')
--VALID SET DOPROCESS FLAG TO 1 As if data is present
-- it should be present insequence in below t1 t2 as they arfe in sequence
INSERT INTO TestSAMP(modelid,target1,target2,target3,target4)
VALUES('3','T1','T2','','')
--NOTVALID SET DOPROCESS FLAG TO 0 DUE TO THE DATA IS MISSING IN SEQUENCE
--where T4 data is provided and not in T3
INSERT INTO TestSAMP(modelid,target1,target2,target3,target4)
VALUES('4','T1','T2','','T4')
I have got lot of solution where people try out to find the sequential
number but here the case is nvarchar
You could treat gaps and islands as string i.e. 'IGGI', squash it, and search for 'IGI' pattern:
SELECT *, CASE WHEN squashed LIKE '%IGI%' THEN 0 ELSE 1 END AS new_doprocess
FROM TestSAMP t
CROSS APPLY(SELECT STRING_AGG(CASE WHEN t = '' THEN 'G' ELSE 'I'END, '') r
FROM (VALUES (1,id, target1), (2,id, target2),
(3,id, target3), (4,id, target4)) sub(rn, id, t)) s
CROSS APPLY (SELECT replace(replace(replace(replace(replace(replace(s.r,'G','<>')
,'><',''),'<>','G'),'I','<>'),'><',''),'<>','I')) AS sub(squashed)
ORDER BY t.id;
db<>fiddle demo
Example:
id | target1 | target2 | target3 | target4 | r | squashed | doprocess
-----|----------|----------|----------|----------|-------|-----------|-----------
1 | T1 | T2 | T3 | T4 | IIII | I | 1
2 | T1 | | T3 | T4 | IGII | IGI | 0
3 | T1 | T2 | | | IIGG | IG | 1
4 | T1 | T2 | | T4 | IIGI | IGI | 0
5 | | | | T4 | GGGI | GI | 1
6 | | | | | GGGG | G | 1
The easiest way should be a Boolean expression checking if a predecessor is empty when a successor is not.
UPDATE testsamp
SET doprocess = 0
WHERE target2 <> ''
AND target1 = ''
OR target3 <> ''
AND target2 = ''
OR target4 <> ''
AND target3 = '';
You might want to extend it for example if it's also invalid, when all targets are empty. I don't know if you want that.